From ab3ff0075323c933cd26f5d21bb91cab360a88ff Mon Sep 17 00:00:00 2001 From: yumoqing Date: Sun, 28 Jun 2026 23:07:45 +0800 Subject: [PATCH] perf: skip check_alive for recently used connections (< 30s idle) Add CHECK_ALIVE_THRESHOLD to avoid unnecessary DB round-trips when reusing warm connections. Fixes connection storm under high concurrency where 57% of logs were 'discarding dead connection'. --- sqlor/dbpools.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sqlor/dbpools.py b/sqlor/dbpools.py index c89a68a..4bfd210 100644 --- a/sqlor/dbpools.py +++ b/sqlor/dbpools.py @@ -53,6 +53,8 @@ def sqlorFactory(dbdesc): class SqlorPool: # Idle timeout: connections unused for this long will be closed IDLE_TIMEOUT = 300 # 5 minutes + # Only check alive if connection has been idle longer than this + CHECK_ALIVE_THRESHOLD = 30 # 30 seconds def __init__(self, create_func, maxconn=100): self.sema = asyncio.Semaphore(maxconn) @@ -123,7 +125,13 @@ class SqlorPool: yielded_sqlor = None # Try to find a healthy idle connection sqlors = [s for s in self.sqlors if not s.used] + now = time.time() for s in sqlors: + # Skip check_alive if connection was used recently + idle_time = now - s.use_at + if idle_time < self.CHECK_ALIVE_THRESHOLD: + yielded_sqlor = s + break ok = await self._check_alive(s.sqlor) if ok: yielded_sqlor = s