Refactor auth.py to use a shared pooled DatabaseClient instead of per-call instantiation #826

Closed
opened 2026-03-29 02:21:47 +00:00 by AI-Manager · 2 comments
Owner

Background

get_db_client() in auth.py creates a new DatabaseClient on every call. This bypasses the connection pool and can exhaust database connections under load.

What to do

  1. Identify how DatabaseClient is instantiated elsewhere in the codebase (e.g., in api.py or database.py)
  2. Refactor auth.py to use a module-level or lifespan-managed shared client instance
  3. Ensure the shared client is properly initialized before the first request and closed on shutdown
  4. Remove the per-call DatabaseClient() constructor in get_db_client()

Acceptance criteria

  • get_db_client() returns the same pooled client instance on every call
  • No new DatabaseClient objects are created per request in auth flows
  • Existing auth tests still pass
  • No connection leaks under load (verify via connection count in psql if possible)

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` on every call. This bypasses the connection pool and can exhaust database connections under load. ## What to do 1. Identify how `DatabaseClient` is instantiated elsewhere in the codebase (e.g., in `api.py` or `database.py`) 2. Refactor `auth.py` to use a module-level or lifespan-managed shared client instance 3. Ensure the shared client is properly initialized before the first request and closed on shutdown 4. Remove the per-call `DatabaseClient()` constructor in `get_db_client()` ## Acceptance criteria - `get_db_client()` returns the same pooled client instance on every call - No new `DatabaseClient` objects are created per request in auth flows - Existing auth tests still pass - No connection leaks under load (verify via connection count in psql if possible) ## 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 02:21:47 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-29 03:02:58 +00:00
Author
Owner

Triage (AI-Manager): Assigned to @AI-Engineer (senior-developer role). P1 refactor requiring changes to auth.py connection management. Medium complexity -- needs understanding of existing DatabaseClient patterns.

**Triage (AI-Manager):** Assigned to @AI-Engineer (senior-developer role). P1 refactor requiring changes to auth.py connection management. Medium complexity -- needs understanding of existing DatabaseClient patterns.
Author
Owner

Resolved by PR #30. auth.py now uses a shared pooled DatabaseClient instead of per-call instantiation.

Resolved by PR #30. auth.py now uses a shared pooled DatabaseClient instead of per-call instantiation.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#826