P0 fixes: - CORS: replace wildcard methods/headers with configurable lists - deps.py: remove unsafe global _warned_unset variable P1 fixes: - http_client: read default timeout from Settings - bzzoiro: replace sync urllib with async httpx - bzzoiro: normalize validation failures use warning level only - db pool: read pool config from Settings (default 5+10) - backtest: add asyncio.Semaphore(8) for concurrent execution - predict/context_builder: add backtest parameter for cutoff buffer P2 improvements: - injuries: enforce int conversion for player_id/fixture_id - injuries: use system temp dir for cache - utils.py: extract shared actual_1x2/is_correct_1x2 - validation: downgrade 1x2 mismatch log to debug - docker-compose: use env vars for all credentials - .env.example: add POSTGRES_USER/PASSWORD/PORT, API_PORT
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
"""共享 httpx 异步客户端(连接池复用 + 生命周期管理)。
|
|
|
|
使用方:
|
|
- src/llm/provider.py: LLM 调用
|
|
- src/data/bzzoiro.py: bzzoiro 比赛数据
|
|
- src/data/understat.py: xG 抓取
|
|
- src/data/injuries.py: 伤停抓取
|
|
|
|
生命周期由 FastAPI lifespan 管理(关闭时 aclose)。
|
|
调用方可通过 `timeout` 参数覆盖 per-request 超时。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import httpx
|
|
|
|
from src.core.config import settings
|
|
|
|
_shared_client: httpx.AsyncClient | None = None
|
|
|
|
|
|
def get_client() -> httpx.AsyncClient:
|
|
"""获取共享客户端(懒初始化)。"""
|
|
global _shared_client
|
|
if _shared_client is None or _shared_client.is_closed:
|
|
_shared_client = httpx.AsyncClient(timeout=settings.HTTP_DEFAULT_TIMEOUT)
|
|
return _shared_client
|
|
|
|
|
|
async def close_client() -> None:
|
|
"""关闭共享客户端(在 FastAPI shutdown 时调用)。"""
|
|
global _shared_client
|
|
if _shared_client is not None and not _shared_client.is_closed:
|
|
await _shared_client.aclose()
|
|
_shared_client = None
|