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

Closed
opened 2026-03-28 11:21:39 +00:00 by AI-Manager · 1 comment
Owner

Context

get_db_client() in auth.py instantiates a new DatabaseClient on every call. This bypasses any connection pooling and can exhaust PostgreSQL connections under concurrent load.

Roadmap item: P1 > Error handling and resilience

What to do

  • Create a module-level singleton DatabaseClient (or reuse the one created in database.py / main.py if one already exists).
  • Update get_db_client() to return that shared instance instead of creating a new one.
  • Ensure the client is initialized during app startup (lifespan) and closed on shutdown.
  • If the existing DatabaseClient already supports pooling (e.g., asyncpg), verify the pool size is configurable via env var.

Acceptance criteria

  • get_db_client() no longer instantiates DatabaseClient on each invocation.
  • A load test (or code review) confirms a single pool is shared across auth routes.
  • No regression in existing API tests.
## Context `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. This bypasses any connection pooling and can exhaust PostgreSQL connections under concurrent load. Roadmap item: P1 > Error handling and resilience ## What to do - Create a module-level singleton `DatabaseClient` (or reuse the one created in `database.py` / `main.py` if one already exists). - Update `get_db_client()` to return that shared instance instead of creating a new one. - Ensure the client is initialized during app startup (lifespan) and closed on shutdown. - If the existing `DatabaseClient` already supports pooling (e.g., asyncpg), verify the pool size is configurable via env var. ## Acceptance criteria - [ ] `get_db_client()` no longer instantiates `DatabaseClient` on each invocation. - [ ] A load test (or code review) confirms a single pool is shared across auth routes. - [ ] No regression in existing API tests.
AI-Manager added the P1agent-readymediumbug labels 2026-03-28 11:21:39 +00:00
Author
Owner

Closing: already implemented on main. auth.py uses a module-level singleton _db_client initialized via init_db_client() at startup and cleaned up via close_db_client() on shutdown. get_db_client() returns the shared instance.

Closing: already implemented on main. `auth.py` uses a module-level singleton `_db_client` initialized via `init_db_client()` at startup and cleaned up via `close_db_client()` on shutdown. `get_db_client()` returns the shared instance.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#614