[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.
--- a/cubicweb/server/repository.py Tue Mar 31 19:15:03 2020 +0200
+++ b/cubicweb/server/repository.py Mon Mar 30 15:17:10 2020 +0200
@@ -143,33 +143,44 @@
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 @@
'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 @@
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 @@
# 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)