Commit 51b3bf02 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

[server] add docstrings to _CnxSetPool class

parent d4288f96f553
......@@ -202,10 +202,23 @@ class _BaseCnxSet:
class _CnxSetPool(_BaseCnxSet):
Database connections pool.
Dynamic database connections pool.
def __init__(self, source, min_size=1, max_size=4, idle_timeout=300):
Connection pool for database connections. This pool is a dynamic pooler
that opens new connections when the load is to high and closes connections
on lower load after some idle timeout.
* source: database source
* min_size: minimum number of simultanuous connections
* max_size: maximum number of simultanuous connections, if set to 0
there won't be any limit
* idle_timeout: time before the pool starts closing connections
self._cnxsets = []
self._queue = LifoDeque()
......@@ -219,13 +232,18 @@ class _CnxSetPool(_BaseCnxSet):
def _new_cnxset(self):
Create a new connection and returns it. This operation grabs the lock.
cnxset = super()._new_cnxset()
with self.lock:
return cnxset
def _close_idle_cnxset(self):
# close connections not being used since idle_timeout
Close connections not being used since idle_timeout.
if abs(time.time() - self.idle) > self.idle_timeout and self.size() > self.min_size:
cnxset = self._queue.get_oldest(block=False)
......@@ -239,13 +257,30 @@ class _CnxSetPool(_BaseCnxSet):
def size(self):
Return the total number of connections.
with self.lock:
return len(self._cnxsets)
def qsize(self):
Return the size of the queue.
return self._queue.qsize()
def get(self):
Try to get a connection from the queue if available or open a new one
if the maximum number isn't reached.
If no connection is available and the maximum number of connections is
reached, try to get a new one with a timeout. If the timeout is reached
an exception is raised. The maximum number of connections can be
modified to avoid that or the timeout can be increased.
If max_size is set to 0 then there won't be any limit to open new connections.
self.debug(f"[pool] try to get a connection, free queue size is "
f"{self.qsize()}/{self.size()} (max {self.max_size})")
......@@ -273,17 +308,26 @@ class _CnxSetPool(_BaseCnxSet):
return self._new_cnxset()
def release(self, cnxset):
Release a connection by returning it into the queue.
self.debug(f"[pool] release {cnxset} [{id(cnxset)}], free queue size is "
f"{self.qsize()}/{self.size()} (max {self.max_size})")
def __iter__(self):
Iter on all connections. This operation grabs the lock.
with self.lock:
for cnxset in self._cnxsets:
yield cnxset
def close(self):
Close the poll by closing all the connections still in the queue.
while True:
cnxset = self._queue.get_nowait()
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