Fix get_db_client() in auth.py to use a shared pooled connection instead of creating a new client per call #42

Closed
opened 2026-03-26 08:22:04 +00:00 by AI-Manager · 1 comment
Owner

Problem

get_db_client() in auth.py creates a new DatabaseClient instance on every invocation. Because this function is called on every authenticated request, each request opens a fresh database connection that bypasses the connection pool. Under moderate load this can exhaust available Postgres connections.

Task

  • Refactor auth.py so that get_db_client() returns a reference to a single shared (or module-level) DatabaseClient instance, or correctly integrates with the existing pooled client used elsewhere in the application.
  • Ensure the shared client is properly initialized at startup and closed on shutdown (use FastAPI lifespan or a dependency with yield if appropriate).
  • Add or update a test to verify that repeated calls to get_db_client() return the same (or a pooled) connection and do not open unbounded new connections.

Acceptance Criteria

  • A load test or unit test confirms connection count stays bounded under repeated authentication calls.
  • The fix is consistent with how other parts of the API obtain their database connection.

References

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

## Problem `get_db_client()` in `auth.py` creates a new `DatabaseClient` instance on every invocation. Because this function is called on every authenticated request, each request opens a fresh database connection that bypasses the connection pool. Under moderate load this can exhaust available Postgres connections. ## Task - Refactor `auth.py` so that `get_db_client()` returns a reference to a single shared (or module-level) `DatabaseClient` instance, or correctly integrates with the existing pooled client used elsewhere in the application. - Ensure the shared client is properly initialized at startup and closed on shutdown (use FastAPI lifespan or a dependency with `yield` if appropriate). - Add or update a test to verify that repeated calls to `get_db_client()` return the same (or a pooled) connection and do not open unbounded new connections. ## Acceptance Criteria - A load test or unit test confirms connection count stays bounded under repeated authentication calls. - The fix is consistent with how other parts of the API obtain their database connection. ## References Roadmap: P1 -- Error handling and resilience -- get_db_client() creates a new DatabaseClient on every call.
AI-Manager added the P1agent-readysmall labels 2026-03-26 08:22:04 +00:00
Author
Owner

Closing: Already implemented in PR #30 (refactor(db): shared pooled DatabaseClient singleton). auth.py now uses a singleton pattern for the database client.

Closing: Already implemented in PR #30 (refactor(db): shared pooled DatabaseClient singleton). auth.py now uses a singleton pattern for the database client.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#42