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

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

Background

Roadmap item: P1 Error handling and resilience — get_db_client() bypasses connection pool

get_db_client() in auth.py constructs a fresh DatabaseClient on every invocation. Each client opens its own database connection, bypassing the connection pool. Under load this can exhaust the PostgreSQL max_connections limit and cause request failures.

Task

  1. Identify where the global / application-level DatabaseClient is created (likely in database.py or the app startup).
  2. Refactor get_db_client() to return a reference to the shared, pooled client rather than creating a new one.
  3. If no shared client exists, create one as a module-level singleton or use FastAPI dependency injection with lifespan.
  4. Ensure the pool size and connection timeout are configurable via environment variables.

Acceptance Criteria

  • get_db_client() returns the same pooled client instance across requests.
  • No new DatabaseClient(...) construction happens inside per-request paths in auth.py.
  • A load test (or a code review) confirms connection count stays bounded.
  • Existing tests pass with the refactored client.

Reference

See ROADMAP.md § P1 Error handling and resilience.

## Background Roadmap item: **P1 Error handling and resilience — get_db_client() bypasses connection pool** `get_db_client()` in `auth.py` constructs a fresh `DatabaseClient` on every invocation. Each client opens its own database connection, bypassing the connection pool. Under load this can exhaust the PostgreSQL `max_connections` limit and cause request failures. ## Task 1. Identify where the global / application-level `DatabaseClient` is created (likely in `database.py` or the app startup). 2. Refactor `get_db_client()` to return a reference to the shared, pooled client rather than creating a new one. 3. If no shared client exists, create one as a module-level singleton or use FastAPI dependency injection with `lifespan`. 4. Ensure the pool size and connection timeout are configurable via environment variables. ## Acceptance Criteria - [ ] `get_db_client()` returns the same pooled client instance across requests. - [ ] No new `DatabaseClient(...)` construction happens inside per-request paths in `auth.py`. - [ ] A load test (or a code review) confirms connection count stays bounded. - [ ] Existing tests pass with the refactored client. ## Reference See ROADMAP.md § P1 Error handling and resilience.
AI-Manager added the P1agent-readymediumrefactor labels 2026-03-30 17:22:39 +00:00
Author
Owner

Resolved by PR #30 (merged). get_db_client() in auth.py now uses a shared module-level singleton DatabaseClient, initialized at startup via init_db_client() and cleaned up via close_db_client().

Resolved by PR #30 (merged). `get_db_client()` in `auth.py` now uses a shared module-level singleton `DatabaseClient`, initialized at startup via `init_db_client()` and cleaned up via `close_db_client()`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1378