1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#[cfg(test)]
mod test;
mod background;
pub(crate) mod conn;
mod establish;
pub(crate) mod options;
mod wait_queue;
use std::{
sync::{
atomic::{AtomicU32, Ordering},
Arc,
RwLock,
},
time::{Duration, Instant},
};
use derivative::Derivative;
pub use self::conn::ConnectionInfo;
pub(crate) use self::conn::{Command, CommandResponse, Connection, StreamDescription};
use self::{
establish::ConnectionEstablisher,
options::ConnectionPoolOptions,
wait_queue::WaitQueue,
};
use crate::{
client::auth::Credential,
error::{ErrorKind, Result},
event::cmap::{
CmapEventHandler,
ConnectionCheckoutFailedEvent,
ConnectionCheckoutFailedReason,
ConnectionCheckoutStartedEvent,
ConnectionClosedReason,
PoolClearedEvent,
PoolClosedEvent,
PoolCreatedEvent,
},
options::{StreamAddress, TlsOptions},
};
const DEFAULT_MAX_POOL_SIZE: u32 = 100;
/// A pool of connections implementing the CMAP spec. All state is kept internally in an `Arc`, and
/// internal state that is mutable is additionally wrapped by a lock.
#[derive(Clone, Debug)]
pub(crate) struct ConnectionPool {
inner: Arc<ConnectionPoolInner>,
}
impl From<Arc<ConnectionPoolInner>> for ConnectionPool {
fn from(inner: Arc<ConnectionPoolInner>) -> Self {
Self { inner }
}
}
/// The internal state of a connection pool.
#[derive(Derivative)]
#[derivative(Debug)]
pub(crate) struct ConnectionPoolInner {
/// The address the pool's connections will connect to.
address: StreamAddress,
/// The set of available connections in the pool. Because the CMAP spec requires that
/// connections are checked out in a FIFO manner, connections are pushed/popped from the back
/// of the Vec.
connections: Arc<RwLock<Vec<Connection>>>,
/// The connect timeout passed to each underlying TcpStream when attemtping to connect to the
/// server.
connect_timeout: Option<Duration>,
/// The credential to use for authenticating connections in this pool.
credential: Option<Credential>,
/// Contains the logic for "establishing" a connection. This includes handshaking and
/// authenticating a connection when it's first created.
establisher: ConnectionEstablisher,
/// The event handler specified by the user to process CMAP events.
#[derivative(Debug = "ignore")]
event_handler: Option<Arc<dyn CmapEventHandler>>,
/// The current generation of the pool. The generation is incremented whenever the pool is
/// cleared. Connections belonging to a previous generation are considered stale and will be
/// closed when checked back in or when popped off of the set of available connections.
generation: AtomicU32,
/// Connections that have been ready for usage in the pool for longer than `max_idle_time` will
/// be closed either by the background thread or when popped off of the set of available
/// connections. If `max_idle_time` is `None`, then connections will not be closed due to being
/// idle.
max_idle_time: Option<Duration>,
/// The maximum number of connections that the pool can have at a given time. This includes
/// connections which are currently checked out of the pool.
max_pool_size: u32,
/// The minimum number of connections that the pool can have at a given time. This includes
/// connections which are currently checked out of the pool. If fewer than `min_pool_size`
/// connections are in the pool, the background thread will create more connections and add
/// them to the pool.
min_pool_size: Option<u32>,
/// The ID of the next connection created by the pool.
next_connection_id: AtomicU32,
/// If a checkout operation takes longer than `wait_queue_timeout`, the pool will return an
/// error. If `wait_queue_timeout` is `None`, then the checkout operation will not time out.
wait_queue_timeout: Option<Duration>,
/// The TLS options to use for the connections. If `tls_options` is None, then TLS will not be
/// used to connect to the server.
tls_options: Option<TlsOptions>,
/// The total number of connections currently in the pool. This includes connections which are
/// currently checked out of the pool.
total_connection_count: AtomicU32,
/// Connections are checked out by concurrent threads on a first-come, first-server basis. This
/// is enforced by threads entering the wait queue when they first try to check out a
/// connection and then blocking until they are at the front of the queue.
wait_queue: WaitQueue,
}
impl ConnectionPool {
pub(crate) fn new(address: StreamAddress, mut options: Option<ConnectionPoolOptions>) -> Self {
// Get the individual options from `options`.
let connect_timeout = options.as_ref().and_then(|opts| opts.connect_timeout);
let credential = options.as_mut().and_then(|opts| opts.credential.clone());
let establisher = ConnectionEstablisher::new(options.as_ref());
let event_handler = options.as_mut().and_then(|opts| opts.event_handler.take());
// The CMAP spec indicates that a max idle time of zero means that connections should not be
// closed due to idleness.
let mut max_idle_time = options.as_ref().and_then(|opts| opts.max_idle_time);
if max_idle_time == Some(Duration::from_millis(0)) {
max_idle_time = None;
}
let max_pool_size = options
.as_ref()
.and_then(|opts| opts.max_pool_size)
.unwrap_or(DEFAULT_MAX_POOL_SIZE);
let min_pool_size = options.as_ref().and_then(|opts| opts.min_pool_size);
let tls_options = options.as_mut().and_then(|opts| opts.tls_options.take());
let wait_queue_timeout = options.as_ref().and_then(|opts| opts.wait_queue_timeout);
let inner = ConnectionPoolInner {
address: address.clone(),
connect_timeout,
credential,
establisher,
event_handler,
generation: AtomicU32::new(0),
max_idle_time,
max_pool_size,
min_pool_size,
next_connection_id: AtomicU32::new(1),
tls_options,
total_connection_count: AtomicU32::new(0),
wait_queue: WaitQueue::new(address.clone(), wait_queue_timeout),
wait_queue_timeout,
connections: Default::default(),
};
let pool = Self {
inner: Arc::new(inner),
};
pool.emit_event(move |handler| {
let event = PoolCreatedEvent { address, options };
handler.handle_pool_created_event(event);
});
background::start_background_thread(Arc::downgrade(&pool.inner));
pool
}
/// Emits an event from the event handler if one is present, where `emit` is a closure that uses
/// the event handler.
fn emit_event<F>(&self, emit: F)
where
F: FnOnce(&Arc<dyn CmapEventHandler>),
{
self.inner.emit_event(emit)
}
/// Checks out a connection from the pool. This method will block until this thread is at the
/// front of the wait queue, and then will block again if no available connections are in the
/// pool and the total number of connections is not less than the max pool size. If the method
/// blocks for longer than `wait_queue_timeout`, a `WaitQueueTimeoutError` will be returned.
pub(crate) fn check_out(&self) -> Result<Connection> {
self.emit_event(|handler| {
let event = ConnectionCheckoutStartedEvent {
address: self.inner.address.clone(),
};
handler.handle_connection_checkout_started_event(event);
});
let result = self.acquire_or_create_connection();
let mut conn = match result {
Ok(conn) => conn,
Err(e) => {
if let ErrorKind::WaitQueueTimeoutError { .. } = e.kind.as_ref() {
self.emit_event(|handler| {
handler.handle_connection_checkout_failed_event(
ConnectionCheckoutFailedEvent {
address: self.inner.address.clone(),
reason: ConnectionCheckoutFailedReason::Timeout,
},
)
});
}
return Err(e);
}
};
self.emit_event(|handler| {
handler.handle_connection_checked_out_event(conn.checked_out_event());
});
conn.mark_checked_out(Arc::downgrade(&self.inner));
Ok(conn)
}
/// Waits for the thread to reach the front of the wait queue, then attempts to check out a
/// connection.
fn acquire_or_create_connection(&self) -> Result<Connection> {
let start_time = Instant::now();
let mut handle = self.inner.wait_queue.wait_until_at_front()?;
loop {
// Try to get the most recent available connection.
while let Some(conn) = self.inner.connections.write().unwrap().pop() {
// Close the connection if it's stale.
if conn.is_stale(self.inner.generation.load(Ordering::SeqCst)) {
self.close_connection(conn, ConnectionClosedReason::Stale);
continue;
}
// Close the connection if it's idle.
if conn.is_idle(self.inner.max_idle_time) {
self.close_connection(conn, ConnectionClosedReason::Idle);
continue;
}
// Otherwise, return the connection.
return Ok(conn);
}
// Create a new connection if the pool is under max size.
if self.inner.total_connection_count.load(Ordering::SeqCst) < self.inner.max_pool_size {
return Ok(self.create_connection(true)?);
}
// Check if the pool has a max timeout.
if let Some(timeout) = self.inner.wait_queue_timeout {
// Check how long since the checkout process began.
let time_waiting = Instant::now().duration_since(start_time);
// If the timeout has been reached, return an error.
if time_waiting >= timeout {
self.emit_event(|handler| {
let event = ConnectionCheckoutFailedEvent {
address: self.inner.address.clone(),
reason: ConnectionCheckoutFailedReason::Timeout,
};
handler.handle_connection_checkout_failed_event(event);
});
return Err(ErrorKind::WaitQueueTimeoutError {
address: self.inner.address.clone(),
}
.into());
}
// Wait until the either the timeout has been reached or a connection is checked
// into the pool.
handle.wait_for_available_connection(Some(timeout - time_waiting))?;
} else {
// Wait until a connection has been returned to the pool.
handle.wait_for_available_connection(None)?;
}
}
}
/// Checks a connection back into the pool and notifies the wait queue that a connection is
/// ready. If the connection is stale, it will be closed instead of being added to the set of
/// available connections. The time that the connection is checked in will be marked to
/// facilitate detecting if the connection becomes idle.
#[cfg(test)]
pub(crate) fn check_in(&self, conn: Connection) {
self.inner.check_in(conn);
}
/// Increments the generation of the pool. Rather than eagerly removing stale connections from
/// the pool, they are left for the background thread to clean up.
pub(crate) fn clear(&self) {
self.inner.generation.fetch_add(1, Ordering::SeqCst);
self.emit_event(|handler| {
let event = PoolClearedEvent {
address: self.inner.address.clone(),
};
handler.handle_pool_cleared_event(event);
});
}
/// Internal helper to close a connection, emit the event for it being closed, and decrement the
/// total connection count. Any connection being closed by the pool should be closed by using
/// this method.
fn close_connection(&self, conn: Connection, reason: ConnectionClosedReason) {
self.emit_event(|handler| {
handler.handle_connection_closed_event(conn.closed_event(reason));
});
self.inner
.total_connection_count
.fetch_sub(1, Ordering::SeqCst);
}
/// Helper method to create a connection and increment the total connection count.
fn create_connection(&self, checking_out: bool) -> Result<Connection> {
self.inner
.total_connection_count
.fetch_add(1, Ordering::SeqCst);
let mut connection = Connection::new(
self.inner.next_connection_id.fetch_add(1, Ordering::SeqCst),
self.inner.address.clone(),
self.inner.generation.load(Ordering::SeqCst),
self.inner.connect_timeout,
self.inner.tls_options.clone(),
self.inner.event_handler.clone(),
)?;
self.emit_event(|handler| {
handler.handle_connection_created_event(connection.created_event())
});
let establish_result = self
.inner
.establisher
.establish_connection(&mut connection, self.inner.credential.as_ref());
if let Err(e) = establish_result {
self.clear();
if checking_out {
self.emit_event(|handler| {
handler.handle_connection_checkout_failed_event(ConnectionCheckoutFailedEvent {
address: self.inner.address.clone(),
reason: ConnectionCheckoutFailedReason::ConnectionError,
})
});
}
return Err(e);
}
self.emit_event(|handler| handler.handle_connection_ready_event(connection.ready_event()));
Ok(connection)
}
}
impl ConnectionPoolInner {
/// Emits an event from the event handler if one is present, where `emit` is a closure that uses
/// the event handler.
fn emit_event<F>(&self, emit: F)
where
F: FnOnce(&Arc<dyn CmapEventHandler>),
{
if let Some(ref handler) = self.event_handler {
emit(handler);
}
}
/// Checks a connection back into the pool and notifies the wait queue that a connection is
/// ready. If the connection is stale, it will be closed instead of being added to the set of
/// available connections. The time that the connection is checked in will be marked to
/// facilitate detecting if the connection becomes idle.
fn check_in(&self, mut conn: Connection) {
self.emit_event(|handler| {
handler.handle_connection_checked_in_event(conn.checked_in_event());
});
conn.mark_checked_in();
// Close the connection if it's stale.
if conn.is_stale(self.generation.load(Ordering::SeqCst)) {
self.close_connection(conn, ConnectionClosedReason::Stale);
return;
}
self.connections.write().unwrap().push(conn);
self.wait_queue.notify_ready();
}
/// Internal helper to close a connection, emit the event for it being closed, and decrement the
/// total connection count. Any connection being closed by the pool should be closed by using
/// this method.
fn close_connection(&self, conn: Connection, reason: ConnectionClosedReason) {
self.emit_event(|handler| {
handler.handle_connection_closed_event(conn.closed_event(reason));
});
self.total_connection_count.fetch_sub(1, Ordering::SeqCst);
}
}
impl Drop for ConnectionPoolInner {
/// Automatic cleanup for the connection pool. This is defined on `ConnectionPoolInner` rather
/// than `ConnectionPool` so that it only gets run once all (non-weak) references to the
/// `ConnectionPoolInner` are dropped.
fn drop(&mut self) {
for mut conn in self.connections.write().unwrap().drain(..) {
conn.pool.take();
self.emit_event(|handler| {
handler.handle_connection_closed_event(
conn.closed_event(ConnectionClosedReason::PoolClosed),
);
});
}
self.emit_event(|handler| {
handler.handle_pool_closed_event(PoolClosedEvent {
address: self.address.clone(),
});
});
}
}