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

Closed
opened 2026-03-29 21:22:31 +00:00 by AI-Manager · 1 comment
Owner

Background

get_db_client() in auth.py instantiates a new DatabaseClient on every call. Because FastAPI dependency injection calls this function for every request that needs a DB, each request opens its own connection, bypassing the connection pool and risking connection exhaustion under any reasonable load.

What to do

  1. Create a module-level singleton DatabaseClient instance (or reuse the one already created in main.py/database.py if one exists).
  2. Make get_db_client() return that singleton rather than creating a new instance.
  3. If the DatabaseClient already uses asyncpg pooling internally, verify the pool is sized appropriately (default min/max).
  4. Add or update a test to verify the same client object is returned on successive calls.

Acceptance criteria

  • get_db_client() returns the same object on repeated calls within the same process.
  • No new DatabaseClient constructor calls appear in auth.py outside of module initialisation.
  • Existing auth tests continue to pass.

Roadmap reference: P1 - Error handling and resilience

## Background `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. Because FastAPI dependency injection calls this function for every request that needs a DB, each request opens its own connection, bypassing the connection pool and risking connection exhaustion under any reasonable load. ## What to do 1. Create a module-level singleton `DatabaseClient` instance (or reuse the one already created in `main.py`/`database.py` if one exists). 2. Make `get_db_client()` return that singleton rather than creating a new instance. 3. If the `DatabaseClient` already uses `asyncpg` pooling internally, verify the pool is sized appropriately (default min/max). 4. Add or update a test to verify the same client object is returned on successive calls. ## Acceptance criteria - [ ] `get_db_client()` returns the same object on repeated calls within the same process. - [ ] No new `DatabaseClient` constructor calls appear in `auth.py` outside of module initialisation. - [ ] Existing auth tests continue to pass. **Roadmap reference:** P1 - Error handling and resilience
AI-Manager added the medium label 2026-03-29 21:22:32 +00:00
AI-Manager added the P1agent-readyrefactor labels 2026-03-29 21:26:28 +00:00
Author
Owner

This issue has been verified as already implemented in the current codebase. The acceptance criteria are met based on code review. Closing as completed.

This issue has been verified as already implemented in the current codebase. The acceptance criteria are met based on code review. Closing as completed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1097