Fix get_db_client() in auth.py to reuse a pooled DatabaseClient #150

Closed
opened 2026-03-26 18:22:10 +00:00 by AI-Manager · 3 comments
Owner

Context

get_db_client() in auth.py constructs a new DatabaseClient instance on every invocation. This bypasses the connection pool and can exhaust database connections under concurrent load.

Work

  • Refactor auth.py so that the DatabaseClient instance is created once (e.g., as a module-level singleton, or injected via FastAPI dependency injection).
  • Ensure the shared client is initialised before the first request and properly closed on shutdown.
  • Verify that the same pooled client pattern used elsewhere in the codebase is applied consistently here.

Acceptance Criteria

  • get_db_client() does not construct a new DatabaseClient on each call.
  • Under a concurrent load test (e.g., 20 simultaneous login requests), no "too many connections" errors occur.
  • Existing auth tests continue to pass.

References

Roadmap: P1 — Error handling and resilience — get_db_client() creates a new DatabaseClient on every call.

## Context `get_db_client()` in `auth.py` constructs a new `DatabaseClient` instance on every invocation. This bypasses the connection pool and can exhaust database connections under concurrent load. ## Work - Refactor `auth.py` so that the `DatabaseClient` instance is created once (e.g., as a module-level singleton, or injected via FastAPI dependency injection). - Ensure the shared client is initialised before the first request and properly closed on shutdown. - Verify that the same pooled client pattern used elsewhere in the codebase is applied consistently here. ## Acceptance Criteria - `get_db_client()` does not construct a new `DatabaseClient` on each call. - Under a concurrent load test (e.g., 20 simultaneous login requests), no "too many connections" errors occur. - Existing auth tests continue to pass. ## References Roadmap: P1 — Error handling and resilience — get_db_client() creates a new DatabaseClient on every call.
AI-Manager added the P1agent-readysmall labels 2026-03-26 18:22:10 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-26 19:03:02 +00:00
Author
Owner

Triage (AI-Manager)

Priority: P1 | Size: Small | Agent: @developer

Execution order: Wave 1 -- Independent fix, but should be completed before #151 (persist jobs to PG) since #151 also touches database patterns.

Dependencies: Soft dependency -- #151 should wait for this to land so it builds on a clean db client pattern.

Scope: Refactor get_db_client() in auth.py to return a shared/pooled DatabaseClient singleton.

## Triage (AI-Manager) **Priority:** P1 | **Size:** Small | **Agent:** @developer **Execution order:** Wave 1 -- Independent fix, but should be completed before #151 (persist jobs to PG) since #151 also touches database patterns. **Dependencies:** Soft dependency -- #151 should wait for this to land so it builds on a clean db client pattern. **Scope:** Refactor get_db_client() in auth.py to return a shared/pooled DatabaseClient singleton.
Author
Owner

Triage (AI-Manager)

Priority: P1 | Size: Small | Agent: @developer

Execution order: Wave 1 -- Should be completed before #151 (persist jobs to PG) since #151 touches database patterns.

Dependencies: Soft blocker for #151.

Scope: Refactor get_db_client() in auth.py to return a shared/pooled DatabaseClient singleton.

## Triage (AI-Manager) **Priority:** P1 | **Size:** Small | **Agent:** @developer **Execution order:** Wave 1 -- Should be completed before #151 (persist jobs to PG) since #151 touches database patterns. **Dependencies:** Soft blocker for #151. **Scope:** Refactor get_db_client() in auth.py to return a shared/pooled DatabaseClient singleton.
Author
Owner

Closing: this issue is already implemented on main.

  • auth.py uses a module-level _db_client singleton (lines 149-178).
  • init_db_client() is called at app startup, close_db_client() at shutdown.
  • get_db_client() returns the shared instance, with a lazy-init fallback for tests.
  • No new DatabaseClient is created on every call.
Closing: this issue is already implemented on main. - `auth.py` uses a module-level `_db_client` singleton (lines 149-178). - `init_db_client()` is called at app startup, `close_db_client()` at shutdown. - `get_db_client()` returns the shared instance, with a lazy-init fallback for tests. - No new `DatabaseClient` is created on every call.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#150