[repository] possibility to disable connections pooling
authorPhilippe Pepiot <philippe.pepiot@logilab.fr>
Fri, 27 Jan 2017 18:09:56 +0100
changeset 11950 68766861debe
parent 11947 3c58ea2fd745
child 11951 330f240435b0
[repository] possibility to disable connections pooling Add a new configuration option 'connections-pooler-enabled' (default yes). If set to no, disable connection pooling and open new connections to the database on demand. This allow to use CubicWeb with other poolers (such as pgbouncer).
cubicweb/server/repository.py
cubicweb/server/serverconfig.py
--- a/cubicweb/server/repository.py	Fri Feb 03 13:37:32 2017 +0100
+++ b/cubicweb/server/repository.py	Fri Jan 27 18:09:56 2017 +0100
@@ -154,17 +154,27 @@
 
     def __init__(self, source, size):
         self._cnxsets = []
-        self._queue = queue.Queue()
-        for i in range(size):
-            cnxset = source.wrapped_connection()
-            self._cnxsets.append(cnxset)
-            self._queue.put_nowait(cnxset)
+        if size is not None:
+            self._queue = queue.Queue()
+            for i in range(size):
+                cnxset = source.wrapped_connection()
+                self._cnxsets.append(cnxset)
+                self._queue.put_nowait(cnxset)
+        else:
+            self._queue = None
+            self._source = source
         super(_CnxSetPool, self).__init__()
 
     def qsize(self):
-        return self._queue.qsize()
+        q = self._queue
+        if q is None:
+            return None
+        return q.qsize()
 
     def get(self):
+        q = self._queue
+        if q is None:
+            return self._source.wrapped_connection()
         try:
             return self._queue.get(True, timeout=5)
         except queue.Empty:
@@ -175,7 +185,11 @@
                             'connections pool size)')
 
     def release(self, cnxset):
-        self._queue.put_nowait(cnxset)
+        q = self._queue
+        if q is None:
+            cnxset.close(True)
+        else:
+            self._queue.put_nowait(cnxset)
 
     def __iter__(self):
         for cnxset in self._cnxsets:
@@ -183,12 +197,13 @@
 
     def close(self):
         q = self._queue
-        while not q.empty():
-            cnxset = q.get_nowait()
-            try:
-                cnxset.close(True)
-            except Exception:
-                self.exception('error while closing %s' % cnxset)
+        if q is not None:
+            while not q.empty():
+                cnxset = q.get_nowait()
+                try:
+                    cnxset.close(True)
+                except Exception:
+                    self.exception('error while closing %s' % cnxset)
 
 
 class Repository(object):
@@ -248,10 +263,13 @@
         # copy pool size here since config.init_cube() and config.load_schema()
         # reload configuration from file and could reset a manually set pool
         # size.
-        pool_size = config['connections-pool-size']
-        # 0. init a cnxset of size 1 that will be used to fetch bootstrap information from
+        if config['connections-pooler-enabled']:
+            pool_size, min_pool_size = config['connections-pool-size'], 1
+        else:
+            pool_size = min_pool_size = None
+        # 0. init a cnxset that will be used to fetch bootstrap information from
         #    the database
-        self.cnxsets = _CnxSetPool(self.system_source, 1)
+        self.cnxsets = _CnxSetPool(self.system_source, min_pool_size)
         # 1. set used cubes
         if config.creating or not config.read_instance_schema:
             config.bootstrap_cubes()
@@ -267,8 +285,8 @@
             # the registry
             config.cube_appobject_path = set(('hooks', 'entities'))
             config.cubicweb_appobject_path = set(('hooks', 'entities'))
-            # limit connections pool to 1
-            pool_size = 1
+            # limit connections pool size
+            pool_size = min_pool_size
         if config.quick_start or config.creating or not config.read_instance_schema:
             # load schema from the file system
             if not config.creating:
--- a/cubicweb/server/serverconfig.py	Fri Feb 03 13:37:32 2017 +0100
+++ b/cubicweb/server/serverconfig.py	Fri Jan 27 18:09:56 2017 +0100
@@ -131,6 +131,11 @@
           'of inactivity. Default to 24h.',
           'group': 'main', 'level': 3,
           }),
+        ('connections-pooler-enabled',
+         {'type': 'yn', 'default': True,
+          'help': 'enable the connection pooler',
+          'group': 'main', 'level': 3,
+          }),
         ('connections-pool-size',
          {'type' : 'int',
           'default': 4,