Browse Source

Adding configuration variable to set max_pool_size for MongoDB connection

Craig Younkins 12 năm trước cách đây
mục cha
commit
52a11bdef7
2 tập tin đã thay đổi với 12 bổ sung1 xóa
  1. 5 1
      celery/backends/mongodb.py
  2. 7 0
      docs/configuration.rst

+ 5 - 1
celery/backends/mongodb.py

@@ -45,6 +45,7 @@ class MongoBackend(BaseDictBackend):
     mongodb_password = None
     mongodb_database = 'celery'
     mongodb_taskmeta_collection = 'celery_taskmeta'
+    mongodb_max_pool_size = 10
 
     def __init__(self, *args, **kwargs):
         """Initialize MongoDB backend instance.
@@ -77,6 +78,8 @@ class MongoBackend(BaseDictBackend):
                     'database', self.mongodb_database)
             self.mongodb_taskmeta_collection = config.get(
                 'taskmeta_collection', self.mongodb_taskmeta_collection)
+            self.mongodb_max_pool_size = config.get(
+                'max_pool_size', self.mongodb_max_pool_size)
 
         self._connection = None
 
@@ -92,11 +95,12 @@ class MongoBackend(BaseDictBackend):
             # This enables the use of replica sets and sharding.
             # See pymongo.Connection() for more info.
             args = [self.mongodb_host]
+            kwargs = {'max_pool_size': self.mongodb_max_pool_size}
             if isinstance(self.mongodb_host, basestring) \
                     and not self.mongodb_host.startswith('mongodb://'):
                 args.append(self.mongodb_port)
 
-            self._connection = Connection(*args)
+            self._connection = Connection(*args, **kwargs)
 
         return self._connection
 

+ 7 - 0
docs/configuration.rst

@@ -488,6 +488,13 @@ This is a dict supporting the following keys:
     The collection name to store task meta data.
     Defaults to "celery_taskmeta".
 
+* max_pool_size
+    Passed as max_pool_size to PyMongo's Connection or MongoClient 
+    constructor. It is the maximum number of TCP connections to keep
+    open to MongoDB at a given time. If there are more open connections
+    than max_pool_size, sockets will be closed when they are released.
+    Defaults to 10.
+
 .. _example-mongodb-result-config:
 
 Example configuration