2b20946ad7
QA review of PR #39 (CAR-1121) identified three blocking issues; this commit addresses all three plus the typecheck errors flagged as CI RED. CAR-1077 (PR #39) changes: - database.py: add pool_timeout=30 so the engine fails fast when the connection pool is exhausted (defends against the "server closed connection unexpectedly" pod failures). - routes/health.py: /health now calls SELECT 1 through Depends(get_db) and raises HTTPException(503) when the database is unreachable, so Kubernetes readiness probes can correctly mark the pod unhealthy and stop routing traffic to it. Logs the failure at exception level for observability. - Drop .mcp.json from this PR (root-level MCP server config, not related to the pool fix; tracked separately). CI typecheck fixes (pre-existing on dev, were failing mypy on PR #39): - auth/passwords.py: cast bcrypt return values so mypy doesn't widen to Any. - config.py: silence the false-positive call-arg on Settings() — the three required fields are populated from the environment by pydantic-settings at runtime. - cache.py: coerce the bytes/str union returned by the redis client to the documented str | None return type. - middleware/rate_limit.py: annotate the three module-level limiters with the RateLimitBackend protocol, cast the redis zrange score to float before arithmetic, and add max_requests/window_seconds to the protocol so the response-header builder can read them. Co-Authored-By: Paperclip <noreply@paperclip.ing>
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
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
|
|
|
|
|
|
def _build_engine_kwargs() -> dict:
|
|
url = settings.database_url
|
|
kwargs: dict = {"echo": False}
|
|
if not url.startswith("sqlite"):
|
|
kwargs.update(
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_timeout=30,
|
|
pool_pre_ping=True,
|
|
pool_recycle=3600,
|
|
)
|
|
return kwargs
|
|
|
|
|
|
engine = create_async_engine(settings.database_url, **_build_engine_kwargs())
|
|
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()
|