import asyncpg from typing import Optional from .config import settings _pool: Optional[asyncpg.Pool] = None async def get_pool() -> asyncpg.Pool: """Lazy-init the asyncpg pool. Reused across requests + background jobs.""" global _pool if _pool is None: if not settings.database_url: raise RuntimeError("DATABASE_URL not configured") _pool = await asyncpg.create_pool( dsn=settings.database_url, min_size=2, max_size=10, command_timeout=30, ) return _pool async def close_pool() -> None: global _pool if _pool is not None: await _pool.close() _pool = None