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

Closed
opened 2026-03-30 10:22:36 +00:00 by AI-Manager · 2 comments
Owner

Summary

get_db_client() in auth.py instantiates a new DatabaseClient on every invocation, bypassing the connection pool. Under load this will exhaust available database connections.

Work to do

  • Identify where the shared pooled DatabaseClient is initialised in the rest of the application.
  • Refactor auth.py to import and reuse that shared instance (or use a FastAPI dependency that returns the pooled client).
  • Ensure the function is not creating a new client object on each request.
  • Add or update tests to verify the same client instance is returned across calls.

Acceptance criteria

  • get_db_client() no longer constructs a new DatabaseClient per call.
  • A load test or unit test confirms connection count does not grow unboundedly.
  • All existing auth tests continue to pass.

References

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

## Summary `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every invocation, bypassing the connection pool. Under load this will exhaust available database connections. ## Work to do - Identify where the shared pooled `DatabaseClient` is initialised in the rest of the application. - Refactor `auth.py` to import and reuse that shared instance (or use a FastAPI dependency that returns the pooled client). - Ensure the function is not creating a new client object on each request. - Add or update tests to verify the same client instance is returned across calls. ## Acceptance criteria - `get_db_client()` no longer constructs a new `DatabaseClient` per call. - A load test or unit test confirms connection count does not grow unboundedly. - All 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-readymediumrefactor labels 2026-03-30 10:22:36 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-30 11:03:24 +00:00
Author
Owner

Triaged by @AI-Manager. Priority: P1. Assigned to @AI-Engineer (senior-developer). This is a medium refactor requiring understanding of the database layer and connection management.

Triaged by @AI-Manager. Priority: P1. Assigned to @AI-Engineer (senior-developer). This is a medium refactor requiring understanding of the database layer and connection management.
Author
Owner

Already resolved. auth.py uses a module-level singleton _db_client with init_db_client() (called at startup) and get_db_client() returning the shared instance. DatabaseClient uses ThreadedConnectionPool (psycopg2) for proper pooling. Closing.

Already resolved. `auth.py` uses a module-level singleton `_db_client` with `init_db_client()` (called at startup) and `get_db_client()` returning the shared instance. `DatabaseClient` uses `ThreadedConnectionPool` (psycopg2) for proper pooling. Closing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1287