Finish external HTTP client resilience: add shared aiohttp config, retry support, and update task status

This commit is contained in:
2026-04-09 22:01:11 +02:00
parent e1d741956e
commit 148756fb79
6 changed files with 185 additions and 21 deletions

View File

@@ -33,6 +33,22 @@ async def _ensure_database_schema(database_path: str) -> None:
await db.close()
def _create_http_session(settings: Settings) -> aiohttp.ClientSession:
"""Build a shared aiohttp session with reasonable global limits and timeouts."""
timeout = aiohttp.ClientTimeout(
total=settings.http_request_timeout_seconds,
connect=settings.http_connect_timeout_seconds,
sock_read=settings.http_request_timeout_seconds,
)
connector = aiohttp.TCPConnector(
limit=settings.http_max_connections,
limit_per_host=settings.http_max_connections,
keepalive_timeout=settings.http_keepalive_timeout_seconds,
enable_cleanup_closed=True,
)
return aiohttp.ClientSession(timeout=timeout, connector=connector)
async def startup_shared_resources(
app: FastAPI,
settings: Settings,
@@ -82,7 +98,7 @@ async def startup_shared_resources(
if unresolved_count > 0:
log.warning("geo_cache_unresolved_ips", unresolved=unresolved_count)
http_session: aiohttp.ClientSession = aiohttp.ClientSession()
http_session: aiohttp.ClientSession = _create_http_session(settings)
geo_service.init_geoip(settings.geoip_db_path)
scheduler: AsyncIOScheduler = AsyncIOScheduler(timezone="UTC")