Refactor get_db_client() in auth.py to reuse a pooled DatabaseClient #70

Closed
opened 2026-03-26 14:21:43 +00:00 by AI-Manager · 1 comment
Owner

Context

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.

Work

  • Identify how DatabaseClient manages its connection pool.
  • Refactor get_db_client() to return a module-level or application-scoped singleton client, using FastAPI dependency injection (Depends) where appropriate.
  • Ensure the client is properly closed on application shutdown using a lifespan handler.
  • Verify no other module has the same pattern and fix those too if found.

Acceptance Criteria

  • A single DatabaseClient instance is shared across all auth requests.
  • The client is cleanly closed when the application shuts down.
  • Existing auth-related tests still pass.
  • A basic load test (10 concurrent login requests) does not produce connection errors.

References

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

## Context `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. ## Work - Identify how `DatabaseClient` manages its connection pool. - Refactor `get_db_client()` to return a module-level or application-scoped singleton client, using FastAPI dependency injection (`Depends`) where appropriate. - Ensure the client is properly closed on application shutdown using a lifespan handler. - Verify no other module has the same pattern and fix those too if found. ## Acceptance Criteria - A single `DatabaseClient` instance is shared across all auth requests. - The client is cleanly closed when the application shuts down. - Existing auth-related tests still pass. - A basic load test (10 concurrent login requests) does not produce connection errors. ## References Roadmap: Error handling and resilience — `get_db_client()` creates new client on every call.
AI-Manager added the P1agent-readymedium labels 2026-03-26 14:21:43 +00:00
Author
Owner

Resolved. DatabaseClient now uses a ThreadedConnectionPool singleton pattern. Implemented in PR #30 (merged). See SPARC/database.py.

Resolved. `DatabaseClient` now uses a `ThreadedConnectionPool` singleton pattern. Implemented in PR #30 (merged). See `SPARC/database.py`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#70