[PATCH 1 of 9 cubicweb] [server] extract "no pooler" CnxSet class to a _BaseCnxSet class

Philippe Pepiot ph at itsalwaysdns.eu
Tue Mar 31 18:28:56 CEST 2020


# HG changeset patch
# User Philippe Pepiot <ph at itsalwaysdns.eu>
# Date 1585574230 -7200
#      Mon Mar 30 15:17:10 2020 +0200
# Node ID 871eaf34586733ab684e675c4b5edfa47a03253f
# Parent  6eae252361e50b9f9b969bb62a6c4975286d47ca
# Available At https://philpep.org/pub/hg/cubicweb
#              hg pull https://philpep.org/pub/hg/cubicweb -r 871eaf345867
[server] extract "no pooler" CnxSet class to a _BaseCnxSet class

So we get rid of "if self._queue is None" in each method of _CnxSetPool

Also add helper get_cnxset(source, size) to instantiate the correct pooler class.

diff --git a/cubicweb/server/repository.py b/cubicweb/server/repository.py
--- a/cubicweb/server/repository.py
+++ b/cubicweb/server/repository.py
@@ -143,33 +143,44 @@ class NullEventBus(object):
         pass
 
 
-class _CnxSetPool:
+class _BaseCnxSet:
+
+    def __init__(self, source):
+        self._source = source
+
+    def qsize(self):
+        return None
+
+    def get(self):
+        return self._source.wrapped_connection()
+
+    def release(self, cnxset):
+        cnxset.close(True)
+
+    def __iter__(self):
+        return
+        yield
+
+    def close(self):
+        pass
+
+
+class _CnxSetPool(_BaseCnxSet):
 
     def __init__(self, source, size):
+        super().__init__(source)
         self._cnxsets = []
-
-        if size is not None:
-            self._queue = queue.Queue()
+        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
+        for i in range(size):
+            cnxset = source.wrapped_connection()
+            self._cnxsets.append(cnxset)
+            self._queue.put_nowait(cnxset)
 
     def qsize(self):
-        if self._queue is None:
-            return None
-
         return self._queue.qsize()
 
     def get(self):
-        if self._queue is None:
-            return self._source.wrapped_connection()
-
         try:
             return self._queue.get(True, timeout=5)
         except queue.Empty:
@@ -180,25 +191,26 @@ class _CnxSetPool:
                             'connections pool size)')
 
     def release(self, cnxset):
-        if self._queue is None:
-            cnxset.close(True)
-        else:
-            self._queue.put_nowait(cnxset)
+        self._queue.put_nowait(cnxset)
 
     def __iter__(self):
         for cnxset in self._cnxsets:
             yield cnxset
 
     def close(self):
-        # XXX we don't close the connection when there is no queue?
-        if self._queue is not None:
-            while not self._queue.empty():
-                cnxset = self._queue.get_nowait()
+        while not self._queue.empty():
+            cnxset = self._queue.get_nowait()
 
-                try:
-                    cnxset.close(True)
-                except Exception as e:
-                    self.exception('error while closing %s, error: %s' % (cnxset, e))
+            try:
+                cnxset.close(True)
+            except Exception as e:
+                self.exception('error while closing %s, error: %s' % (cnxset, e))
+
+
+def get_cnxset(source, size):
+    if not size:
+        return _BaseCnxSet(source)
+    return _CnxSetPool(source, size)
 
 
 class Repository(object):
@@ -246,7 +258,7 @@ class Repository(object):
             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, min_pool_size)
+        self.cnxsets = get_cnxset(self.system_source, min_pool_size)
         # 1. set used cubes
         if config.creating or not config.read_instance_schema:
             config.bootstrap_cubes()
@@ -295,7 +307,7 @@ class Repository(object):
         # 4. close initialization connection set and reopen fresh ones for
         #    proper initialization
         self.cnxsets.close()
-        self.cnxsets = _CnxSetPool(self.system_source, pool_size)
+        self.cnxsets = get_cnxset(self.system_source, pool_size)
         # 5. call instance level initialisation hooks
         self.hm.call_hooks('server_startup', repo=self)
 



More information about the cubicweb-devel mailing list