Refactor get_db_client() in auth.py to use a shared pooled connection #943

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

Background

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

Task

  1. Identify where the application-level DatabaseClient (or connection pool) is initialised.
  2. Refactor get_db_client() to return the shared instance rather than creating a new one.
  3. Ensure the pooled client is properly closed on application shutdown.

Acceptance Criteria

  • get_db_client() returns the same pooled client instance across requests.
  • No new DatabaseClient is instantiated per-request in auth flows.
  • Existing auth tests pass; add a test or log assertion confirming single-instance behaviour if feasible.

Reference

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

## Background `get_db_client()` in `auth.py` instantiates a new `DatabaseClient` on every call. This bypasses the connection pool and can exhaust database connections under load. ## Task 1. Identify where the application-level `DatabaseClient` (or connection pool) is initialised. 2. Refactor `get_db_client()` to return the shared instance rather than creating a new one. 3. Ensure the pooled client is properly closed on application shutdown. ## Acceptance Criteria - [ ] `get_db_client()` returns the same pooled client instance across requests. - [ ] No new `DatabaseClient` is instantiated per-request in auth flows. - [ ] Existing auth tests pass; add a test or log assertion confirming single-instance behaviour if feasible. ## Reference Roadmap: 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 09:21:58 +00:00
Author
Owner

Triaged by repo manager. This issue has already been resolved in the current codebase. auth.py implements a module-level singleton pattern: _db_client (line 150), init_db_client() (line 153), close_db_client() (line 160), and get_db_client() (line 168) returns the shared instance. api.py calls init_db_client() at startup and close_db_client() at shutdown. Closing as already implemented.

Triaged by repo manager. This issue has already been resolved in the current codebase. `auth.py` implements a module-level singleton pattern: `_db_client` (line 150), `init_db_client()` (line 153), `close_db_client()` (line 160), and `get_db_client()` (line 168) returns the shared instance. `api.py` calls `init_db_client()` at startup and `close_db_client()` at shutdown. Closing as already implemented.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#943