한 DECODO 계정을 여러 워커 프로세스가 나눠 쓰는 전제로 전환한다. 인메모리 장부는 프로세스마다 따로라 (1) 같은 IP 를 동시에 잡고 (2) 한쪽이 태운 IP 를 다른 쪽이 곧바로 집으며 (3) 재시작하면 쿨다운이 통째로 사라졌다. proxy_port 테이블 = 단일 진실. 상태는 세 시각으로만 표현한다(leased/rest/cooldown_until). - acquire: 한 UPDATE 안에서 FOR UPDATE SKIP LOCKED 로 후보를 잠그고 임대까지 끝낸다 (잡 큐와 같은 방식 — SELECT 후 UPDATE 로 나누면 그 틈에 다른 프로세스가 같은 행을 집는다) - 회전은 LRU(last_used_at). 프로세스가 몇 개든 '가장 오래 안 쓴 IP'를 집으므로 전체가 자연히 한 바퀴씩 돈다 → 프로세스별 seed_offset 계산 제거 - 죽은 프로세스 회수: leased_until 만료로 자동 복귀(별도 reaper 불필요) - 차단·휴식은 전역이라 재시작해도 유지된다 DB 왕복은 비동기라 검색 루프(동기)에서 곧바로 못 한다 → 회전·차단을 pending 에 적어두고 ensure_port(브라우저 재기동 직전, async)에서 한 번에 flush. _close_ctx 에서도 flush 해 종료 시 유실(=태운 IP 를 남이 그대로 집는 상황)을 막는다. **프로필 슬롯**(services/search/profile_slot): Chrome 은 user_data_dir 당 1 인스턴스다. 예전엔 워커 인덱스로만 갈라서 프로세스 2개면 같은 경로를 잡아 두 번째가 통째로 죽었다 (실측: 잡 3건 중 2건 DEAD, TargetClosedError). 파일 락으로 슬롯을 선점한다 — PID 경로가 아니라 슬롯이라 재시작 시 재사용돼 웜 쿠키(cf_clearance·Akamai)를 버리지 않는다. 검증: 프로세스 2개 동시 acquire 20회 → 중복 배정 0건. 워커 2프로세스 e2e → 잡 3건 모두 DONE(네이버가 삼다수 최저가 획득 8,960 < 13,200). 테스트 14건 추가, 전체 217 passed.
35 lines
1.6 KiB
Python
35 lines
1.6 KiB
Python
"""Chrome 프로필 슬롯 배정 — 프로세스가 여러 개여도 같은 프로필을 잡으면 안 된다."""
|
|
|
|
import os
|
|
|
|
from services.search import profile_slot
|
|
from services.search.profile_slot import claim_profile_slot
|
|
|
|
|
|
def test_second_claim_gets_a_different_slot(tmp_path):
|
|
"""같은 소스·같은 워커 인덱스라도 두 번째 요청(=다른 프로세스)은 다른 슬롯을 받아야 한다."""
|
|
a = claim_profile_slot(str(tmp_path), "coupang", worker_index=0)
|
|
b = claim_profile_slot(str(tmp_path), "coupang", worker_index=0)
|
|
assert a != b and os.path.isdir(a) and os.path.isdir(b)
|
|
|
|
|
|
def test_sources_and_workers_are_separate(tmp_path):
|
|
c = claim_profile_slot(str(tmp_path), "coupang", worker_index=0)
|
|
n = claim_profile_slot(str(tmp_path), "naver", worker_index=0)
|
|
w1 = claim_profile_slot(str(tmp_path), "coupang", worker_index=1)
|
|
assert len({c, n, w1}) == 3
|
|
|
|
|
|
def test_slot_is_reused_after_release(tmp_path):
|
|
"""프로세스가 죽으면 OS 가 락을 풀고 같은 슬롯이 재사용된다 — 웜 쿠키를 버리지 않기 위함."""
|
|
a = claim_profile_slot(str(tmp_path), "coupang", worker_index=0)
|
|
for fh in list(profile_slot._HELD): # 프로세스 종료를 흉내
|
|
fh.close()
|
|
profile_slot._HELD.remove(fh)
|
|
assert claim_profile_slot(str(tmp_path), "coupang", worker_index=0) == a
|
|
|
|
|
|
def test_exhausted_slots_fall_back_without_raising(tmp_path):
|
|
paths = [claim_profile_slot(str(tmp_path), "coupang", worker_index=0, max_slots=2) for _ in range(3)]
|
|
assert paths[2] == paths[1] # 마지막 슬롯 공유(검색 중단보다 낫다)
|