Fix: share a single pooled DatabaseClient instead of creating one per request in auth.py #1312

Closed
opened 2026-03-30 11:22:35 +00:00 by AI-Manager · 1 comment
Owner

Background

get_db_client() in auth.py instantiates a new DatabaseClient on every call. This bypasses the connection pool and can exhaust the PostgreSQL max_connections limit under even moderate load.

What to do

  • Refactor get_db_client() to return a module-level (or application-lifespan-scoped) singleton DatabaseClient.
  • Ensure the singleton is initialized once at startup and properly closed on shutdown.
  • If FastAPI dependency injection is used elsewhere to provide a DB client, align auth.py with that pattern.

Acceptance criteria

  • A single DatabaseClient instance is shared across all requests handled by auth.py.
  • Under a load test (e.g., 50 concurrent login requests), PostgreSQL connection count does not grow unboundedly.
  • All existing auth-related tests continue to pass.

References

Roadmap: P1 Error handling and resilience — get_db_client() creates a new DatabaseClient on every call.

## Background `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. This bypasses the connection pool and can exhaust the PostgreSQL `max_connections` limit under even moderate load. ## What to do - Refactor `get_db_client()` to return a module-level (or application-lifespan-scoped) singleton `DatabaseClient`. - Ensure the singleton is initialized once at startup and properly closed on shutdown. - If FastAPI dependency injection is used elsewhere to provide a DB client, align `auth.py` with that pattern. ## Acceptance criteria - A single `DatabaseClient` instance is shared across all requests handled by `auth.py`. - Under a load test (e.g., 50 concurrent login requests), PostgreSQL connection count does not grow unboundedly. - All existing auth-related tests continue to pass. ## References Roadmap: P1 Error handling and resilience — get_db_client() creates a new DatabaseClient on every call.
AI-Manager added the P1agent-readysmallrefactor labels 2026-03-30 11:22:35 +00:00
Author
Owner

Already resolved. SPARC/auth.py uses a module-level singleton _db_client with init_db_client()/close_db_client()/get_db_client(). DatabaseClient in database.py uses psycopg2.pool.ThreadedConnectionPool (min 2, max 10 connections).

Already resolved. `SPARC/auth.py` uses a module-level singleton `_db_client` with `init_db_client()`/`close_db_client()`/`get_db_client()`. `DatabaseClient` in `database.py` uses `psycopg2.pool.ThreadedConnectionPool` (min 2, max 10 connections).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1312