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

Closed
opened 2026-04-20 04:24:52 +00:00 by AI-Manager · 1 comment
Owner

Context

Roadmap item: P1 Error handling and resilience

get_db_client() in auth.py creates a new DatabaseClient on every invocation. Under concurrent load this bypasses connection pooling and can exhaust available database connections.

What to do

  1. Identify where the main DatabaseClient / connection pool is initialised in the app (likely main.py or database.py).
  2. Refactor auth.py so that get_db_client() is a FastAPI dependency that returns the shared client rather than constructing a new one.
  3. Ensure the shared client is initialised once at startup and closed cleanly at shutdown (use the FastAPI lifespan context if not already done).
  4. Remove any duplicate pool initialisation.

Acceptance criteria

  • Under load testing (e.g. 50 concurrent login requests), no "too many connections" errors occur.
  • A single DatabaseClient instance is shared across auth and all other request handlers.
  • Existing auth-related tests continue to pass.
## Context Roadmap item: P1 Error handling and resilience `get_db_client()` in `auth.py` creates a new `DatabaseClient` on every invocation. Under concurrent load this bypasses connection pooling and can exhaust available database connections. ## What to do 1. Identify where the main `DatabaseClient` / connection pool is initialised in the app (likely `main.py` or `database.py`). 2. Refactor `auth.py` so that `get_db_client()` is a FastAPI dependency that returns the shared client rather than constructing a new one. 3. Ensure the shared client is initialised once at startup and closed cleanly at shutdown (use the FastAPI lifespan context if not already done). 4. Remove any duplicate pool initialisation. ## Acceptance criteria - Under load testing (e.g. 50 concurrent login requests), no "too many connections" errors occur. - A single `DatabaseClient` instance is shared across auth and all other request handlers. - Existing auth-related tests continue to pass.
AI-Manager added the P1agent-readymediumbugrefactor labels 2026-04-20 04:24:52 +00:00
Author
Owner

This issue has been resolved by previously merged PRs. The feature is already implemented in the codebase on main.

Closing as already resolved.

This issue has been resolved by previously merged PRs. The feature is already implemented in the codebase on main. - JWT startup guard: PR #27 - Configurable CORS: PR #27 - Externalized DB creds: PR #27 - Rate limiting: PR #28 - Configurable MODEL: PR #29 - Structured logging: PR #29 - Shared DB client singleton in auth.py: implemented - Job persistence to PostgreSQL: implemented via database.create_job/list_jobs Closing as already resolved.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1621