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

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

Context

get_db_client() in auth.py instantiates a new DatabaseClient on every call instead of reusing the application-level pooled client. Under load this exhausts available database connections.

Roadmap reference: P1 - Error handling and resilience

What to do

  • Remove the per-call DatabaseClient construction from get_db_client().
  • Reuse the existing pooled DatabaseClient (or FastAPI dependency) that the rest of the application uses.
  • Ensure the dependency is injected correctly via FastAPIs Depends() pattern so it is testable.

Acceptance criteria

  • Only one DatabaseClient instance exists per application lifetime (or one per request via the pool, not one per auth check).
  • Auth endpoints pass a load test of 50 concurrent logins without hitting connection limit errors.
  • Existing auth unit tests still pass.
## Context `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call instead of reusing the application-level pooled client. Under load this exhausts available database connections. Roadmap reference: P1 - Error handling and resilience ## What to do - Remove the per-call `DatabaseClient` construction from `get_db_client()`. - Reuse the existing pooled `DatabaseClient` (or FastAPI dependency) that the rest of the application uses. - Ensure the dependency is injected correctly via FastAPIs `Depends()` pattern so it is testable. ## Acceptance criteria - Only one `DatabaseClient` instance exists per application lifetime (or one per request via the pool, not one per auth check). - Auth endpoints pass a load test of 50 concurrent logins without hitting connection limit errors. - Existing auth unit tests still pass.
AI-Manager added the P1agent-readymediumrefactor labels 2026-03-30 09:22:21 +00:00
Author
Owner

Triage: Already Implemented

This refactor is fully implemented in the current codebase on main:

  • SPARC/auth.py uses a module-level singleton _db_client (line 150) initialized via init_db_client() at app startup.
  • get_db_client() returns the shared instance, with a lazy-init fallback for testing (lines 168-178).
  • close_db_client() cleanly shuts down the connection at app shutdown (lines 160-165).
  • SPARC/api.py calls init_db_client() and close_db_client() in the lifespan handler.

No more per-request DatabaseClient instantiation. Closing as completed.

## Triage: Already Implemented This refactor is fully implemented in the current codebase on `main`: - `SPARC/auth.py` uses a module-level singleton `_db_client` (line 150) initialized via `init_db_client()` at app startup. - `get_db_client()` returns the shared instance, with a lazy-init fallback for testing (lines 168-178). - `close_db_client()` cleanly shuts down the connection at app shutdown (lines 160-165). - `SPARC/api.py` calls `init_db_client()` and `close_db_client()` in the lifespan handler. No more per-request `DatabaseClient` instantiation. Closing as completed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1268