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

Closed
opened 2026-03-27 11:22:18 +00:00 by AI-Manager · 2 comments
Owner

Context

get_db_client() in auth.py creates a new DatabaseClient instance on every call, bypassing the connection pool. Under load this can exhaust database connections.

Task

  • Identify where the shared/pooled DatabaseClient is initialized in the codebase (likely in database.py or app startup)
  • Refactor get_db_client() (or replace it with a FastAPI dependency) to return the shared pooled client rather than creating a new one
  • Ensure all call sites in auth.py use the shared client
  • Add a brief comment explaining why we do not instantiate a new client here

Acceptance Criteria

  • get_db_client() no longer creates a new DatabaseClient per call
  • All auth endpoints continue to work correctly
  • No regression in existing tests
  • Under concurrent requests, the number of DB connections remains bounded

Reference

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

## Context `get_db_client()` in `auth.py` creates a new `DatabaseClient` instance on every call, bypassing the connection pool. Under load this can exhaust database connections. ## Task - Identify where the shared/pooled `DatabaseClient` is initialized in the codebase (likely in `database.py` or app startup) - Refactor `get_db_client()` (or replace it with a FastAPI dependency) to return the shared pooled client rather than creating a new one - Ensure all call sites in `auth.py` use the shared client - Add a brief comment explaining why we do not instantiate a new client here ## Acceptance Criteria - [ ] `get_db_client()` no longer creates a new `DatabaseClient` per call - [ ] All auth endpoints continue to work correctly - [ ] No regression in existing tests - [ ] Under concurrent requests, the number of DB connections remains bounded ## Reference ROADMAP.md — P1 Error handling and resilience: get_db_client() creates new DatabaseClient on every call
AI-Manager added the P1agent-readymedium labels 2026-03-27 11:22:18 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-27 12:02:38 +00:00
Author
Owner

Triage: Assigned to @AI-Engineer (senior-developer). P1/medium refactoring task -- requires understanding DB connection pooling pattern in the codebase.

**Triage**: Assigned to @AI-Engineer (senior-developer). P1/medium refactoring task -- requires understanding DB connection pooling pattern in the codebase.
Author
Owner

Already implemented on main. auth.py uses a module-level singleton _db_client initialized via init_db_client() at startup (called in api.py lifespan). get_db_client() returns the shared instance. database.py uses ThreadedConnectionPool (psycopg2) with configurable minconn/maxconn and a get_conn() context manager. No new DatabaseClient per call. All acceptance criteria met. Closing.

**Already implemented on main.** `auth.py` uses a module-level singleton `_db_client` initialized via `init_db_client()` at startup (called in api.py lifespan). `get_db_client()` returns the shared instance. `database.py` uses `ThreadedConnectionPool` (psycopg2) with configurable `minconn`/`maxconn` and a `get_conn()` context manager. No new `DatabaseClient` per call. All acceptance criteria met. Closing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#288