perf(lps): Step2/3 — Turnstile 웜업·동적 차단 + 유휴 브라우저 정리

G마켓/옥션 Cloudflare Turnstile 을 실제로 통과시키고, 쌓이는 브라우저를 정리한다.

Turnstile 규명(실측): patchright 가 콜드 ~12초에 자동 통과, 통과 후 cf_clearance 쿠키로
이후 요청은 ~5초(웜). 단 **리소스를 조금이라도 차단하면 Turnstile 지문검사에 걸려 실패**
(이미지만 막아도 실패). 하지만 **cf_clearance 확보 후엔 차단해도 재챌린지 없음**.
- 동적 차단: _block_active + _blocking_now() 훅. ESM 은 cf_clearance 있을 때만 차단
  (콜드=허용→Turnstile 통과, 웜=이미지/미디어/폰트 차단→대역폭↓). ready_timeout 25s.
  best-effort Turnstile 체크박스 클릭(인터랙티브/나쁜IP 대비).
- 웜업: 기동 시 gmarket/auction/coupang 를 1회 풀어 쿠키 선점(백그라운드) → 실 작업은 웜.
- 유휴 브라우저 정리: _last_used + close_if_idle + run_browser_reaper(유휴 120s) — 메모리 회수.
  쿠키는 user_data_dir 에 남아 재기동해도 (같은 IP면) 웜 유지.
- 테스트: 유휴 정리 2종.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
민헌andClaude Fable 5 committed 2026-07-09 20:20:58 +09:00
1 parent 4b8dee8583
commit 8a594b667d
4 files changed
+140 -6

No files matched your search

+33 -1
View File
@@ -4,8 +4,10 @@ from pathlib import Path
import pytest
import time
from services.search.card_parser import parse_cards
from services.search.browser_base import is_proxy_error
from services.search.browser_base import is_proxy_error, BrowserSearchAdapter
from services.search.esm.selectors import GMARKET, AUCTION
from services.search.st11.selectors import CARDS as ST11
@@ -62,6 +64,36 @@ def test_is_proxy_error_false(msg):
assert is_proxy_error(msg) is False
class _IdleAdapter(BrowserSearchAdapter):
source = "test"
def _search_url(self, q, l): return ""
def _parse(self, h): return []
class _FakeCtx:
def __init__(self): self.closed = False
async def close(self): self.closed = True
async def test_close_if_idle_keeps_recent_closes_idle():
ad = _IdleAdapter()
ad._ctx = _FakeCtx()
ad._last_used = time.monotonic() # 방금 사용
await ad.close_if_idle(60)
assert ad._ctx is not None # 최근 사용 → 유지
ctx = ad._ctx
ad._last_used = time.monotonic() - 100 # 100s 전(유휴)
await ad.close_if_idle(60)
assert ad._ctx is None and ctx.closed # 유휴 초과 → 브라우저 정리
async def test_close_if_idle_skips_when_no_ctx():
ad = _IdleAdapter() # 브라우저 미기동
await ad.close_if_idle(0) # 예외 없이 no-op
assert ad._ctx is None
def test_dedup_by_link():
# 동일 링크 카드가 반복돼도 1건으로 축약
card = ('<div class="box__item-container"><a href="/x"><span class="text__item-title">상품명 물병</span></a>'