优化代理相关, 移除旧的死代码
This commit is contained in:
+14
-16
@@ -1,9 +1,9 @@
|
||||
"""简化版代理获取器(替代 ProxyManager)。
|
||||
"""简化版代理获取器。
|
||||
|
||||
专为短效代理设计:无池、无冷却、无复用。
|
||||
每次调用 fetch_new_proxy() 从 API 取 1 个新代理,用完即弃。
|
||||
|
||||
线程安全:白名单同步有全局锁 + IP缓存避免重复同步。
|
||||
线程安全:白名单同步有全局锁 + 标志位避免重复同步。
|
||||
"""
|
||||
|
||||
import threading
|
||||
@@ -26,31 +26,30 @@ class ProxyFetcher:
|
||||
):
|
||||
self.api_url = api_url
|
||||
|
||||
_wl_platform = whitelist_platform or "xiequ"
|
||||
_wl_credentials = whitelist_credentials
|
||||
|
||||
self._whitelist_syncer = (
|
||||
DouyuWhitelistSyncer(platform=_wl_platform, credentials=_wl_credentials)
|
||||
if _wl_credentials
|
||||
DouyuWhitelistSyncer(
|
||||
platform=whitelist_platform or "xiequ",
|
||||
credentials=whitelist_credentials,
|
||||
)
|
||||
if whitelist_credentials
|
||||
else None
|
||||
)
|
||||
# 跨线程共享:已同步白名单的IP,避免5个线程重复同步同一个IP
|
||||
self._synced_ip: Optional[str] = None
|
||||
# 跨线程共享:是否已同步过白名单,避免多线程重复同步
|
||||
self._synced = False
|
||||
self._sync_lock = threading.Lock()
|
||||
|
||||
def fetch_new_proxy(self, max_attempts: int = 3) -> Optional[str]:
|
||||
"""从代理 API 获取 1 个可用代理,失败返回 None。"""
|
||||
# 快速检查:是否已经同步过白名单
|
||||
skip_sync = False
|
||||
if self._whitelist_syncer:
|
||||
with self._sync_lock:
|
||||
if self._synced_ip:
|
||||
skip_sync = True
|
||||
already_synced = self._synced
|
||||
else:
|
||||
already_synced = False
|
||||
|
||||
resolver = ProxyResolver(
|
||||
api_url=self.api_url,
|
||||
whitelist_syncer=self._whitelist_syncer,
|
||||
sync_local_exit_ip=bool(self._whitelist_syncer) and not skip_sync,
|
||||
sync_local_exit_ip=bool(self._whitelist_syncer) and not already_synced,
|
||||
sync_whitelist_once=False,
|
||||
)
|
||||
result, msg = resolver.fetch_verified(
|
||||
@@ -58,10 +57,9 @@ class ProxyFetcher:
|
||||
return_all=False,
|
||||
)
|
||||
|
||||
# 记录已同步的IP
|
||||
if self._whitelist_syncer and result:
|
||||
with self._sync_lock:
|
||||
self._synced_ip = self._synced_ip or "done"
|
||||
self._synced = True
|
||||
|
||||
if isinstance(result, str):
|
||||
logger.info(f"获取新代理: {result}")
|
||||
|
||||
Reference in New Issue
Block a user