[PATCH 04 of 23 cubicweb V2] [database/pool] add a delay between closing each connection when load is low

Laurent Peuch cortex at worlddomination.be
Fri Jan 17 14:37:01 CET 2020


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1576829419 -3600
#      Fri Dec 20 09:10:19 2019 +0100
# Node ID 891f688858cef7cff9c02389b29c2a2539007e00
# Parent  8881f32c71d3732eaffb782e94021d708e94d0c5
# Available At https://hg.logilab.org/users/lpeuch/cubicweb
#              hg pull https://hg.logilab.org/users/lpeuch/cubicweb -r 891f688858ce
[database/pool] add a delay between closing each connection when load is low

The idea is to avoid "blinking" too much and spending all the time opening and
closing connections when load varies too fast.

Closes #17241878

diff --git a/cubicweb/server/repository.py b/cubicweb/server/repository.py
--- a/cubicweb/server/repository.py
+++ b/cubicweb/server/repository.py
@@ -188,7 +188,7 @@ class LifoDeque(queue.LifoQueue):
 class _CnxSetPool:
 
     def __init__(self, source, size, min_size=3, min_timeout=0.1, max_timeout=5,
-                 low_load_delay=30):
+                 low_load_delay=30, close_delay=5):
         self._cnxsets = []
         self.size = size
         self.min_size = min_size
@@ -197,8 +197,10 @@ class _CnxSetPool:
         self.max_timeout = max_timeout
         self.source = source
         self.low_load_delay = low_load_delay
+        self.close_delay = close_delay
 
         self._last_time_we_waited = None
+        self._last_time_connection_close = None
 
         if self.size is not None:
             self._queue = LifoDeque()
@@ -310,6 +312,11 @@ class _CnxSetPool:
            and (time.time() - self._last_time_we_waited) < self.low_load_delay:
             return
 
+        # don't close all the connections too fast
+        if self._last_time_connection_close\
+           and (time.time() - self._last_time_connection_close) < self.close_delay:
+            return
+
         # let's try to close a connection
         try:
             connection = self._queue.get_oldest(block=False)



More information about the cubicweb-devel mailing list