Skip to content

Commit 9e8782b

Browse files
authored
RUST-591 Use separate ConnectionPoolOptions types for monitoring and internal usage (#326)
1 parent db97329 commit 9e8782b

File tree

5 files changed

+122
-36
lines changed

5 files changed

+122
-36
lines changed

src/cmap/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl ConnectionPool {
7878
if let Some(ref handler) = event_handler {
7979
handler.handle_pool_created_event(PoolCreatedEvent {
8080
address: address.clone(),
81-
options,
81+
options: options.map(|o| o.to_event_options()),
8282
});
8383
};
8484

src/cmap/options.rs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,43 @@ use typed_builder::TypedBuilder;
77
use crate::{
88
bson_util,
99
client::{auth::Credential, options::ServerApi},
10-
event::cmap::CmapEventHandler,
10+
event::cmap::{CmapEventHandler, ConnectionPoolOptions as EventOptions},
1111
options::{ClientOptions, DriverInfo, StreamAddress, TlsOptions},
1212
};
1313

14-
/// Contains the options for creating a connection pool. While these options are specified at the
15-
/// client-level, `ConnectionPoolOptions` is exposed for the purpose of CMAP event handling.
14+
/// Contains the options for creating a connection pool.
1615
#[derive(Clone, Default, Deserialize, TypedBuilder, Derivative)]
1716
#[derivative(Debug, PartialEq)]
1817
#[builder(field_defaults(default, setter(strip_option)))]
1918
#[serde(rename_all = "camelCase")]
20-
pub struct ConnectionPoolOptions {
19+
pub(crate) struct ConnectionPoolOptions {
2120
/// The application name specified by the user. This is sent to the server as part of the
2221
/// handshake that each connection makes when it's created.
23-
pub app_name: Option<String>,
22+
pub(crate) app_name: Option<String>,
2423

25-
/// The connect timeout passed to each underlying TcpStream when attemtping to connect to the
24+
/// The connect timeout passed to each underlying TcpStream when attempting to connect to the
2625
/// server.
2726
#[serde(skip)]
28-
pub connect_timeout: Option<Duration>,
27+
pub(crate) connect_timeout: Option<Duration>,
2928

3029
/// The credential to use for authenticating connections in this pool.
3130
#[serde(skip)]
32-
pub credential: Option<Credential>,
31+
pub(crate) credential: Option<Credential>,
3332

3433
/// Extra information to append to the driver version in the metadata of the handshake with the
3534
/// server. This should be used by libraries wrapping the driver, e.g. ODMs.
3635
#[serde(skip)]
37-
pub driver_info: Option<DriverInfo>,
36+
pub(crate) driver_info: Option<DriverInfo>,
3837

3938
/// Processes all events generated by the pool.
4039
#[derivative(Debug = "ignore", PartialEq = "ignore")]
4140
#[serde(skip)]
42-
pub event_handler: Option<Arc<dyn CmapEventHandler>>,
41+
pub(crate) event_handler: Option<Arc<dyn CmapEventHandler>>,
4342

4443
/// How often the background thread performs its maintenance (e.g. ensure minPoolSize).
4544
#[cfg(test)]
4645
#[serde(skip)]
47-
pub maintenance_frequency: Option<Duration>,
46+
pub(crate) maintenance_frequency: Option<Duration>,
4847

4948
/// Connections that have been ready for usage in the pool for longer than `max_idle_time` will
5049
/// not be used.
@@ -53,20 +52,20 @@ pub struct ConnectionPoolOptions {
5352
#[serde(rename = "maxIdleTimeMS")]
5453
#[serde(default)]
5554
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
56-
pub max_idle_time: Option<Duration>,
55+
pub(crate) max_idle_time: Option<Duration>,
5756

5857
/// The maximum number of connections that the pool can have at a given time. This includes
5958
/// connections which are currently checked out of the pool.
6059
///
6160
/// The default is 100.
62-
pub max_pool_size: Option<u32>,
61+
pub(crate) max_pool_size: Option<u32>,
6362

6463
/// The minimum number of connections that the pool can have at a given time. This includes
6564
/// connections which are currently checked out of the pool. If fewer than `min_pool_size`
6665
/// connections are in the pool, connections will be added to the pool in the background.
6766
///
6867
/// The default is that no minimum is enforced
69-
pub min_pool_size: Option<u32>,
68+
pub(crate) min_pool_size: Option<u32>,
7069

7170
/// Whether to start the pool as "ready" or not.
7271
/// For tests only.
@@ -85,7 +84,7 @@ pub struct ConnectionPoolOptions {
8584
///
8685
/// The default is not to use TLS for connections.
8786
#[serde(skip)]
88-
pub tls_options: Option<TlsOptions>,
87+
pub(crate) tls_options: Option<TlsOptions>,
8988

9089
/// Rather than wait indefinitely for a connection to become available, instead return an error
9190
/// after the given duration.
@@ -94,23 +93,42 @@ pub struct ConnectionPoolOptions {
9493
#[serde(rename = "waitQueueTimeoutMS")]
9594
#[serde(default)]
9695
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
97-
pub wait_queue_timeout: Option<Duration>,
96+
pub(crate) wait_queue_timeout: Option<Duration>,
9897
}
9998

10099
impl ConnectionPoolOptions {
101100
pub(crate) fn from_client_options(options: &ClientOptions) -> Self {
102-
let mut pool_options = Self::builder().build();
103-
pool_options.app_name = options.app_name.clone();
104-
pool_options.connect_timeout = options.connect_timeout;
105-
pool_options.credential = options.credential.clone();
106-
pool_options.driver_info = options.driver_info.clone();
107-
pool_options.event_handler = options.cmap_event_handler.clone();
108-
pool_options.max_idle_time = options.max_idle_time;
109-
pool_options.min_pool_size = options.min_pool_size;
110-
pool_options.tls_options = options.tls_options();
111-
pool_options.wait_queue_timeout = options.wait_queue_timeout;
112-
113-
pool_options
101+
Self {
102+
app_name: options.app_name.clone(),
103+
connect_timeout: options.connect_timeout,
104+
driver_info: options.driver_info.clone(),
105+
max_idle_time: options.max_idle_time,
106+
min_pool_size: options.min_pool_size,
107+
max_pool_size: options.max_pool_size,
108+
server_api: options.server_api.clone(),
109+
tls_options: options.tls_options(),
110+
wait_queue_timeout: options.wait_queue_timeout,
111+
credential: options.credential.clone(),
112+
event_handler: options.cmap_event_handler.clone(),
113+
#[cfg(test)]
114+
maintenance_frequency: None,
115+
#[cfg(test)]
116+
ready: None,
117+
}
118+
}
119+
120+
pub(crate) fn to_event_options(&self) -> EventOptions {
121+
EventOptions {
122+
app_name: self.app_name.clone(),
123+
connect_timeout: self.connect_timeout,
124+
driver_info: self.driver_info.clone(),
125+
max_idle_time: self.max_idle_time,
126+
min_pool_size: self.min_pool_size,
127+
max_pool_size: self.max_pool_size,
128+
server_api: self.server_api.clone(),
129+
tls_options: self.tls_options.clone(),
130+
wait_queue_timeout: self.wait_queue_timeout,
131+
}
114132
}
115133
}
116134

src/cmap/test/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub struct TestFile {
1717
version: u8,
1818
style: TestStyle,
1919
pub description: String,
20-
pub pool_options: Option<ConnectionPoolOptions>,
20+
pub(crate) pool_options: Option<ConnectionPoolOptions>,
2121
pub operations: Vec<ThreadedOperation>,
2222
pub error: Option<Error>,
2323
pub events: Vec<Event>,

src/cmap/test/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use self::{
1212
};
1313

1414
use crate::{
15-
cmap::{options::ConnectionPoolOptions, Connection, ConnectionPool},
15+
cmap::{Connection, ConnectionPool, ConnectionPoolOptions},
1616
error::{Error, Result},
17+
event::cmap::ConnectionPoolOptions as EventOptions,
1718
options::TlsOptions,
1819
runtime::AsyncJoinHandle,
1920
sdam::{ServerUpdate, ServerUpdateSender},
@@ -329,11 +330,10 @@ impl Matchable for TlsOptions {
329330
}
330331
}
331332

332-
impl Matchable for ConnectionPoolOptions {
333-
fn content_matches(&self, expected: &ConnectionPoolOptions) -> bool {
333+
impl Matchable for EventOptions {
334+
fn content_matches(&self, expected: &EventOptions) -> bool {
334335
self.app_name.matches(&expected.app_name)
335336
&& self.connect_timeout.matches(&expected.connect_timeout)
336-
&& self.credential.matches(&expected.credential)
337337
&& self.max_idle_time.matches(&expected.max_idle_time)
338338
&& self.max_pool_size.matches(&expected.max_pool_size)
339339
&& self.min_pool_size.matches(&expected.min_pool_size)

src/event/cmap.rs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
//! Contains the events and functionality for monitoring behavior of the connection pooling of a
22
//! `Client`.
33
4+
use std::time::Duration;
5+
46
use serde::Deserialize;
57

6-
pub use crate::cmap::options::ConnectionPoolOptions;
7-
use crate::options::StreamAddress;
8+
use crate::{
9+
client::options::{DriverInfo, ServerApi, TlsOptions},
10+
options::StreamAddress,
11+
};
812

913
/// We implement `Deserialize` for all of the event types so that we can more easily parse the CMAP
1014
/// spec tests. However, we have no need to parse the address field from the JSON files (if it's
@@ -31,6 +35,70 @@ pub struct PoolCreatedEvent {
3135
pub options: Option<ConnectionPoolOptions>,
3236
}
3337

38+
/// Contains the options for creating a connection pool. While these options are specified at the
39+
/// client-level, `ConnectionPoolOptions` is exposed for the purpose of CMAP event handling.
40+
#[derive(Clone, Default, Deserialize, Debug, PartialEq)]
41+
#[serde(rename_all = "camelCase")]
42+
#[non_exhaustive]
43+
pub struct ConnectionPoolOptions {
44+
/// The application name specified by the user. This is sent to the server as part of the
45+
/// handshake that each connection makes when it's created.
46+
pub app_name: Option<String>,
47+
48+
/// The connect timeout passed to each underlying TcpStream when attempting to connect to the
49+
/// server.
50+
#[serde(skip)]
51+
pub connect_timeout: Option<Duration>,
52+
53+
/// Extra information to append to the driver version in the metadata of the handshake with the
54+
/// server. This should be used by libraries wrapping the driver, e.g. ODMs.
55+
#[serde(skip)]
56+
pub driver_info: Option<DriverInfo>,
57+
58+
/// Connections that have been ready for usage in the pool for longer than `max_idle_time` will
59+
/// not be used.
60+
///
61+
/// The default is that connections will not be closed due to being idle.
62+
#[serde(rename = "maxIdleTimeMS")]
63+
#[serde(default)]
64+
#[serde(deserialize_with = "crate::bson_util::deserialize_duration_from_u64_millis")]
65+
pub max_idle_time: Option<Duration>,
66+
67+
/// The maximum number of connections that the pool can have at a given time. This includes
68+
/// connections which are currently checked out of the pool.
69+
///
70+
/// The default is 100.
71+
pub max_pool_size: Option<u32>,
72+
73+
/// The minimum number of connections that the pool can have at a given time. This includes
74+
/// connections which are currently checked out of the pool. If fewer than `min_pool_size`
75+
/// connections are in the pool, connections will be added to the pool in the background.
76+
///
77+
/// The default is that no minimum is enforced
78+
pub min_pool_size: Option<u32>,
79+
80+
/// The declared API version
81+
///
82+
/// The default value is to have no declared API version
83+
pub(crate) server_api: Option<ServerApi>,
84+
85+
/// The options specifying how a TLS connection should be configured. If `tls_options` is
86+
/// `None`, then TLS will not be used for the connections.
87+
///
88+
/// The default is not to use TLS for connections.
89+
#[serde(skip)]
90+
pub tls_options: Option<TlsOptions>,
91+
92+
/// Rather than wait indefinitely for a connection to become available, instead return an error
93+
/// after the given duration.
94+
///
95+
/// The default is to block indefinitely until a connection becomes available.
96+
#[serde(rename = "waitQueueTimeoutMS")]
97+
#[serde(default)]
98+
#[serde(deserialize_with = "crate::bson_util::deserialize_duration_from_u64_millis")]
99+
pub wait_queue_timeout: Option<Duration>,
100+
}
101+
34102
/// Event emitted when a connection pool becomes ready.
35103
#[derive(Clone, Debug, Deserialize, PartialEq)]
36104
#[non_exhaustive]

0 commit comments

Comments
 (0)