fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性
This commit is contained in:
@@ -19,14 +19,16 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (3, 5)) -> tuple[bool, str
|
||||
|
||||
# ── 斗鱼主站可达性验证 ──
|
||||
try:
|
||||
response = requests.get(
|
||||
"https://www.douyu.com",
|
||||
proxies=proxies,
|
||||
timeout=timeout,
|
||||
headers={"User-Agent": "Mozilla/5.0"},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return True, "代理可用 → 斗鱼可达"
|
||||
with requests.Session() as sess:
|
||||
sess.trust_env = False
|
||||
with sess.get(
|
||||
"https://www.douyu.com",
|
||||
proxies=proxies,
|
||||
timeout=timeout,
|
||||
headers={"User-Agent": "Mozilla/5.0"},
|
||||
) as response:
|
||||
response.raise_for_status()
|
||||
return True, "代理可用 → 斗鱼可达"
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
err_msg = str(exc)
|
||||
if "Tunnel connection failed" in err_msg or "503" in err_msg:
|
||||
@@ -63,13 +65,12 @@ def verify_proxies_concurrent(
|
||||
|
||||
if return_all:
|
||||
available: list[str] = []
|
||||
with ThreadPoolExecutor(
|
||||
max_workers=min(max_workers, len(proxy_urls))
|
||||
) as executor:
|
||||
future_map = {
|
||||
executor.submit(verify_proxy_url, proxy, timeout): proxy
|
||||
for proxy in proxy_urls
|
||||
}
|
||||
executor = ThreadPoolExecutor(max_workers=min(max_workers, len(proxy_urls)))
|
||||
future_map = {
|
||||
executor.submit(verify_proxy_url, proxy, timeout): proxy
|
||||
for proxy in proxy_urls
|
||||
}
|
||||
try:
|
||||
for future in as_completed(future_map):
|
||||
try:
|
||||
ok, _ = future.result()
|
||||
@@ -78,6 +79,8 @@ def verify_proxies_concurrent(
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"代理并发验证失败: {future_map[future]}: {exc}")
|
||||
continue
|
||||
finally:
|
||||
executor.shutdown(wait=True, cancel_futures=True)
|
||||
if available:
|
||||
logger.success(
|
||||
f"并发验证找到 {len(available)}/{len(proxy_urls)} 个可用代理"
|
||||
@@ -85,11 +88,12 @@ def verify_proxies_concurrent(
|
||||
return available, f"找到 {len(available)} 个可用代理"
|
||||
return None, f"共 {len(proxy_urls)} 个代理均不可用"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=min(max_workers, len(proxy_urls))) as executor:
|
||||
future_map = {
|
||||
executor.submit(verify_proxy_url, proxy, timeout): proxy
|
||||
for proxy in proxy_urls
|
||||
}
|
||||
executor = ThreadPoolExecutor(max_workers=min(max_workers, len(proxy_urls)))
|
||||
future_map = {
|
||||
executor.submit(verify_proxy_url, proxy, timeout): proxy
|
||||
for proxy in proxy_urls
|
||||
}
|
||||
try:
|
||||
for future in as_completed(future_map):
|
||||
proxy_url = future_map[future]
|
||||
try:
|
||||
@@ -103,5 +107,8 @@ def verify_proxies_concurrent(
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.debug(f"代理并发验证失败: {proxy_url}: {exc}")
|
||||
continue
|
||||
finally:
|
||||
# 命中首个可用代理后立即返回,不能在 with 退出时 wait=True 等慢/挂死代理超时。
|
||||
executor.shutdown(wait=False, cancel_futures=True)
|
||||
|
||||
return None, f"共 {len(proxy_urls)} 个代理均不可用"
|
||||
|
||||
Reference in New Issue
Block a user