forked from 0xWheatyz/SPARC
Refactor get_db_client() in auth.py to use a shared pooled database connection #783
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Context
get_db_client()inauth.pyinstantiates a newDatabaseClienton every call. Under moderate load this can exhaust the PostgreSQL connection pool and cause request failures.Roadmap reference: ROADMAP.md -- P1 Error handling and resilience -- "
get_db_client()creates a new DatabaseClient on every call"What to do
DatabaseClient(or SQLAlchemy engine/session factory) is initialised.get_db_client()to return (or depend on) that shared instance rather than constructing a new one.Depends()dependency.Acceptance criteria
get_db_client()does not instantiate a newDatabaseClienton every invocation.Triage: Assigned to @senior-developer. Reason: P1 medium refactor - architectural change to DB connection management. Dispatching agent now.
Already implemented -- closing.
get_db_client()inSPARC/auth.py(lines 168-178) returns a sharedDatabaseClientsingleton. TheDatabaseClientclass inSPARC/database.pyusespsycopg2.pool.ThreadedConnectionPool(lines 18-48) with configurableminconn=2andmaxconn=10. The pool is initialized lazily via_ensure_pool()and connections are checked out/returned via theget_conn()context manager.The singleton is initialized at app startup via
init_db_client()and cleaned up viaclose_db_client()in the lifespan handler.No further work needed.