Commit e57f8d9a authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

[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.
parent 0c973204033a
......@@ -143,33 +143,52 @@ class NullEventBus(object):
pass
class _CnxSetPool:
class _BaseCnxSet:
"""
Simple connection set without any pooling capability.
def __init__(self, source, size):
self._cnxsets = []
You should only uses this if you are using an external pool like pgbouncer.
"""
if size is not None:
self._queue = queue.Queue()
def __init__(self, source):
self._source = source
for i in range(size):
cnxset = source.wrapped_connection()
self._cnxsets.append(cnxset)
self._queue.put_nowait(cnxset)
def qsize(self):
return None
else:
self._queue = None
self._source = source
def get(self):
return self._source.wrapped_connection()
def release(self, cnxset):
cnxset.close(True)
def __iter__(self):
return
yield
def close(self):
pass
def qsize(self):
if self._queue is None:
return None
class _CnxSetPool(_BaseCnxSet):
"""
Database connections pool.
"""
def __init__(self, source, size):
super().__init__(source)
self._cnxsets = []
self._queue = queue.Queue()
for i in range(size):
cnxset = source.wrapped_connection()
self._cnxsets.append(cnxset)
self._queue.put_nowait(cnxset)
def qsize(self):
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 +199,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 +266,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 +315,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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment