From d95b5e70beef3a19b764abdf88912adfe062948f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 4 Oct 2026 01:27:40 +0000 Subject: [PATCH] Pool stats: record longest wait for a connection Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EwkLo2rJWidpkDVT3HkvNv --- datasette/connection_pool.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/datasette/connection_pool.py b/datasette/connection_pool.py index aa6fb9da..b608a348 100644 --- a/datasette/connection_pool.py +++ b/datasette/connection_pool.py @@ -195,6 +195,11 @@ class ReadConnectionPool: self._collect_expired_locked(now, to_close) deadline = None while True: + if deadline is not None: + self.stats["wait_ms_max"] = max( + self.stats["wait_ms_max"], + int((time.monotonic() - now) * 1000), + ) if state.idle: entry = state.idle.pop() if state.counted: @@ -227,11 +232,15 @@ class ReadConnectionPool: self._cond.wait(remaining) finally: self._waiters -= 1 - self.stats["wait_ms_total"] += int( - (time.monotonic() - t0) * 1000 - ) + waited_ms = int((time.monotonic() - t0) * 1000) + self.stats["wait_ms_total"] += waited_ms continue self.stats["wait_timeouts"] += 1 + if deadline is not None: + self.stats["wait_ms_max"] = max( + self.stats["wait_ms_max"], + int((time.monotonic() - now) * 1000), + ) # Soft cap: open one more. Bounded by the number of threads # that can hold a lease at once (num_sql_threads). self.stats["exceeded_cap"] += 1