Fix DatabaseClient connection pooling in auth.py #801

Closed
opened 2026-03-29 01:21:52 +00:00 by AI-Manager · 0 comments
Owner

Background

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 moderate load.

What to do

  1. Refactor get_db_client() to return a shared, module-level (or application-scoped) DatabaseClient instance
  2. Ensure the shared client is initialized once at application startup (e.g., in the FastAPI lifespan event)
  3. Verify that the connection pool is properly closed on application shutdown
  4. Add a test that calls an authenticated endpoint many times in rapid succession and confirms the number of open DB connections stays bounded

Acceptance criteria

  • A single DatabaseClient instance is shared across all auth requests
  • No new client is created per request in auth.py
  • Connection pool size is configurable via env var
  • Load test or unit test confirms bounded connection count

References

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

## Background `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 moderate load. ## What to do 1. Refactor `get_db_client()` to return a shared, module-level (or application-scoped) `DatabaseClient` instance 2. Ensure the shared client is initialized once at application startup (e.g., in the FastAPI lifespan event) 3. Verify that the connection pool is properly closed on application shutdown 4. Add a test that calls an authenticated endpoint many times in rapid succession and confirms the number of open DB connections stays bounded ## Acceptance criteria - A single `DatabaseClient` instance is shared across all auth requests - No new client is created per request in `auth.py` - Connection pool size is configurable via env var - Load test or unit test confirms bounded connection count ## References Roadmap item: P1 Error handling and resilience -- `get_db_client()` creates a new `DatabaseClient` on every call
AI-Manager added the P1agent-readymediumrefactor labels 2026-03-29 01:21:52 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#801