Skip to content

Commit 7f6f420

Browse files
authored
RUST-807 Disallow maxPoolSize=0 (#491)
1 parent 751e1f6 commit 7f6f420

File tree

6 files changed

+79
-6
lines changed

6 files changed

+79
-6
lines changed

src/client/options/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,12 @@ impl ClientOptions {
11781178
}
11791179
}
11801180

1181+
if let Some(0) = self.max_pool_size {
1182+
return Err(crate::error::Error::invalid_argument(
1183+
"cannot specify maxPoolSize=0",
1184+
));
1185+
}
1186+
11811187
Ok(())
11821188
}
11831189

src/client/options/test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ async fn run_test(test_file: TestFile) {
5353
)
5454
)
5555
)
56+
// The Rust driver disallows `maxPoolSize=0`.
57+
|| test_case.description.contains("maxPoolSize=0 does not error")
5658
{
5759
continue;
5860
}
@@ -113,6 +115,15 @@ async fn run_test(test_file: TestFile) {
113115
}
114116
}
115117

118+
// The default types parsed from the test file don't match those serialized
119+
// from the `ClientOptions` struct.
120+
if let Ok(min) = json_options.get_i32("minpoolsize") {
121+
json_options.insert("minpoolsize", Bson::Int64(min.into()));
122+
}
123+
if let Ok(max) = json_options.get_i32("maxpoolsize") {
124+
json_options.insert("maxpoolsize", Bson::Int64(max.into()));
125+
}
126+
116127
options_doc = options_doc
117128
.into_iter()
118129
.filter(|(ref key, _)| json_options.contains_key(key))

src/cmap/worker.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ impl ConnectionPoolWorker {
340340
});
341341
}
342342

343+
fn below_max_connections(&self) -> bool {
344+
self.total_connection_count < self.max_pool_size
345+
}
346+
343347
fn can_service_connection_request(&self) -> bool {
344348
if !matches!(self.state, PoolState::Ready) {
345349
return false;
@@ -349,8 +353,7 @@ impl ConnectionPoolWorker {
349353
return true;
350354
}
351355

352-
self.total_connection_count < self.max_pool_size
353-
&& self.pending_connection_count < MAX_CONNECTING
356+
self.below_max_connections() && self.pending_connection_count < MAX_CONNECTING
354357
}
355358

356359
async fn check_out(&mut self, request: ConnectionRequest) {
@@ -381,7 +384,7 @@ impl ConnectionPoolWorker {
381384
}
382385

383386
// otherwise, attempt to create a connection.
384-
if self.total_connection_count < self.max_pool_size {
387+
if self.below_max_connections() {
385388
let event_handler = self.event_handler.clone();
386389
let establisher = self.establisher.clone();
387390
let pending_connection = self.create_pending_connection();

src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ impl Error {
9494
.into()
9595
}
9696

97+
pub(crate) fn invalid_argument(message: impl Into<String>) -> Error {
98+
ErrorKind::InvalidArgument {
99+
message: message.into(),
100+
}
101+
.into()
102+
}
103+
97104
pub(crate) fn is_state_change_error(&self) -> bool {
98105
self.is_recovering() || self.is_not_master()
99106
}

src/test/spec/json/uri-options/connection-pool-options.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"tests": [
33
{
44
"description": "Valid connection pool options are parsed correctly",
5-
"uri": "mongodb://example.com/?maxIdleTimeMS=50000",
5+
"uri": "mongodb://example.com/?maxIdleTimeMS=50000&maxPoolSize=5&minPoolSize=3",
66
"valid": true,
77
"warning": false,
88
"hosts": null,
99
"auth": null,
1010
"options": {
11-
"maxIdleTimeMS": 50000
11+
"maxIdleTimeMS": 50000,
12+
"maxPoolSize": 5,
13+
"minPoolSize": 3
1214
}
1315
},
1416
{
@@ -28,6 +30,28 @@
2830
"hosts": null,
2931
"auth": null,
3032
"options": {}
33+
},
34+
{
35+
"description": "maxPoolSize=0 does not error",
36+
"uri": "mongodb://example.com/?maxPoolSize=0",
37+
"valid": true,
38+
"warning": false,
39+
"hosts": null,
40+
"auth": null,
41+
"options": {
42+
"maxPoolSize": 0
43+
}
44+
},
45+
{
46+
"description": "minPoolSize=0 does not error",
47+
"uri": "mongodb://example.com/?minPoolSize=0",
48+
"valid": true,
49+
"warning": false,
50+
"hosts": null,
51+
"auth": null,
52+
"options": {
53+
"minPoolSize": 0
54+
}
3155
}
3256
]
3357
}

src/test/spec/json/uri-options/connection-pool-options.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
tests:
22
-
33
description: "Valid connection pool options are parsed correctly"
4-
uri: "mongodb://example.com/?maxIdleTimeMS=50000"
4+
uri: "mongodb://example.com/?maxIdleTimeMS=50000&maxPoolSize=5&minPoolSize=3"
55
valid: true
66
warning: false
77
hosts: ~
88
auth: ~
99
options:
1010
maxIdleTimeMS: 50000
11+
maxPoolSize: 5
12+
minPoolSize: 3
1113
-
1214
description: "Non-numeric maxIdleTimeMS causes a warning"
1315
uri: "mongodb://example.com/?maxIdleTimeMS=invalid"
@@ -24,3 +26,23 @@ tests:
2426
hosts: ~
2527
auth: ~
2628
options: {}
29+
30+
-
31+
description: "maxPoolSize=0 does not error"
32+
uri: "mongodb://example.com/?maxPoolSize=0"
33+
valid: true
34+
warning: false
35+
hosts: ~
36+
auth: ~
37+
options:
38+
maxPoolSize: 0
39+
40+
-
41+
description: "minPoolSize=0 does not error"
42+
uri: "mongodb://example.com/?minPoolSize=0"
43+
valid: true
44+
warning: false
45+
hosts: ~
46+
auth: ~
47+
options:
48+
minPoolSize: 0

0 commit comments

Comments
 (0)