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:
민헌andClaude Fable 5 committed 2026-07-13 16:35:44 +09:00
1 parent f97e407f7c
commit 3c5286aeec
14 files changed
+409 -18

No files matched your search

+27
View File
@@ -105,6 +105,33 @@ class price_history(MAIN_BASE):
)
class ip_session(MAIN_BASE):
"""IP(프록시 포트) 세션 종료 이력 — '이 IP 로 몇 번 요청하고 어떻게 끝났나'를 매 세션 기록.
bot_detection 은 차단된 세션만 남지만 여기엔 무사 종료도 남아, 요청 예산(LPS_IP_REQUEST_BUDGET)
상한 튜닝의 원천 데이터가 된다. (예: end_reason='block' 의 requests 분포 → 안전 상한 산출)"""
@staticmethod
def DBType():
return DBType.MAIN.value
__tablename__ = "ip_session"
id = Column(UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"))
source = Column(String(20), nullable=False) # coupang 등
proxy_port = Column(Integer, nullable=True) # 사용 포트(=IP 세션), 프록시 미사용이면 NULL
requests = Column(Integer, nullable=False) # 이 IP 로 보낸 요청 수
ok_count = Column(Integer, nullable=False, server_default=text("0")) # 성공 검색 수
blocked_count = Column(Integer, nullable=False, server_default=text("0")) # 차단 감지 수
elapsed_sec = Column(Integer, nullable=True) # 세션 지속 시간(초)
end_reason = Column(String(20), nullable=False) # budget/block/proxy_error/window/idle/shutdown/rotate
created_at = Column(DateTime(timezone=True), nullable=False, server_default=text("now()")) # 세션 종료 시각
__table_args__ = (
# 상한 튜닝 쿼리(소스·기간별 종료 사유 분포) 최적화
Index("ix_ip_session_source", "source", "created_at"),
)
class bot_detection(MAIN_BASE):
"""봇 감지 이력 — '이 IP로 몇 번째 요청에서, 어떤 방식으로 차단됐나'를 축적해 패턴 분석.
(예: SELECT avg(ip_request_no) → IP당 평균 몇 요청 만에 감지되는지)"""