perf: API 성능 개선 Round 2 — 9개 항목 (P6-P14)
Tier 1: - P6: 미들웨어 request_info 지연 추출 (성공 경로에서 body/headers 읽기 제거) - P7: GZip 압축 미들웨어 (minimum_size=1000, ~4x 압축) - P8: Redis maxmemory 512MB + allkeys-lru 퇴거 정책 Tier 2: - P9: _store_ticker_data() batch upsert 전환 (ON CONFLICT DO NOTHING) - P10: DB 커넥션 풀 증가 (pool_size=20, max_overflow=30, 환경변수 설정) - P11: Attention 엔드포인트 Redis 캐시 추가 (@with_cache ttl=3600) Tier 3: - P12: Dockerfile 멀티 워커 (--workers 4, dev는 --reload 오버라이드) - P13: aiohttp ClientSession 싱글턴 공유 (4개 파일 7곳 TCP/TLS 재사용) - P14: Pydantic model_validate → model_construct (bulk 경로 validation 스킵) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>main
parent
6a98cb2d94
commit
0752b29a63
@ -0,0 +1,33 @@
|
|||||||
|
"""
|
||||||
|
Shared aiohttp ClientSession singleton.
|
||||||
|
|
||||||
|
Reuses a single TCP connection pool across all HTTP clients,
|
||||||
|
avoiding repeated TCP/TLS handshake overhead per request.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
_session: Optional[aiohttp.ClientSession] = None
|
||||||
|
|
||||||
|
|
||||||
|
async def get_http_session() -> aiohttp.ClientSession:
|
||||||
|
"""Get or create the shared aiohttp ClientSession."""
|
||||||
|
global _session
|
||||||
|
if _session is None or _session.closed:
|
||||||
|
_session = aiohttp.ClientSession(
|
||||||
|
timeout=aiohttp.ClientTimeout(total=30),
|
||||||
|
)
|
||||||
|
return _session
|
||||||
|
|
||||||
|
|
||||||
|
async def close_http_session() -> None:
|
||||||
|
"""Close the shared session. Call on app shutdown."""
|
||||||
|
global _session
|
||||||
|
if _session and not _session.closed:
|
||||||
|
await _session.close()
|
||||||
|
_session = None
|
||||||
Loading…
Reference in New Issue