Skip to content

Commit 5a88c6c

Browse files
author
A. Jesse Jiryu Davis
committed
Update docs for new pool options. PYTHON-436
1 parent 77f876b commit 5a88c6c

File tree

9 files changed

+96
-16
lines changed

9 files changed

+96
-16
lines changed

doc/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Important new features:
1818
.. warning:: SIGNIFICANT BEHAVIOR CHANGE in 2.5.2+. Previously, `max_pool_size`
1919
would limit only the idle sockets the pool would hold onto, not the
2020
number of open sockets. The default has also changed, from 10 to 100.
21+
If you pass a value for ``max_pool_size`` make sure it is large enough for
22+
the expected load. (Sockets are only opened when needed, so there is no cost
23+
to having a ``max_pool_size`` larger than necessary. Err towards a larger
24+
value.) If your application accepts the default, continue to do so.
25+
26+
See :ref:`connection-pooling` for more information.
2127

2228
Changes in Version 2.5.2
2329
------------------------

doc/examples/requests.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,17 @@ the previous example more terse:
9494
2
9595
>>> client.in_request() # request automatically ended
9696
False
97+
98+
Requests And ``max_pool_size``
99+
------------------------------
100+
101+
A thread in a request retains exclusive access to a socket until its request
102+
ends or the thread dies; thus, applications in which more than 100 threads are
103+
in requests at once should disable the ``max_pool_size`` option::
104+
105+
client = MongoClient(host, port, max_pool_size=None)
106+
107+
Failure to increase or disable ``max_pool_size`` in such an application can
108+
leave threads forever waiting for sockets.
109+
110+
See :ref:`connection-pooling`

doc/faq.rst

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,60 @@ How does connection pooling work in PyMongo?
1717
Every :class:`~pymongo.mongo_client.MongoClient` instance has a built-in
1818
connection pool. The pool begins with one open connection. If necessary to
1919
support concurrent access to MongoDB from multiple threads in your application,
20-
the client will open new connections on demand.
20+
the client opens new connections on demand.
2121

2222
By default, there is no thread-affinity for connections.
2323

24-
The size of the connection pool is capped at ``max_pool_size`` (default 100).
25-
When a thread in your application begins an operation on MongoDB, if no
26-
connections are available and the pool has reach its maximum, the thread
27-
blocks waiting for a connection to be returned to the pool by another thread.
24+
In versions before 2.6, the default ``max_pool_size`` was 10, and it did not
25+
actually bound the number of open connections; it only determined the number
26+
of connections that would be kept open when no longer in use.
27+
28+
Starting with PyMongo 2.6, the size of the connection pool is capped at
29+
``max_pool_size``, which now defaults to 100. When a thread in your application
30+
begins an operation on MongoDB, if all other connections are in use and the
31+
pool has reached its maximum, the thread pauses, waiting for a connection to
32+
be returned to the pool by another thread.
33+
34+
The default configuration for a :class:`~pymongo.mongo_client.MongoClient`
35+
works for most applications::
36+
37+
client = MongoClient(host, port)
38+
39+
Create this client **once** when your program starts up, and reuse it for all
40+
operations. It is a common mistake to create a new client for each request,
41+
which is very inefficient.
42+
43+
To support extremely high numbers of concurrent MongoDB operations within one
44+
process, increase ``max_pool_size``::
45+
46+
client = MongoClient(host, port, max_pool_size=200)
47+
48+
... or make it unbounded::
49+
50+
client = MongoClient(host, port, max_pool_size=None)
51+
52+
By default, any number of threads are allowed to wait for connections to become
53+
available, and they can wait any length of time. Override ``waitQueueMultiple``
54+
to cap the number of waiting threads. E.g., to keep the number of waiters less
55+
than or equal to 500::
56+
57+
client = MongoClient(host, port, max_pool_size=50, waitQueueMultiple=10)
58+
59+
When 500 threads are waiting for a socket, the 501st that needs a connection
60+
raises :exc:`~pymongo.errors.ExceededMaxWaiters`. Use this option to
61+
bound the amount of queueing in your application during a load spike, at the
62+
cost of additional exceptions.
63+
64+
Once the pool reaches its max size, additional threads are allowed to wait
65+
indefinitely for connections to become available, unless you set
66+
``waitQueueTimeoutMS``::
67+
68+
client = MongoClient(host, port, waitQueueTimeoutMS=100)
69+
70+
A thread that waits more than 100ms (in this example) for a connection raises
71+
:exc:`~pymongo.errors.ConnectionFailure`. Use this option if it is more
72+
important to bound the duration of operations during a load spike than it is to
73+
complete every operation.
2874

2975
When :meth:`~pymongo.mongo_client.MongoClient.disconnect` is called by any thread,
3076
all sockets are closed.

pymongo/connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ def __init__(self, host=None, port=None, max_pool_size=10,
8484
it must be enclosed in '[' and ']' characters following
8585
the RFC2732 URL syntax (e.g. '[::1]' for localhost)
8686
- `port` (optional): port number on which to connect
87-
- `max_pool_size` (optional): The maximum size limit for
88-
the connection pool.
87+
- `max_pool_size` (optional): The maximum number of connections
88+
that the pool will open simultaneously. If this is set, operations
89+
will block if there are `max_pool_size` outstanding connections
90+
from the pool. Defaults to 100.
8991
- `network_timeout` (optional): timeout (in seconds) to use
9092
for socket operations - default is no timeout
9193
- `document_class` (optional): default class to use for

pymongo/errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,13 @@ class UnsupportedOption(ConfigurationError):
109109
110110
.. versionadded:: 2.0
111111
"""
112+
113+
114+
class ExceededMaxWaiters(Exception):
115+
"""Raised when a thread tries to get a connection from a pool and
116+
``max_pool_size * waitQueueMultiple`` threads are already waiting.
117+
118+
.. versionadded:: 2.6
119+
"""
120+
pass
121+

pymongo/mongo_replica_set_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,10 @@ def __init__(self, hosts_or_uri=None, max_pool_size=100,
526526
pairs. If a host is an IPv6 literal it must be enclosed in '[' and
527527
']' characters following the RFC2732 URL syntax (e.g. '[::1]' for
528528
localhost)
529-
- `max_pool_size` (optional): The maximum number of idle connections
530-
to keep open in each pool for future use
529+
- `max_pool_size` (optional): The maximum number of connections
530+
each pool will open simultaneously. If this is set, operations
531+
will block if there are `max_pool_size` outstanding connections
532+
from the pool. Defaults to 100.
531533
- `document_class` (optional): default class to use for
532534
documents returned from queries on this client
533535
- `tz_aware` (optional): if ``True``,

pymongo/replica_set_connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ def __init__(self, hosts_or_uri=None, max_pool_size=10,
8080
pairs. If a host is an IPv6 literal it must be enclosed in '[' and
8181
']' characters following the RFC2732 URL syntax (e.g. '[::1]' for
8282
localhost)
83-
- `max_pool_size` (optional): The maximum size limit for
84-
each connection pool.
83+
- `max_pool_size` (optional): The maximum number of connections
84+
each pool will open simultaneously. If this is set, operations
85+
will block if there are `max_pool_size` outstanding connections
86+
from the pool. Defaults to 100.
8587
- `document_class` (optional): default class to use for
8688
documents returned from queries on this connection
8789
- `tz_aware` (optional): if ``True``,

pymongo/thread_util.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
except ImportError:
3636
have_gevent = False
3737

38+
from pymongo.errors import ExceededMaxWaiters
39+
3840

3941
# Do we have to work around http://bugs.python.org/issue1868?
4042
issue1868 = (sys.version_info[:3] <= (2, 7, 0))
@@ -244,10 +246,6 @@ def release(self):
244246
pass
245247

246248

247-
class ExceededMaxWaiters(Exception):
248-
pass
249-
250-
251249
class MaxWaitersBoundedSemaphore(object):
252250
def __init__(self, semaphore_class, value=1, max_waiters=1):
253251
self.waiter_semaphore = semaphore_class(max_waiters)

test/test_pooling_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from pymongo.mongo_client import MongoClient
3232
from pymongo.pool import Pool, NO_REQUEST, NO_SOCKET_YET, SocketInfo
3333
from pymongo.errors import ConfigurationError, ConnectionFailure
34-
from pymongo.thread_util import ExceededMaxWaiters
34+
from pymongo.errors import ExceededMaxWaiters
3535
from test import version, host, port
3636
from test.test_client import get_client
3737
from test.utils import delay, is_mongos, one

0 commit comments

Comments
 (0)