继续优化登录逻辑

This commit is contained in:
yml2213
2026-06-25 08:40:00 +08:00
parent a62e7c192f
commit 0b4fe1daa2
6 changed files with 105 additions and 48 deletions
+30
View File
@@ -8,6 +8,8 @@
时由 ProxyResolver 内部被动同步兜底。
"""
import threading
import time
from typing import Optional
from loguru import logger
@@ -19,13 +21,18 @@ from .proxy_whitelist import DouyuWhitelistSyncer
class ProxyFetcher:
"""每次从代理 API 取 1 个新代理,无缓存无复用。"""
# 代理 API 最小调用间隔(秒)——防止高并发 + 多轮重试把代理 API 打爆
MIN_FETCH_INTERVAL = 1.5
def __init__(
self,
api_url: str,
whitelist_platform: str = "xiequ",
whitelist_credentials: dict = None,
stop_event: Optional[threading.Event] = None,
):
self.api_url = api_url
self.stop_event = stop_event
self._whitelist_syncer = (
DouyuWhitelistSyncer(
@@ -36,6 +43,10 @@ class ProxyFetcher:
else None
)
# 节流:跨线程共享的"上次取代理时刻"
self._last_fetch_at = 0.0
self._fetch_lock = threading.Lock()
def warmup_whitelist(self) -> tuple[bool, str]:
"""批次启动前调用一次:把当前本地出口 IP 同步到白名单。
@@ -60,12 +71,31 @@ class ProxyFetcher:
不主动检测/同步本地出口 IP;如果代理 API 返回"白名单错误"
由 ProxyResolver 内部被动同步兜底。
进程内节流:高并发场景下任意两次调用至少间隔 MIN_FETCH_INTERVAL 秒,
避免代理 API 被限频。
"""
with self._fetch_lock:
elapsed = time.monotonic() - self._last_fetch_at
if elapsed < self.MIN_FETCH_INTERVAL:
wait = self.MIN_FETCH_INTERVAL - elapsed
logger.debug(f"代理 API 节流等待 {wait:.2f}s")
if self.stop_event:
if self.stop_event.wait(wait):
return None
else:
time.sleep(wait)
self._last_fetch_at = time.monotonic()
if self.stop_event and self.stop_event.is_set():
return None
resolver = ProxyResolver(
api_url=self.api_url,
whitelist_syncer=self._whitelist_syncer,
sync_local_exit_ip=False,
sync_whitelist_once=False,
stop_event=self.stop_event,
)
result, msg = resolver.fetch_verified(
max_attempts=max_attempts,