3300d67137
- Add connection pool config to SQLAlchemy async engine (pool_size=10, max_overflow=20, pool_pre_ping, pool_recycle) - Implement Redis connection pool in CacheClient with initialize/close lifecycle - Wire lifespan startup/shutdown to initialize and dispose pools - Add dispose_engine() for graceful DB pool cleanup on shutdown Closes CAR-550 Co-Authored-By: Paperclip <noreply@paperclip.ing>
29 lines
822 B
Python
29 lines
822 B
Python
"""Database session management for the API gateway."""
|
|
|
|
from collections.abc import AsyncGenerator
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
from cartsnitch_api.config import settings
|
|
|
|
engine = create_async_engine(
|
|
settings.database_url,
|
|
echo=False,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_pre_ping=True,
|
|
pool_recycle=3600,
|
|
)
|
|
async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
|
"""FastAPI dependency that yields an async DB session."""
|
|
async with async_session_factory() as session:
|
|
yield session
|
|
|
|
|
|
async def dispose_engine() -> None:
|
|
"""Dispose the database engine, closing all pooled connections."""
|
|
await engine.dispose()
|