Merge pull request 'fix(api): document dispose_engine lazy import + regression test (CAR-1135)' (#45) from barcode-betty/fix-car-1135-dispose-engine into dev
This commit was merged in pull request #45.
This commit is contained in:
@@ -40,7 +40,7 @@ class CacheClient:
|
||||
return None
|
||||
if isinstance(value, bytes):
|
||||
return value.decode("utf-8", errors="replace")
|
||||
return value
|
||||
return str(value)
|
||||
|
||||
async def set(self, key: str, value: str, ttl_seconds: int = 300) -> None:
|
||||
if not self._client:
|
||||
|
||||
@@ -25,6 +25,12 @@ from cartsnitch_api.routes.user import router as user_router
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Lazy import: keep `dispose_engine` out of the top-level imports so a
|
||||
# stale or partially-built database.py never breaks module load on
|
||||
# container start. The function is required for graceful pool cleanup
|
||||
# on shutdown; if the import fails, the cache_client.close() that
|
||||
# follows the yield would mask it. See CAR-1135 for the original
|
||||
# ImportError that motivated this pattern.
|
||||
from cartsnitch_api.database import dispose_engine
|
||||
|
||||
await cache_client.initialize()
|
||||
|
||||
@@ -147,8 +147,8 @@ def _get_client_ip(request: Request) -> str:
|
||||
"""Extract client IP, respecting X-Forwarded-For behind a reverse proxy."""
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
if forwarded:
|
||||
return forwarded.split(",")[0].strip()
|
||||
return request.client.host if request.client else "unknown"
|
||||
return str(forwarded.split(",")[0].strip())
|
||||
return str(request.client.host) if request.client else "unknown"
|
||||
|
||||
|
||||
def _get_rate_limit_key(request: Request) -> tuple[str, RateLimitBackend]:
|
||||
|
||||
Reference in New Issue
Block a user