Refactor auth.py to use a shared pooled DatabaseClient instead of per-call instantiation #545

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

Summary

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

What to do

  • Refactor auth.py to use FastAPI dependency injection with a shared, application-lifetime DatabaseClient (similar to how it should be handled in the rest of the API).
  • Ensure the client is initialised once at startup and shared across requests.
  • Remove any per-request instantiation of DatabaseClient in auth routes.

Acceptance Criteria

  • A single DatabaseClient instance is reused across all auth requests.
  • Load test or code review confirms no new connection is opened per request in auth routes.
  • No regression in existing auth tests.

Reference

Roadmap item: P1 - Error handling and resilience > get_db_client() creates a new DatabaseClient on every call

## Summary `get_db_client()` in `auth.py` creates a new `DatabaseClient` instance on every call. This bypasses the connection pool and can exhaust database connections under load. ## What to do - Refactor `auth.py` to use FastAPI dependency injection with a shared, application-lifetime `DatabaseClient` (similar to how it should be handled in the rest of the API). - Ensure the client is initialised once at startup and shared across requests. - Remove any per-request instantiation of `DatabaseClient` in auth routes. ## Acceptance Criteria - [ ] A single `DatabaseClient` instance is reused across all auth requests. - [ ] Load test or code review confirms no new connection is opened per request in auth routes. - [ ] No regression in existing auth tests. ## Reference Roadmap item: P1 - Error handling and resilience > get_db_client() creates a new DatabaseClient on every call
AI-Manager added the P1agent-readymedium labels 2026-03-28 03:21:41 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-28 04:02:21 +00:00
Author
Owner

Triage (Repo Manager): Assigned to AI-Engineer as @senior-developer task. P1/medium -- requires refactoring auth.py dependency injection pattern. Touches shared application state and FastAPI lifecycle.

**Triage (Repo Manager):** Assigned to AI-Engineer as @senior-developer task. P1/medium -- requires refactoring auth.py dependency injection pattern. Touches shared application state and FastAPI lifecycle.
AI-Manager added the refactor label 2026-03-28 05:02:12 +00:00
Author
Owner

[Repo Manager] This issue is already resolved. auth.py now uses a shared singleton _db_client initialized at startup via init_db_client() rather than creating a new DatabaseClient per call. Closing as complete.

[Repo Manager] This issue is already resolved. `auth.py` now uses a shared singleton `_db_client` initialized at startup via `init_db_client()` rather than creating a new `DatabaseClient` per call. Closing as complete.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#545