Skip to content

Commit 278a50d

Browse files
authored
PYTHON-3005 Make maxConnecting configurable (#789)
1 parent a7fb328 commit 278a50d

File tree

6 files changed

+111
-3
lines changed

6 files changed

+111
-3
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ Notable improvements
185185

186186
- Enhanced connection pooling to create connections more efficiently and
187187
avoid connection storms.
188+
- Added the ``maxConnecting`` URI and
189+
:class:`~pymongo.mongo_client.MongoClient` keyword argument.
188190
- :class:`~pymongo.mongo_client.MongoClient` now accepts a URI and keyword
189191
argument `srvMaxHosts` that limits the number of mongos-like hosts a client
190192
will connect to. More specifically, when a mongodb+srv:// connection string

pymongo/client_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def _parse_pool_options(options):
138138
options.get('zlibcompressionlevel', -1))
139139
ssl_context, tls_allow_invalid_hostnames = _parse_ssl_options(options)
140140
load_balanced = options.get('loadbalanced')
141+
max_connecting = options.get('maxconnecting', common.MAX_CONNECTING)
141142
return PoolOptions(max_pool_size,
142143
min_pool_size,
143144
max_idle_time_seconds,
@@ -148,6 +149,7 @@ def _parse_pool_options(options):
148149
appname,
149150
driver,
150151
compression_settings,
152+
max_connecting=max_connecting,
151153
server_api=server_api,
152154
load_balanced=load_balanced)
153155

pymongo/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ def validate_auto_encryption_opts_or_none(option, value):
615615
'journal': validate_boolean_or_string,
616616
'localthresholdms': validate_positive_float_or_zero,
617617
'maxidletimems': validate_timeout_or_none,
618+
'maxconnecting': validate_positive_integer,
618619
'maxpoolsize': validate_non_negative_integer_or_none,
619620
'maxstalenessseconds': validate_max_staleness,
620621
'readconcernlevel': validate_string_or_none,

pymongo/mongo_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ def __init__(
216216
- `maxIdleTimeMS` (optional): The maximum number of milliseconds that
217217
a connection can remain idle in the pool before being removed and
218218
replaced. Defaults to `None` (no limit).
219+
- `maxConnecting` (optional): The maximum number of connections that
220+
each pool can establish concurrently. Defaults to `2`.
219221
- `socketTimeoutMS`: (integer or None) Controls how long (in
220222
milliseconds) the driver will wait for a response after sending an
221223
ordinary (non-monitoring) database operation before concluding that
@@ -506,7 +508,8 @@ def __init__(
506508
arguments.
507509
The default for `uuidRepresentation` was changed from
508510
``pythonLegacy`` to ``unspecified``.
509-
Added the ``srvServiceName`` URI and keyword argument.
511+
Added the ``srvServiceName`` and ``maxConnecting`` URI and keyword
512+
argument.
510513
511514
.. versionchanged:: 3.12
512515
Added the ``server_api`` keyword argument.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"version": 1,
3+
"style": "integration",
4+
"description": "custom maxConnecting is enforced",
5+
"runOn": [
6+
{
7+
"minServerVersion": "4.4.0"
8+
}
9+
],
10+
"failPoint": {
11+
"configureFailPoint": "failCommand",
12+
"mode": "alwaysOn",
13+
"data": {
14+
"failCommands": [
15+
"isMaster",
16+
"hello"
17+
],
18+
"closeConnection": false,
19+
"blockConnection": true,
20+
"blockTimeMS": 500
21+
}
22+
},
23+
"poolOptions": {
24+
"maxConnecting": 1,
25+
"maxPoolSize": 2,
26+
"waitQueueTimeoutMS": 5000
27+
},
28+
"operations": [
29+
{
30+
"name": "ready"
31+
},
32+
{
33+
"name": "start",
34+
"target": "thread1"
35+
},
36+
{
37+
"name": "start",
38+
"target": "thread2"
39+
},
40+
{
41+
"name": "checkOut",
42+
"thread": "thread1"
43+
},
44+
{
45+
"name": "waitForEvent",
46+
"event": "ConnectionCreated",
47+
"count": 1
48+
},
49+
{
50+
"name": "checkOut",
51+
"thread": "thread2"
52+
},
53+
{
54+
"name": "waitForEvent",
55+
"event": "ConnectionReady",
56+
"count": 2
57+
}
58+
],
59+
"events": [
60+
{
61+
"type": "ConnectionCreated"
62+
},
63+
{
64+
"type": "ConnectionReady"
65+
},
66+
{
67+
"type": "ConnectionCreated"
68+
},
69+
{
70+
"type": "ConnectionReady"
71+
}
72+
],
73+
"ignore": [
74+
"ConnectionCheckOutStarted",
75+
"ConnectionCheckedIn",
76+
"ConnectionCheckedOut",
77+
"ConnectionClosed",
78+
"ConnectionPoolCreated",
79+
"ConnectionPoolReady"
80+
]
81+
}

test/uri_options/connection-pool-options.json

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
"tests": [
33
{
44
"description": "Valid connection pool options are parsed correctly",
5-
"uri": "mongodb://example.com/?maxIdleTimeMS=50000&maxPoolSize=5&minPoolSize=3",
5+
"uri": "mongodb://example.com/?maxIdleTimeMS=50000&maxPoolSize=5&minPoolSize=3&maxConnecting=1",
66
"valid": true,
77
"warning": false,
88
"hosts": null,
99
"auth": null,
1010
"options": {
1111
"maxIdleTimeMS": 50000,
1212
"maxPoolSize": 5,
13-
"minPoolSize": 3
13+
"minPoolSize": 3,
14+
"maxConnecting": 1
1415
}
1516
},
1617
{
@@ -52,6 +53,24 @@
5253
"options": {
5354
"minPoolSize": 0
5455
}
56+
},
57+
{
58+
"description": "maxConnecting=0 causes a warning",
59+
"uri": "mongodb://example.com/?maxConnecting=0",
60+
"valid": true,
61+
"warning": true,
62+
"hosts": null,
63+
"auth": null,
64+
"options": {}
65+
},
66+
{
67+
"description": "maxConnecting<0 causes a warning",
68+
"uri": "mongodb://example.com/?maxConnecting=-1",
69+
"valid": true,
70+
"warning": true,
71+
"hosts": null,
72+
"auth": null,
73+
"options": {}
5574
}
5675
]
5776
}

0 commit comments

Comments
 (0)