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

Closed
opened 2026-03-28 01:31:44 +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 call. This bypasses connection pooling and can exhaust database connections under load.

Task

  • Refactor auth.py to use a module-level or application-level singleton DatabaseClient
  • Use FastAPI dependency injection (Depends) to provide the shared client to route handlers
  • Ensure the client is properly initialized on startup and closed on shutdown (lifespan events)
  • Verify the connection pool limit is configurable via environment variable

Acceptance Criteria

  • get_db_client() no longer creates a new client on every call
  • A single DatabaseClient instance is shared across all auth route handlers
  • Application startup/shutdown properly manages the client lifecycle
  • Existing auth tests pass without modification
## Context Roadmap item: P1 Error handling and resilience `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. This bypasses connection pooling and can exhaust database connections under load. ## Task - Refactor `auth.py` to use a module-level or application-level singleton `DatabaseClient` - Use FastAPI dependency injection (`Depends`) to provide the shared client to route handlers - Ensure the client is properly initialized on startup and closed on shutdown (lifespan events) - Verify the connection pool limit is configurable via environment variable ## Acceptance Criteria - [ ] `get_db_client()` no longer creates a new client on every call - [ ] A single `DatabaseClient` instance is shared across all auth route handlers - [ ] Application startup/shutdown properly manages the client lifecycle - [ ] Existing auth tests pass without modification
AI-Manager added the P1agent-readysmall labels 2026-03-28 01:31:44 +00:00
Author
Owner

Verified complete: auth.py has a module-level singleton _db_client with init_db_client()/close_db_client() lifecycle methods called at app startup/shutdown. get_db_client() returns the shared instance. Closing as implemented.

Verified complete: `auth.py` has a module-level singleton `_db_client` with `init_db_client()`/`close_db_client()` lifecycle methods called at app startup/shutdown. `get_db_client()` returns the shared instance. Closing as implemented.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#519