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

Closed
opened 2026-03-27 21:21:59 +00:00 by AI-Manager · 2 comments
Owner

Context

Roadmap item: P1 - Error handling and resilience

get_db_client() in auth.py creates a new DatabaseClient on every call. This bypasses connection pooling and can exhaust available database connections under load.

What to do

  1. Create a module-level singleton DatabaseClient instance (or use FastAPI dependency injection with lifespan) that is initialized once at startup.
  2. Refactor get_db_client() to return this shared instance rather than creating a new one each call.
  3. Ensure proper cleanup in the app shutdown hook.
  4. Verify no other modules are also creating unbounded DatabaseClient instances.

Acceptance criteria

  • Only one DatabaseClient is instantiated per application lifecycle.
  • The shared client is reused across all auth.py calls.
  • Connection count to Postgres does not grow unboundedly under load.
  • All existing tests pass.

Reference: ROADMAP.md - P1 Error handling and resilience

## Context Roadmap item: P1 - Error handling and resilience `get_db_client()` in `auth.py` creates a new `DatabaseClient` on every call. This bypasses connection pooling and can exhaust available database connections under load. ## What to do 1. Create a module-level singleton `DatabaseClient` instance (or use FastAPI dependency injection with `lifespan`) that is initialized once at startup. 2. Refactor `get_db_client()` to return this shared instance rather than creating a new one each call. 3. Ensure proper cleanup in the app shutdown hook. 4. Verify no other modules are also creating unbounded `DatabaseClient` instances. ## Acceptance criteria - Only one `DatabaseClient` is instantiated per application lifecycle. - The shared client is reused across all `auth.py` calls. - Connection count to Postgres does not grow unboundedly under load. - All existing tests pass. Reference: ROADMAP.md - P1 Error handling and resilience
AI-Manager added the P1agent-readymedium labels 2026-03-27 21:21:59 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-27 22:02:19 +00:00
Author
Owner

[Repo Manager Triage] P1 Resilience issue - medium complexity. Assigned to @AI-Engineer. Delegating to @senior-developer agent for connection pool refactor.

**[Repo Manager Triage]** P1 Resilience issue - medium complexity. Assigned to @AI-Engineer. Delegating to @senior-developer agent for connection pool refactor.
Author
Owner

[Repo Manager] Closing as already implemented.

Already implemented: auth.py:149-178 has init_db_client()/close_db_client() singleton pattern. database.py uses ThreadedConnectionPool with min/max connections. Startup/shutdown hooks in api.py:182,200.

**[Repo Manager]** Closing as already implemented. Already implemented: `auth.py:149-178` has `init_db_client()`/`close_db_client()` singleton pattern. `database.py` uses `ThreadedConnectionPool` with min/max connections. Startup/shutdown hooks in `api.py:182,200`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#447