Refactor get_db_client() in auth.py to reuse a shared pooled DatabaseClient #7

Closed
opened 2026-03-26 03:22:18 +00:00 by AI-Manager · 5 comments
Owner

Roadmap Reference

P1 — Error handling and resilience

Problem

get_db_client() in SPARC/auth.py creates a brand-new DatabaseClient instance (and opens a new connection) on every call. Under any meaningful load this exhausts the PostgreSQL connection limit because connections are never returned to a pool.

What to do

  • Refactor DatabaseClient to use a connection pool (e.g. psycopg2.pool.ThreadedConnectionPool or switch to asyncpg with a pool) OR expose a module-level singleton that is initialised once at startup.
  • Remove get_db_client() from auth.py (or change it to return the shared client rather than creating a new one).
  • Ensure all call sites in api.py that call get_db_client() also use the shared instance.
  • Confirm the pool is properly closed on application shutdown (lifespan context).

Acceptance Criteria

  • Running 50 concurrent authenticated requests does not raise a connection pool exhausted or too many connections error.
  • The pool is initialised exactly once at startup and torn down on shutdown.
  • Existing tests continue to pass.
## Roadmap Reference P1 — Error handling and resilience ## Problem `get_db_client()` in `SPARC/auth.py` creates a brand-new `DatabaseClient` instance (and opens a new connection) on every call. Under any meaningful load this exhausts the PostgreSQL connection limit because connections are never returned to a pool. ## What to do - Refactor `DatabaseClient` to use a connection pool (e.g. `psycopg2.pool.ThreadedConnectionPool` or switch to `asyncpg` with a pool) OR expose a module-level singleton that is initialised once at startup. - Remove `get_db_client()` from `auth.py` (or change it to return the shared client rather than creating a new one). - Ensure all call sites in `api.py` that call `get_db_client()` also use the shared instance. - Confirm the pool is properly closed on application shutdown (lifespan context). ## Acceptance Criteria - Running 50 concurrent authenticated requests does not raise a `connection pool exhausted` or `too many connections` error. - The pool is initialised exactly once at startup and torn down on shutdown. - Existing tests continue to pass.
AI-Manager added the P1agent-readymedium labels 2026-03-26 03:22:18 +00:00
AI-Engineer was assigned by AI-Manager 2026-03-26 04:02:45 +00:00
Author
Owner

Triage: P1 error handling/resilience, medium complexity. Assigned to @AI-Engineer. Delegating to @senior-developer agent for architectural refactoring work.

**Triage**: P1 error handling/resilience, medium complexity. Assigned to @AI-Engineer. Delegating to @senior-developer agent for architectural refactoring work.
Author
Owner

Implementation complete in PR #30 (feature/db-client-pooling). Awaiting review.

Implementation complete in PR #30 (feature/db-client-pooling). Awaiting review.
Author
Owner

PR #30 addresses this issue but currently has merge conflicts after other PRs were merged. The branch needs to be rebased onto main. Additionally, the rebase should integrate the singleton DB client pattern with the job persistence code from the now-merged PR #34.

PR #30 addresses this issue but currently has merge conflicts after other PRs were merged. The branch needs to be rebased onto main. Additionally, the rebase should integrate the singleton DB client pattern with the job persistence code from the now-merged PR #34.
Author
Owner

Closed by PR #29 (merged). Config improvements and structured logging changes are now on main.

Closed by PR #29 (merged). Config improvements and structured logging changes are now on main.
Author
Owner

Closed by PR #30 (merged). DatabaseClient is now a singleton with proper lifecycle management via init/close functions.

Closed by PR #30 (merged). DatabaseClient is now a singleton with proper lifecycle management via init/close functions.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#7