fix(db): validate cached project connections
Server CI/CD / docker-image (push) Successful in 23s
Server CI/CD / deploy-fallback-log (push) Has been skipped

This commit is contained in:
2026-07-21 11:26:21 +08:00
parent 045d6c5b49
commit b977bf6725
2 changed files with 64 additions and 9 deletions
+14 -2
View File
@@ -20,6 +20,17 @@ def _close_connection(connection: pg.Connection) -> None:
connection.close()
def _is_healthy(connection: pg.Connection) -> bool:
if _is_closed(connection):
return False
try:
with connection.cursor() as cur:
cur.execute("SELECT 1")
except pg.Error:
return False
return True
def _get_project_lock(name: str) -> RLock:
with _registry_lock:
lock = _project_locks.get(name)
@@ -32,7 +43,7 @@ def _get_project_lock(name: str) -> RLock:
def open_connection(name: str) -> pg.Connection:
with _get_project_lock(name):
connection = g_conn_dict.get(name)
if connection is None or _is_closed(connection):
if connection is None or not _is_healthy(connection):
if connection is not None:
_close_connection(connection)
connection = pg.connect(
@@ -47,8 +58,9 @@ def is_connection_open(name: str) -> bool:
connection = g_conn_dict.get(name)
if connection is None:
return False
if _is_closed(connection):
if not _is_healthy(connection):
del g_conn_dict[name]
_close_connection(connection)
return False
return True