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

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

Context

get_db_client() in auth.py instantiates a new DatabaseClient on every call. Under concurrent load this bypasses the connection pool and can exhaust available database connections, causing 500 errors.

Roadmap reference: ROADMAP.md > P1 > Error handling and resilience > get_db_client() creates a new DatabaseClient on every call

What to do

  1. Create a single application-level DatabaseClient instance (e.g., as a FastAPI dependency or module-level singleton).
  2. Update auth.py and any other callers that instantiate their own DatabaseClient to use the shared instance.
  3. Ensure the shared client is properly initialized at startup and closed on shutdown using FastAPI lifespan events.

Acceptance criteria

  • A single DatabaseClient instance is shared across all auth requests.
  • No new DatabaseClient objects are created per-request in auth.py.
  • A load test (or code inspection) confirms the connection pool is respected.
## Context `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. Under concurrent load this bypasses the connection pool and can exhaust available database connections, causing 500 errors. Roadmap reference: ROADMAP.md > P1 > Error handling and resilience > get_db_client() creates a new DatabaseClient on every call ## What to do 1. Create a single application-level `DatabaseClient` instance (e.g., as a FastAPI dependency or module-level singleton). 2. Update `auth.py` and any other callers that instantiate their own `DatabaseClient` to use the shared instance. 3. Ensure the shared client is properly initialized at startup and closed on shutdown using FastAPI lifespan events. ## Acceptance criteria - [ ] A single `DatabaseClient` instance is shared across all auth requests. - [ ] No new `DatabaseClient` objects are created per-request in `auth.py`. - [ ] A load test (or code inspection) confirms the connection pool is respected.
AI-Manager added the P1agent-readymediumbug-fix labels 2026-03-30 04:22:20 +00:00
Author
Owner

This issue has been resolved on main. SPARC/auth.py (lines 149-178) implements a module-level singleton DatabaseClient initialized via init_db_client() at startup and accessed through get_db_client(), replacing per-call instantiation. Closing as complete.

This issue has been resolved on main. `SPARC/auth.py` (lines 149-178) implements a module-level singleton `DatabaseClient` initialized via `init_db_client()` at startup and accessed through `get_db_client()`, replacing per-call instantiation. 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#1196