Refactor get_db_client() in auth.py to use a shared connection pool #1403

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

Context

Roadmap item: P1 -- Error handling and resilience

get_db_client() in auth.py instantiates a new DatabaseClient on every invocation. Because FastAPI dependency injection calls this function per-request, each request opens its own connection, bypassing the connection pool and potentially exhausting the PostgreSQL max_connections limit under load.

What to do

  • Create a single module-level (or app-lifespan-scoped) DatabaseClient instance.
  • Convert get_db_client() to a FastAPI dependency that yields the shared client.
  • Ensure the client is properly closed on application shutdown via the lifespan context manager.
  • Verify no other module instantiates DatabaseClient outside of the shared instance.

Acceptance criteria

  • Only one DatabaseClient instance is created per application process.
  • Load testing with 20+ concurrent requests does not exhaust the DB connection pool.
  • Existing auth-related tests pass.
## Context Roadmap item: P1 -- Error handling and resilience `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every invocation. Because FastAPI dependency injection calls this function per-request, each request opens its own connection, bypassing the connection pool and potentially exhausting the PostgreSQL `max_connections` limit under load. ## What to do - Create a single module-level (or app-lifespan-scoped) `DatabaseClient` instance. - Convert `get_db_client()` to a FastAPI dependency that yields the shared client. - Ensure the client is properly closed on application shutdown via the `lifespan` context manager. - Verify no other module instantiates `DatabaseClient` outside of the shared instance. ## Acceptance criteria - [ ] Only one `DatabaseClient` instance is created per application process. - [ ] Load testing with 20+ concurrent requests does not exhaust the DB connection pool. - [ ] Existing auth-related tests pass.
AI-Manager added the P1agent-readymediumrefactor labels 2026-03-30 18:22:08 +00:00
Author
Owner

Triage: Already resolved in main.

DatabaseClient in SPARC/database.py already uses psycopg2.pool.ThreadedConnectionPool (min=2, max=10) with a get_conn() context manager. get_db_client() in SPARC/auth.py uses a module-level singleton initialized at startup via init_db_client(). No per-request connection creation remains. Closing as complete.

**Triage: Already resolved in main.** `DatabaseClient` in `SPARC/database.py` already uses `psycopg2.pool.ThreadedConnectionPool` (min=2, max=10) with a `get_conn()` context manager. `get_db_client()` in `SPARC/auth.py` uses a module-level singleton initialized at startup via `init_db_client()`. No per-request connection creation remains. Closing as complete.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1403