@@ -64,9 +64,30 @@ pub(crate) struct ConnectionPoolInner {
64
64
/// The address the pool's connections will connect to.
65
65
address : StreamAddress ,
66
66
67
- /// If a checkout operation takes longer than `wait_queue_timeout`, the pool will return an
68
- /// error. If `wait_queue_timeout` is `None`, then the checkout operation will not time out.
69
- wait_queue_timeout : Option < Duration > ,
67
+ /// The set of available connections in the pool. Because the CMAP spec requires that
68
+ /// connections are checked out in a FIFO manner, connections are pushed/popped from the back
69
+ /// of the Vec.
70
+ connections : Arc < RwLock < Vec < Connection > > > ,
71
+
72
+ /// The connect timeout passed to each underlying TcpStream when attemtping to connect to the
73
+ /// server.
74
+ connect_timeout : Option < Duration > ,
75
+
76
+ /// The credential to use for authenticating connections in this pool.
77
+ credential : Option < Credential > ,
78
+
79
+ /// Contains the logic for "establishing" a connection. This includes handshaking and
80
+ /// authenticating a connection when it's first created.
81
+ establisher : ConnectionEstablisher ,
82
+
83
+ /// The event handler specified by the user to process CMAP events.
84
+ #[ derivative( Debug = "ignore" ) ]
85
+ event_handler : Option < Arc < dyn CmapEventHandler > > ,
86
+
87
+ /// The current generation of the pool. The generation is incremented whenever the pool is
88
+ /// cleared. Connections belonging to a previous generation are considered stale and will be
89
+ /// closed when checked back in or when popped off of the set of available connections.
90
+ generation : AtomicU32 ,
70
91
71
92
/// Connections that have been ready for usage in the pool for longer than `max_idle_time` will
72
93
/// be closed either by the background thread or when popped off of the set of available
@@ -84,75 +105,66 @@ pub(crate) struct ConnectionPoolInner {
84
105
/// them to the pool.
85
106
min_pool_size : Option < u32 > ,
86
107
108
+ /// The ID of the next connection created by the pool.
109
+ next_connection_id : AtomicU32 ,
110
+
111
+ /// If a checkout operation takes longer than `wait_queue_timeout`, the pool will return an
112
+ /// error. If `wait_queue_timeout` is `None`, then the checkout operation will not time out.
113
+ wait_queue_timeout : Option < Duration > ,
114
+
87
115
/// The TLS options to use for the connections. If `tls_options` is None, then TLS will not be
88
116
/// used to connect to the server.
89
117
tls_options : Option < TlsOptions > ,
90
118
91
- /// The credential to use for authenticating connections in this pool.
92
- credential : Option < Credential > ,
93
-
94
- /// The current generation of the pool. The generation is incremented whenever the pool is
95
- /// cleared. Connections belonging to a previous generation are considered stale and will be
96
- /// closed when checked back in or when popped off of the set of available connections.
97
- generation : AtomicU32 ,
98
-
99
119
/// The total number of connections currently in the pool. This includes connections which are
100
120
/// currently checked out of the pool.
101
121
total_connection_count : AtomicU32 ,
102
122
103
- /// The ID of the next connection created by the pool.
104
- next_connection_id : AtomicU32 ,
105
-
106
123
/// Connections are checked out by concurrent threads on a first-come, first-server basis. This
107
124
/// is enforced by threads entering the wait queue when they first try to check out a
108
125
/// connection and then blocking until they are at the front of the queue.
109
126
wait_queue : WaitQueue ,
110
-
111
- /// The set of available connections in the pool. Because the CMAP spec requires that
112
- /// connections are checked out in a FIFO manner, connections are pushed/popped from the back
113
- /// of the Vec.
114
- connections : Arc < RwLock < Vec < Connection > > > ,
115
-
116
- /// Contains the logic for "establishing" a connection. This includes handshaking and
117
- /// authenticating a connection when it's first created.
118
- establisher : ConnectionEstablisher ,
119
-
120
- /// The event handler specified by the user to process CMAP events.
121
- #[ derivative( Debug = "ignore" ) ]
122
- event_handler : Option < Arc < dyn CmapEventHandler > > ,
123
127
}
124
128
125
129
impl ConnectionPool {
126
130
pub ( crate ) fn new ( address : StreamAddress , mut options : Option < ConnectionPoolOptions > ) -> Self {
127
131
// Get the individual options from `options`.
128
- let max_idle_time = options. as_ref ( ) . and_then ( |opts| opts. max_idle_time ) ;
129
- let wait_queue_timeout = options. as_ref ( ) . and_then ( |opts| opts. wait_queue_timeout ) ;
132
+ let connect_timeout = options. as_ref ( ) . and_then ( |opts| opts. connect_timeout ) ;
133
+ let credential = options. as_mut ( ) . and_then ( |opts| opts. credential . clone ( ) ) ;
134
+ let establisher = ConnectionEstablisher :: new ( options. as_ref ( ) ) ;
135
+ let event_handler = options. as_mut ( ) . and_then ( |opts| opts. event_handler . take ( ) ) ;
136
+
137
+ // The CMAP spec indicates that a max idle time of zero means that connections should not be
138
+ // closed due to idleness.
139
+ let mut max_idle_time = options. as_ref ( ) . and_then ( |opts| opts. max_idle_time ) ;
140
+ if max_idle_time == Some ( Duration :: from_millis ( 0 ) ) {
141
+ max_idle_time = None ;
142
+ }
143
+
130
144
let max_pool_size = options
131
145
. as_ref ( )
132
146
. and_then ( |opts| opts. max_pool_size )
133
147
. unwrap_or ( DEFAULT_MAX_POOL_SIZE ) ;
134
148
let min_pool_size = options. as_ref ( ) . and_then ( |opts| opts. min_pool_size ) ;
135
149
let tls_options = options. as_mut ( ) . and_then ( |opts| opts. tls_options . take ( ) ) ;
136
- let event_handler = options. as_mut ( ) . and_then ( |opts| opts. event_handler . take ( ) ) ;
137
-
138
- let credential = options. as_mut ( ) . and_then ( |opts| opts. credential . clone ( ) ) ;
139
- let establisher = ConnectionEstablisher :: new ( options. as_ref ( ) ) ;
150
+ let wait_queue_timeout = options. as_ref ( ) . and_then ( |opts| opts. wait_queue_timeout ) ;
140
151
141
152
let inner = ConnectionPoolInner {
142
153
address : address. clone ( ) ,
143
- wait_queue_timeout,
154
+ connect_timeout,
155
+ credential,
156
+ establisher,
157
+ event_handler,
158
+ generation : AtomicU32 :: new ( 0 ) ,
144
159
max_idle_time,
145
160
max_pool_size,
146
161
min_pool_size,
162
+ next_connection_id : AtomicU32 :: new ( 1 ) ,
147
163
tls_options,
148
- credential,
149
- generation : AtomicU32 :: new ( 0 ) ,
150
164
total_connection_count : AtomicU32 :: new ( 0 ) ,
151
- next_connection_id : AtomicU32 :: new ( 1 ) ,
152
165
wait_queue : WaitQueue :: new ( address. clone ( ) , wait_queue_timeout) ,
166
+ wait_queue_timeout,
153
167
connections : Default :: default ( ) ,
154
- establisher,
155
- event_handler,
156
168
} ;
157
169
158
170
let pool = Self {
@@ -329,6 +341,7 @@ impl ConnectionPool {
329
341
self . inner . next_connection_id . fetch_add ( 1 , Ordering :: SeqCst ) ,
330
342
self . inner . address . clone ( ) ,
331
343
self . inner . generation . load ( Ordering :: SeqCst ) ,
344
+ self . inner . connect_timeout ,
332
345
self . inner . tls_options . clone ( ) ,
333
346
self . inner . event_handler . clone ( ) ,
334
347
) ?;
0 commit comments