Skip to content

Commit b342990

Browse files
authored
PYTHON-2164 Add MongoClient.options, remove redundant properties (#772)
1 parent 9f6c6a3 commit b342990

21 files changed

+159
-149
lines changed

doc/api/pymongo/client_options.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:mod:`client_options` -- Read only configuration options for a MongoClient.
2+
===========================================================================
3+
4+
.. automodule:: pymongo.client_options
5+
6+
.. autoclass:: pymongo.client_options.ClientOptions()
7+
:members:

doc/api/pymongo/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Sub-modules:
2929

3030
bulk
3131
change_stream
32+
client_options
3233
client_session
3334
collation
3435
collection

doc/api/pymongo/mongo_client.rst

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,19 @@
1414

1515
Raises :class:`~pymongo.errors.InvalidName` if an invalid database name is used.
1616

17-
.. autoattribute:: event_listeners
1817
.. autoattribute:: topology_description
1918
.. autoattribute:: address
2019
.. autoattribute:: primary
2120
.. autoattribute:: secondaries
2221
.. autoattribute:: arbiters
2322
.. autoattribute:: is_primary
2423
.. autoattribute:: is_mongos
25-
.. autoattribute:: max_pool_size
26-
.. autoattribute:: min_pool_size
27-
.. autoattribute:: max_idle_time_ms
2824
.. autoattribute:: nodes
29-
.. autoattribute:: local_threshold_ms
30-
.. autoattribute:: server_selection_timeout
3125
.. autoattribute:: codec_options
3226
.. autoattribute:: read_preference
3327
.. autoattribute:: write_concern
3428
.. autoattribute:: read_concern
29+
.. autoattribute:: options
3530
.. automethod:: start_session
3631
.. automethod:: list_databases
3732
.. automethod:: list_database_names

doc/api/pymongo/pool.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
==============================================================
33

44
.. automodule:: pymongo.pool
5-
:synopsis: Pool module for use with a MongoDB client.
6-
:members:
5+
6+
.. autoclass:: pymongo.pool.PoolOptions()
7+
:members:

doc/changelog.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ Breaking Changes in 4.0
3636
- Removed :attr:`pymongo.mongo_client.MongoClient.max_bson_size`.
3737
- Removed :attr:`pymongo.mongo_client.MongoClient.max_message_size`.
3838
- Removed :attr:`pymongo.mongo_client.MongoClient.max_write_batch_size`.
39+
- Removed :attr:`pymongo.mongo_client.MongoClient.event_listeners`.
40+
- Removed :attr:`pymongo.mongo_client.MongoClient.max_pool_size`.
41+
- Removed :attr:`pymongo.mongo_client.MongoClient.max_idle_time_ms`.
42+
- Removed :attr:`pymongo.mongo_client.MongoClient.local_threshold_ms`.
43+
- Removed :attr:`pymongo.mongo_client.MongoClient.server_selection_timeout`.
44+
- Removed :attr:`pymongo.mongo_client.MongoClient.retry_writes`.
45+
- Removed :attr:`pymongo.mongo_client.MongoClient.retry_reads`.
3946
- Removed :meth:`pymongo.database.Database.eval`,
4047
:data:`pymongo.database.Database.system_js` and
4148
:class:`pymongo.database.SystemJS`.
@@ -180,6 +187,8 @@ Notable improvements
180187
will connect to. More specifically, when a mongodb+srv:// connection string
181188
resolves to more than `srvMaxHosts` number of hosts, the client will randomly
182189
choose a `srvMaxHosts` sized subset of hosts.
190+
- Added :attr:`pymongo.mongo_client.MongoClient.options` for read-only access
191+
to a client's configuration options.
183192

184193
Issues Resolved
185194
...............
@@ -2121,7 +2130,7 @@ Important new features:
21212130
- The ``max_pool_size`` option for :class:`~pymongo.mongo_client.MongoClient`
21222131
and :class:`~pymongo.mongo_replica_set_client.MongoReplicaSetClient` now
21232132
actually caps the number of sockets the pool will open concurrently.
2124-
Once the pool has reached :attr:`~pymongo.mongo_client.MongoClient.max_pool_size`
2133+
Once the pool has reaches max_pool_size
21252134
operations will block waiting for a socket to become available. If
21262135
``waitQueueTimeoutMS`` is set, an operation that blocks waiting for a socket
21272136
will raise :exc:`~pymongo.errors.ConnectionFailure` after the timeout. By

doc/migrate-to-pymongo4.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,37 @@ can be changed to this::
196196

197197
.. _hello command: https://docs.mongodb.com/manual/reference/command/hello/
198198

199+
MongoClient.event_listeners and other configuration option helpers are removed
200+
..............................................................................
201+
202+
The following client configuration option helpers are removed:
203+
- :attr:`pymongo.mongo_client.MongoClient.event_listeners`.
204+
- :attr:`pymongo.mongo_client.MongoClient.max_pool_size`.
205+
- :attr:`pymongo.mongo_client.MongoClient.max_idle_time_ms`.
206+
- :attr:`pymongo.mongo_client.MongoClient.local_threshold_ms`.
207+
- :attr:`pymongo.mongo_client.MongoClient.server_selection_timeout`.
208+
- :attr:`pymongo.mongo_client.MongoClient.retry_writes`.
209+
- :attr:`pymongo.mongo_client.MongoClient.retry_reads`.
210+
211+
These helpers have been replaced by
212+
:attr:`pymongo.mongo_client.MongoClient.options`. Code like this::
213+
214+
client.event_listeners
215+
client.local_threshold_ms
216+
client.server_selection_timeout
217+
client.max_pool_size
218+
client.min_pool_size
219+
client.max_idle_time_ms
220+
221+
can be changed to this::
222+
223+
client.options.event_listeners
224+
client.options.local_threshold_ms
225+
client.options.server_selection_timeout
226+
client.options.pool_options.max_pool_size
227+
client.options.pool_options.min_pool_size
228+
client.options.pool_options.max_idle_time_seconds
229+
199230
``tz_aware`` defaults to ``False``
200231
..................................
201232

pymongo/client_options.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,12 @@ def _parse_pool_options(options):
153153

154154

155155
class ClientOptions(object):
156+
"""Read only configuration options for a MongoClient.
156157
157-
"""ClientOptions"""
158+
Should not be instantiated directly by application developers. Access
159+
a client's options via :attr:`pymongo.mongo_client.MongoClient.options`
160+
instead.
161+
"""
158162

159163
def __init__(self, username, password, database, options):
160164
self.__options = options
@@ -200,7 +204,7 @@ def codec_options(self):
200204
return self.__codec_options
201205

202206
@property
203-
def credentials(self):
207+
def _credentials(self):
204208
"""A :class:`~pymongo.auth.MongoCredentials` instance or None."""
205209
return self.__credentials
206210

@@ -272,3 +276,13 @@ def auto_encryption_opts(self):
272276
def load_balanced(self):
273277
"""True if the client was configured to connect to a load balancer."""
274278
return self.__load_balanced
279+
280+
@property
281+
def event_listeners(self):
282+
"""The event listeners registered for this client.
283+
284+
See :mod:`~pymongo.monitoring` for details.
285+
286+
.. versionadded:: 4.0
287+
"""
288+
return self.__pool_options._event_listeners.event_listeners()

pymongo/encryption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ def __init__(self, client, opts):
261261
self._internal_client = None
262262

263263
def _get_internal_client(encrypter, mongo_client):
264-
if mongo_client.max_pool_size is None:
264+
if mongo_client.options.pool_options.max_pool_size is None:
265265
# Unlimited pool size, use the same client.
266266
return mongo_client
267267
# Else - limited pool size, use an internal client.

pymongo/mongo_client.py

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,14 @@ def __init__(
722722
self.__lock = threading.Lock()
723723
self.__kill_cursors_queue = []
724724

725-
self._event_listeners = options.pool_options.event_listeners
725+
self._event_listeners = options.pool_options._event_listeners
726726
super(MongoClient, self).__init__(options.codec_options,
727727
options.read_preference,
728728
options.write_concern,
729729
options.read_concern)
730730

731731
self.__all_credentials = {}
732-
creds = options.credentials
732+
creds = options._credentials
733733
if creds:
734734
self.__all_credentials[creds.source] = creds
735735

@@ -893,14 +893,6 @@ def watch(self, pipeline=None, full_document=None, resume_after=None,
893893
batch_size, collation, start_at_operation_time, session,
894894
start_after)
895895

896-
@property
897-
def event_listeners(self):
898-
"""The event listeners registered for this client.
899-
900-
See :mod:`~pymongo.monitoring` for details.
901-
"""
902-
return self._event_listeners.event_listeners()
903-
904896
@property
905897
def topology_description(self):
906898
"""The description of the connected MongoDB deployment.
@@ -1005,40 +997,6 @@ def is_mongos(self):
1005997
"""
1006998
return self._server_property('server_type') == SERVER_TYPE.Mongos
1007999

1008-
@property
1009-
def max_pool_size(self):
1010-
"""The maximum allowable number of concurrent connections to each
1011-
connected server. Requests to a server will block if there are
1012-
`maxPoolSize` outstanding connections to the requested server.
1013-
Defaults to 100. Can be either 0 or None, in which case there is no
1014-
limit on the number of concurrent connections.
1015-
1016-
When a server's pool has reached `max_pool_size`, operations for that
1017-
server block waiting for a socket to be returned to the pool. If
1018-
``waitQueueTimeoutMS`` is set, a blocked operation will raise
1019-
:exc:`~pymongo.errors.ConnectionFailure` after a timeout.
1020-
By default ``waitQueueTimeoutMS`` is not set.
1021-
"""
1022-
return self.__options.pool_options.max_pool_size
1023-
1024-
@property
1025-
def min_pool_size(self):
1026-
"""The minimum required number of concurrent connections that the pool
1027-
will maintain to each connected server. Default is 0.
1028-
"""
1029-
return self.__options.pool_options.min_pool_size
1030-
1031-
@property
1032-
def max_idle_time_ms(self):
1033-
"""The maximum number of milliseconds that a connection can remain
1034-
idle in the pool before being removed and replaced. Defaults to
1035-
`None` (no limit).
1036-
"""
1037-
seconds = self.__options.pool_options.max_idle_time_seconds
1038-
if seconds is None:
1039-
return None
1040-
return 1000 * seconds
1041-
10421000
@property
10431001
def nodes(self):
10441002
"""Set of all currently connected servers.
@@ -1054,38 +1012,15 @@ def nodes(self):
10541012
return frozenset(s.address for s in description.known_servers)
10551013

10561014
@property
1057-
def local_threshold_ms(self):
1058-
"""The local threshold for this instance."""
1059-
return self.__options.local_threshold_ms
1060-
1061-
@property
1062-
def server_selection_timeout(self):
1063-
"""The server selection timeout for this instance in seconds."""
1064-
return self.__options.server_selection_timeout
1065-
1066-
@property
1067-
def retry_writes(self):
1068-
"""If this instance should retry supported write operations."""
1069-
return self.__options.retry_writes
1015+
def options(self):
1016+
"""The configuration options for this client.
10701017
1071-
@property
1072-
def retry_reads(self):
1073-
"""If this instance should retry supported write operations."""
1074-
return self.__options.retry_reads
1018+
:Returns:
1019+
An instance of :class:`~pymongo.client_options.ClientOptions`.
10751020
1076-
def _is_writable(self):
1077-
"""Attempt to connect to a writable server, or return False.
1021+
.. versionadded:: 4.0
10781022
"""
1079-
topology = self._get_topology() # Starts monitors if necessary.
1080-
try:
1081-
svr = topology.select_server(writable_server_selector)
1082-
1083-
# When directly connected to a secondary, arbiter, etc.,
1084-
# select_server returns it, whatever the selector. Check
1085-
# again if the server is writable.
1086-
return svr.description.is_writable
1087-
except ConnectionFailure:
1088-
return False
1023+
return self.__options
10891024

10901025
def _end_sessions(self, session_ids):
10911026
"""Send endSessions command(s) with the given session ids."""
@@ -1282,7 +1217,7 @@ def _retry_with_session(self, retryable, func, session, bulk):
12821217
12831218
Re-raises any exception thrown by func().
12841219
"""
1285-
retryable = (retryable and self.retry_writes
1220+
retryable = (retryable and self.options.retry_writes
12861221
and session and not session.in_transaction)
12871222
return self._retry_internal(retryable, func, session, bulk)
12881223

@@ -1353,7 +1288,7 @@ def _retryable_read(self, func, read_pref, session, address=None,
13531288
Re-raises any exception thrown by func().
13541289
"""
13551290
retryable = (retryable and
1356-
self.retry_reads
1291+
self.options.retry_reads
13571292
and not (session and session.in_transaction))
13581293
last_error = None
13591294
retrying = False

pymongo/monitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __init__(
123123
self._server_description = server_description
124124
self._pool = pool
125125
self._settings = topology_settings
126-
self._listeners = self._settings._pool_options.event_listeners
126+
self._listeners = self._settings._pool_options._event_listeners
127127
pub = self._listeners is not None
128128
self._publish = pub and self._listeners.enabled_for_server_heartbeat
129129
self._cancel_context = None

0 commit comments

Comments
 (0)