diff --git a/pymongo/common.py b/pymongo/common.py index a62114ad1..bf5919d84 100644 --- a/pymongo/common.py +++ b/pymongo/common.py @@ -336,7 +336,8 @@ VALIDATORS = { 'gssapiservicename': validate_basestring, 'authmechanismproperties': validate_auth_mechanism_properties, 'uuidrepresentation': validate_uuid_representation, - 'socketkeepalive': validate_boolean + 'socketkeepalive': validate_boolean, + 'maxpoolsize': validate_positive_integer_or_none } diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 439757a41..41b3a8004 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -140,6 +140,8 @@ class MongoClient(common.BaseObject): | **Other optional parameters can be passed as keyword arguments:** + - `maxPoolSize` (optional): Alias for max_pool_size. Takes + precedence over max_pool_size. - `socketTimeoutMS`: (integer or None) How long (in milliseconds) a send or receive on a socket can take before timing out. Defaults to ``None`` (no timeout). @@ -312,8 +314,10 @@ class MongoClient(common.BaseObject): options['codec_options'] = CodecOptions( document_class, tz_aware, uuid_representation) - self.__max_pool_size = common.validate_positive_integer_or_none( - 'max_pool_size', max_pool_size) + self.__max_pool_size = options.get( + 'maxpoolsize', + common.validate_positive_integer_or_none('max_pool_size', + max_pool_size)) self.__cursor_manager = CursorManager(self) diff --git a/pymongo/mongo_replica_set_client.py b/pymongo/mongo_replica_set_client.py index e09cf850e..32be49edd 100644 --- a/pymongo/mongo_replica_set_client.py +++ b/pymongo/mongo_replica_set_client.py @@ -490,6 +490,8 @@ class MongoReplicaSetClient(common.BaseObject): precedence. - `port`: For compatibility with :class:`~mongo_client.MongoClient`. The default port number to use for hosts. + - `maxpoolsize` (optional): Alias for max_pool_size. Takes + precendence over max_pool_size. - `socketTimeoutMS`: (integer or None) How long (in milliseconds) a send or receive on a socket can take before timing out. Defaults to ``None`` (no timeout). @@ -595,8 +597,6 @@ class MongoReplicaSetClient(common.BaseObject): self.__index_cache = {} self.__auth_credentials = {} - self.__max_pool_size = common.validate_positive_integer_or_none( - 'max_pool_size', max_pool_size) self.__monitor = None self.__closed = False @@ -633,6 +633,11 @@ class MongoReplicaSetClient(common.BaseObject): self.__opts[option] = value self.__opts.update(options) + self.__max_pool_size = self.__opts.get( + 'maxpoolsize', + common.validate_positive_integer_or_none('max_pool_size', + max_pool_size)) + common.validate_boolean('tz_aware', tz_aware) uuid_representation = options.pop('uuidrepresentation', PYTHON_LEGACY) self.__opts['codec_options'] = CodecOptions( diff --git a/test/test_client.py b/test/test_client.py index 6a2548f11..2f4f2fe2a 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -331,6 +331,31 @@ class TestClient(unittest.TestCase, TestRequestMixin): finally: ctx.exit() + def test_backport_maxpoolsize_uri(self): + uri = "mongodb://%s:%s" % (host, port) + mps_uri = ("mongodb://%s:%d/?maxPoolSize=10" % (host, port)) + + client = MongoClient(uri) + self.assertEqual(client.max_pool_size, 100) + + client = MongoClient(uri, maxPoolSize=10) + self.assertEqual(client.max_pool_size, 10) + + client = MongoClient(uri, max_pool_size=8, maxPoolSize=10) + self.assertEqual(client.max_pool_size, 10) + + client = MongoClient(mps_uri) + self.assertEqual(client.max_pool_size, 10) + + client = MongoClient(mps_uri, maxPoolSize=8) + self.assertEqual(client.max_pool_size, 10) + + client = MongoClient(mps_uri, max_pool_size=8) + self.assertEqual(client.max_pool_size, 10) + + client = MongoClient(mps_uri, max_pool_size=6, maxPoolSize=8) + self.assertEqual(client.max_pool_size, 10) + def test_backport_localthresholdms_uri(self): uri = "mongodb://%s:%s" % (host, port) lt_uri = "mongodb://%s:%d/?localThresholdMS=10" % (host, port) diff --git a/test/test_pooling_base.py b/test/test_pooling_base.py index d47f01134..f84c37409 100644 --- a/test/test_pooling_base.py +++ b/test/test_pooling_base.py @@ -402,6 +402,20 @@ class _TestPooling(_TestPoolingBase): c = MongoClient(host=host, port=port, max_pool_size=100) self.assertEqual(c.max_pool_size, 100) + def test_maxpoolsize_validation(self): + self.assertRaises( + ConfigurationError, MongoClient, host=host, port=port, + maxpoolsize=-1 + ) + + self.assertRaises( + ConfigurationError, MongoClient, host=host, port=port, + maxpoolsize='foo' + ) + + c = MongoClient(host=host, port=port, maxpoolsize=100) + self.assertEqual(c.max_pool_size, 100) + def test_no_disconnect(self): run_cases(self, [NoRequest, NonUnique, Unique, SaveAndFind]) diff --git a/test/test_replica_set_client.py b/test/test_replica_set_client.py index 02e1017fb..cb63fd78e 100644 --- a/test/test_replica_set_client.py +++ b/test/test_replica_set_client.py @@ -692,6 +692,12 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin): self.assertEqual(pool.wait_queue_multiple, 2) self.assertEqual(pool._socket_semaphore.waiter_semaphore.counter, 6) + def test_waitQueueMultiple_backport_maxpoolsize(self): + client = self._get_client(maxpoolsize=3, waitQueueMultiple=2) + pool = get_pool(client) + self.assertEqual(pool.wait_queue_multiple, 2) + self.assertEqual(pool._socket_semaphore.waiter_semaphore.counter, 6) + def test_socketKeepAlive(self): client = self._get_client(socketKeepAlive=True) pool = get_pool(client) @@ -743,6 +749,32 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin): self.assertTrue("pymongo_test_bernie" in dbs) client.close() + def test_backport_repl_maxpoolsize_uri(self): + uri = "mongodb://%s/?replicaSet=%s" % (pair, self.name) + mps_uri = ("mongodb://%s/?replicaSet=%s;maxPoolSize=10" + % (pair, self.name)) + + client = MongoReplicaSetClient(uri) + self.assertEqual(client.max_pool_size, 100) + + client = MongoReplicaSetClient(uri, maxPoolSize=10) + self.assertEqual(client.max_pool_size, 10) + + client = MongoReplicaSetClient(uri, max_pool_size=8, maxPoolSize=10) + self.assertEqual(client.max_pool_size, 10) + + client = MongoReplicaSetClient(mps_uri) + self.assertEqual(client.max_pool_size, 10) + + client = MongoReplicaSetClient(mps_uri, max_pool_size=8) + self.assertEqual(client.max_pool_size, 10) + + client = MongoReplicaSetClient(mps_uri, maxPoolSize=8) + self.assertEqual(client.max_pool_size, 10) + + client = MongoReplicaSetClient(mps_uri, max_pool_size=6, maxPoolSize=8) + self.assertEqual(client.max_pool_size, 10) + def test_backport_repl_localthresholdms_uri(self): uri = ("mongodb://%s/?replicaSet=%s" % (pair, self.name)) lt_uri = ("mongodb://%s/?replicaSet=%s;localThresholdMS=10" diff --git a/test/test_uri_parser.py b/test/test_uri_parser.py index 499688e22..163ff1264 100644 --- a/test/test_uri_parser.py +++ b/test/test_uri_parser.py @@ -136,8 +136,7 @@ class TestURI(unittest.TestCase): self.assertRaises(ConfigurationError, split_options, 'authMechanism=foo') self.assertEqual({'authsource': 'foobar'}, split_options('authSource=foobar')) - # maxPoolSize isn't yet a documented URI option. - self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=50') + self.assertEqual({'maxpoolsize': 50}, split_options('maxpoolsize=50')) self.assertEqual({'localthresholdms': 300}, split_options('localThresholdMS=300'))