feat(lps): IP 선제 로테이션 — 요청 예산·포트 쿨다운·ip_session 관측
쿠팡 크롤 IP 를 '막힐 때까지' 쓰던 방식을 '막히기 전에 교체'로 전환한다. - 요청 예산(LPS_IP_REQUEST_BUDGET, 기본 3): IP당 요청 수가 예산에 닿으면 차단 전에 선제 회전. 실측상 5회 부근 차단 이력이 있어 보수적으로 3회. 선제 교체된 포트는 평판이 깨끗해 로테이션 복귀 시 재사용된다. - 포트 쿨다운(LPS_PORT_COOLDOWN_SEC, 기본 max(sticky,30분)): 차단 감지· 전송오류 포트는 격리하고 _port() 가 건너뛴다. 전 포트 쿨다운이면 만료 임박 포트 사용(가용성 우선). 포트 수는 config 범위에서 동적 산출. - 차단 재시도 소진 시에도 회전 예약 — 불탄 포트로 다음 검색을 하지 않음. - ip_session 테이블 신설: 세션마다 요청 수·성공/차단·종료 사유(budget/ block/proxy_error/window/idle/shutdown)를 기록. bot_detection 과 달리 무사 종료도 남아 예산 상한 튜닝의 원천 데이터가 된다(쿼리 database.md). models.py·migrations·init.sql(lps_db 섹션) 동행 갱신, dev DB 적용 완료. - 테스트 17건 추가(쿨다운·예산 판정·세션 기록·CRUD), 전체 126 passed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f97e407f7c
commit
3c5286aeec
14 files changed
+409
-18
No files matched your search
@@ -49,8 +49,9 @@ class _MockCtx:
|
||||
class _MockProxy:
|
||||
enabled = True
|
||||
session_minutes = 10
|
||||
def __init__(self): self.rotations = 0
|
||||
def __init__(self): self.rotations, self.burned = 0, []
|
||||
def rotate(self): self.rotations += 1
|
||||
def mark_burned(self, port, cooldown_sec=None): self.burned.append(port)
|
||||
def playwright_proxy(self): return None
|
||||
@property
|
||||
def current_port(self): return 10001
|
||||
@@ -70,6 +71,7 @@ class _MockAdapter(BrowserSearchAdapter):
|
||||
self._ctx = _MockCtx(self._page)
|
||||
self._force_recycle = False
|
||||
self._ip_requests = 0
|
||||
self._current_port = self._proxy.current_port if self._proxy else None # 실제 _ensure_browser 와 동일
|
||||
|
||||
async def _ensure_net_meter(self, page): pass # CDP 없음 → last_bytes=DOM 크기
|
||||
async def _wait_ready(self, page): pass
|
||||
@@ -110,11 +112,12 @@ async def test_non_proxy_goto_error_raises_no_rotation():
|
||||
|
||||
|
||||
async def test_persistent_block_exhausts_and_raises_blocked():
|
||||
# 차단 2회 연속(max_block_retries=1) → 회전 1회 후 소진 → blocked=True 로 실패
|
||||
# 차단 2회 연속(max_block_retries=1) → 재시도 회전 1회 + 소진 후 '다음 검색용' 회전 1회 → blocked=True 로 실패
|
||||
ad = _ad(["x", "x"])
|
||||
with pytest.raises(AdapterError) as ei:
|
||||
await ad.search("q")
|
||||
assert ei.value.blocked is True and ad._proxy.rotations == 1
|
||||
assert ei.value.blocked is True and ad._proxy.rotations == 2
|
||||
assert ad._proxy.burned == [10001, 10001] # 차단마다 해당 포트 쿨다운 격리
|
||||
|
||||
|
||||
# ── 라이브 스모크(옵트인): 실제 사이트 셀렉터·안티봇 드리프트 감지 ──────
|
||||
|
||||
Reference in new issue
Block a user