Repository: MongoEngine/mongoengine

Issue:
Connection pooling with mongoengine
Hi,
   Kindly assist, if there is any way to configure connection pool in mongo engine maybe in connect function or through some other way.
  In case we are using default connection pool of pymongo, kindly clarify about the same.

  Also, i observed a certain behavior after testing my django app with mongo using mongoengine. With some random number of hits, it opens a new connection and keeps that open. I made about 50 requests and 11 new connections were opened and neither of them was closed. In my first 7 requests, every time a new connection was established. Since every request was sequential, shouldn't it use previous connection object in-spite of creating one.

 A decent clarification will be helpful. This is the first time i am using mongoengine.



Discussion:
Great question. I'm also confused on this.
After googling, there should be a config that restricts connection pool, like in pymongo:

```python
client = MongoClient(host, port, maxPoolSize=200)
```

Redundant connections will result in both heavy DB load and performance degradation.

Resolving patch:
diff --git a/mongoengine/connection.py b/mongoengine/connection.py
--- a/mongoengine/connection.py
+++ b/mongoengine/connection.py
@@ -51,7 +51,9 @@ def register_connection(alias, name=None, host=None, port=None,
         MONGODB-CR (MongoDB Challenge Response protocol) for older servers.
     :param is_mock: explicitly use mongomock for this connection
         (can also be done by using `mongomock://` as db host prefix)
-    :param kwargs: allow ad-hoc parameters to be passed into the pymongo driver
+    :param kwargs: ad-hoc parameters to be passed into the pymongo driver,
+        for example maxpoolsize, tz_aware, etc. See the documentation
+        for pymongo's `MongoClient` for a full list.
 
     .. versionchanged:: 0.10.6 - added mongomock support
     """
@@ -241,9 +243,12 @@ def connect(db=None, alias=DEFAULT_CONNECTION_NAME, **kwargs):
     running on the default port on localhost. If authentication is needed,
     provide username and password arguments as well.
 
-    Multiple databases are supported by using aliases.  Provide a separate
+    Multiple databases are supported by using aliases. Provide a separate
     `alias` to connect to a different instance of :program:`mongod`.
 
+    See the docstring for `register_connection` for more details about all
+    supported kwargs.
+
     .. versionchanged:: 0.6 - added multiple database support.
     """
     if alias not in _connections:


Test patch:
diff --git a/tests/test_connection.py b/tests/test_connection.py
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -285,8 +285,7 @@ def test_register_connection_defaults(self):
         self.assertTrue(isinstance(conn, pymongo.mongo_client.MongoClient))
 
     def test_connection_kwargs(self):
-        """Ensure that connection kwargs get passed to pymongo.
-        """
+        """Ensure that connection kwargs get passed to pymongo."""
         connect('mongoenginetest', alias='t1', tz_aware=True)
         conn = get_connection('t1')
 
@@ -296,6 +295,32 @@ def test_connection_kwargs(self):
         conn = get_connection('t2')
         self.assertFalse(get_tz_awareness(conn))
 
+    def test_connection_pool_via_kwarg(self):
+        """Ensure we can specify a max connection pool size using
+        a connection kwarg.
+        """
+        # Use "max_pool_size" or "maxpoolsize" depending on PyMongo version
+        # (former was changed to the latter as described in
+        # https://jira.mongodb.org/browse/PYTHON-854).
+        # TODO remove once PyMongo < 3.0 support is dropped
+        if pymongo.version_tuple[0] >= 3:
+            pool_size_kwargs = {'maxpoolsize': 100}
+        else:
+            pool_size_kwargs = {'max_pool_size': 100}
+
+        conn = connect('mongoenginetest', alias='max_pool_size_via_kwarg', **pool_size_kwargs)
+        self.assertEqual(conn.max_pool_size, 100)
+
+    def test_connection_pool_via_uri(self):
+        """Ensure we can specify a max connection pool size using
+        an option in a connection URI.
+        """
+        if pymongo.version_tuple[0] == 2 and pymongo.version_tuple[1] < 9:
+            raise SkipTest('maxpoolsize as a URI option is only supported in PyMongo v2.9+')
+
+        conn = connect(host='mongodb://localhost/test?maxpoolsize=100', alias='max_pool_size_via_uri')
+        self.assertEqual(conn.max_pool_size, 100)
+
     def test_write_concern(self):
         """Ensure write concern can be specified in connect() via
         a kwarg or as part of the connection URI.