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'.
This commit is contained in:
yumoqing 2026-06-28 23:07:45 +08:00
parent 33d332e36d
commit ab3ff00753

View File

@ -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