fix: 백그라운드 reindex 전역 세마포어(3) 추가 — 이벤트 루프 포화 방지

500+ 티커 bulk 스캔 시 ensure_future로 수백 개의 SEC EDGAR 태스크가
동시 생성되어 이벤트 루프 포화 → 서버 응답 불가.
_BACKGROUND_REINDEX_SEMAPHORE(3)으로 동시 실행을 최대 3개로 제한.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
main
I Luk Kim 4 months ago
parent 86b28330b6
commit 4e77043671

@ -25,6 +25,10 @@ logger = logging.getLogger(__name__)
# Maximum exhibit content size (5 MB) # Maximum exhibit content size (5 MB)
MAX_EXHIBIT_SIZE = 5 * 1024 * 1024 MAX_EXHIBIT_SIZE = 5 * 1024 * 1024
# Limit concurrent background reindex tasks to prevent event-loop saturation
# when bulk scans trigger hundreds of simultaneous SEC EDGAR fetches.
_BACKGROUND_REINDEX_SEMAPHORE = asyncio.Semaphore(3)
class SECFilingsService: class SECFilingsService:
SUPPORTED_FORM_TYPES: Set[str] = { SUPPORTED_FORM_TYPES: Set[str] = {
@ -247,6 +251,7 @@ class SECFilingsService:
self, ticker: str, form_types: Optional[Set[str]], lock: asyncio.Lock self, ticker: str, form_types: Optional[Set[str]], lock: asyncio.Lock
) -> None: ) -> None:
"""Re-index a stale ticker in the background with its own DB session.""" """Re-index a stale ticker in the background with its own DB session."""
async with _BACKGROUND_REINDEX_SEMAPHORE:
async with lock: async with lock:
try: try:
from app.core.database import AsyncSessionLocal from app.core.database import AsyncSessionLocal

Loading…
Cancel
Save