perf(lps): 폴백 크롤 병렬화 + 오픈마켓 대역폭 절감

한 검색의 소요·DECODO 비용을 크게 낮춘다(실측: 110s→52s, proxy 8MB→2MB, 총비용 70%↓).

- 폴백 병렬화: _enrich_with_fallback 의 순차 for-loop → gather 동시 크롤. 각 몰 어댑터는
  별 브라우저 인스턴스라 병렬 안전. 소요=합→최댓값. AI usage 계측 동시성 불변식 주석 명시
  (judge.last_usage 세팅~읽기 사이 await 없음 → asyncio 협조 스케줄링상 안전).
- 대역폭: blocked_resource_types 를 어댑터별 설정화. 오픈마켓은 이미지/미디어/폰트만 차단
  (CSS/JS 유지 — 챌린지/렌더 보호). 격리 실험으로 차단 유무가 ESM 챌린지 성패에 무관함 확인
  (실패 원인은 IP평판/headless). st11 라이브 0.11MB 검증.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
민헌andClaude Fable 5 committed 2026-07-09 16:11:32 +09:00
1 parent a80109cdcf
commit bf19d4a8fd
4 files changed
+26 -14

No files matched your search

+17 -9
View File
@@ -102,33 +102,41 @@ def build_search_handler(
return products, per_source, tech_failed
async def _match(target: dict, products: list, base_price, metrics: SearchMetrics):
"""필터 → (있으면) AI 같은상품 판정 → 매칭 후보. AI 토큰은 metrics 에 누적."""
"""필터 → (있으면) AI 같은상품 판정 → 매칭 후보. AI 토큰은 metrics 에 누적.
⚠️ 동시성 불변식: judge.judge 가 last_usage 를 세팅한 뒤 여기서 읽기까지 await 이 없어야
한다(asyncio 협조 스케줄링상 그 사이 다른 코루틴이 last_usage 를 덮어쓸 수 없음). 병렬 폴백 안전."""
candidates, _ = apply_filters(products, base_price=base_price)
if judge is not None and candidates:
verdicts = await judge.judge(target, candidates)
metrics.add_ai(judge.last_usage)
metrics.add_ai(judge.last_usage) # ← await 직후 즉시 읽음(사이에 await 금지)
candidates = [c for c, v in zip(candidates, verdicts) if v.is_match]
return candidates
async def _enrich_with_fallback(target: dict, query: str, matched: list, base_price, metrics: SearchMetrics):
"""네이버가 커버 못 한 오픈마켓만 직접 크롤(폴백) → 같은상품 판정 후 병합.
사용자 규칙: '네이버로 그 몰 값 확보 성공 → 그 값, 실패(몰 없음) → 실사이트 크롤'."""
사용자 규칙: '네이버로 그 몰 값 확보 성공 → 그 값, 실패(몰 없음) → 실사이트 크롤'.
미커버 몰들을 **동시 크롤**한다(각 어댑터=별 브라우저 인스턴스라 병렬 안전, 소요=합→최댓값)."""
if not fallbacks:
return matched
covered = {canonical_mall(p) for p in matched}
for src, adapter in fallbacks.items():
mall = MALL_BY_SOURCE.get(src, src)
if mall in covered: # 네이버가 이미 그 몰 최저가 확보 → 크롤 생략
continue
todo = [(src, ad) for src, ad in fallbacks.items() if MALL_BY_SOURCE.get(src, src) not in covered]
if not todo:
return matched
async def _crawl_match(src, adapter):
try:
crawled = await _timed_search(adapter, query, src, metrics, crawl=True)
except Exception as ex:
LOG.w(f"[fallback:{src}] 크롤 실패(무시): {type(ex).__name__}: {ex}")
continue
return []
hits = await _match(target, crawled, base_price, metrics)
if hits:
LOG.d(f"[fallback:{src}] 크롤 {len(crawled)}건 중 같은상품 {len(hits)}건 병합")
matched = matched + hits
return hits
results = await asyncio.gather(*[_crawl_match(s, a) for s, a in todo])
for hits in results:
matched = matched + hits
return matched
async def _round_queries(base_query: str, target: dict, metrics: SearchMetrics):