From d97843079e4c006922b201fe35cb3746cdb2ad71 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Mon, 16 Nov 2026 02:55:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=B1=E6=95=88=E8=B6=85=E8=BF=87?= =?UTF-8?q?=E4=B8=89=E5=8D=81=E5=88=86=E9=92=9F=E7=9A=84=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E4=BC=9A=E9=87=8D=E6=96=B0=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/proxy_pool.py | 24 +++++++++++++++++++----- backend/tests/test_proxy_pool.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/backend/euniv/proxy_pool.py b/backend/euniv/proxy_pool.py index 5118c88..0f6897c 100644 --- a/backend/euniv/proxy_pool.py +++ b/backend/euniv/proxy_pool.py @@ -6,7 +6,7 @@ import logging import threading import time from concurrent.futures import ThreadPoolExecutor, as_completed -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone from urllib.parse import urlparse import httpx @@ -316,6 +316,7 @@ def _refresh_body() -> dict: } new_ids: list[int] = [] alive_ids: list[int] = [] + dead_ids: list[int] = [] for item in candidates: if not is_china(item.country): continue @@ -340,6 +341,7 @@ def _refresh_body() -> dict: node.anonymity = item.anonymity or node.anonymity node.source = item.source or node.source if node.status == STATUS_DEAD: + dead_ids.append(node.id) continue if node.status == STATUS_NEW: new_ids.append(node.id) @@ -349,7 +351,7 @@ def _refresh_body() -> dict: if node.status == STATUS_ALIVE and node.id not in alive_ids and node.id not in new_ids: alive_ids.append(node.id) db.commit() - probe_ids = _pick_probe_ids(db, new_ids, alive_ids) + probe_ids = _pick_probe_ids(db, new_ids, alive_ids, dead_ids) nodes = [db.get(ProxyNode, node_id) for node_id in probe_ids] snapshot = [ (node.id, node.scheme, node.host, int(node.port)) @@ -364,8 +366,6 @@ def _refresh_body() -> dict: node = db.get(ProxyNode, node_id) if node is None: continue - if node.status == STATUS_DEAD: - continue node.last_checked_at = _now() node.latency_ms = latency if ok: @@ -397,7 +397,7 @@ def _refresh_body() -> dict: return sync_view(db) -def _pick_probe_ids(db: Session, new_ids: list[int], alive_ids: list[int]) -> list[int]: +def _pick_probe_ids(db: Session, new_ids: list[int], alive_ids: list[int], dead_ids: list[int] | None = None) -> list[int]: ordered: list[int] = [] seen: set[int] = set() for node_id in new_ids: @@ -419,6 +419,20 @@ def _pick_probe_ids(db: Session, new_ids: list[int], alive_ids: list[int]) -> li ordered.append(node.id) if len(ordered) >= MAX_PROBES: break + cutoff = _now() - timedelta(minutes=30) + for node_id in dead_ids or []: + if len(ordered) >= MAX_PROBES or node_id in seen: + continue + node = db.get(ProxyNode, node_id) + checked = node.last_checked_at if node is not None else None + if checked is None: + continue + if checked.tzinfo is None: + checked = checked.replace(tzinfo=timezone.utc) + if checked > cutoff: + continue + seen.add(node_id) + ordered.append(node_id) return ordered diff --git a/backend/tests/test_proxy_pool.py b/backend/tests/test_proxy_pool.py index bf0845d..57aa5ac 100644 --- a/backend/tests/test_proxy_pool.py +++ b/backend/tests/test_proxy_pool.py @@ -191,6 +191,35 @@ def test_failed_proxy_is_not_reprobed(monkeypatch) -> None: assert node.status == "dead" +def test_old_dead_proxy_is_reprobed(monkeypatch) -> None: + from datetime import datetime, timedelta, timezone + + _holds.clear() + stale = _node("8.8.8.8", 88, status="dead") + with SessionLocal() as db: + node = db.get(ProxyNode, stale.id) + assert node is not None + node.last_checked_at = datetime.now(timezone.utc) - timedelta(hours=1) + db.commit() + monkeypatch.setattr( + "euniv.proxy_pool.collect_all", + lambda: [ProxyCandidate("http", "8.8.8.8", 88, "cn", "elite", "proxyhub")], + ) + called: list[str] = [] + + def fake_probe(scheme: str, host: str, port: int): + called.append(host) + return True, 20, "" + + monkeypatch.setattr("euniv.proxy_pool.probe", fake_probe) + refresh_once() + assert called == ["8.8.8.8"] + with SessionLocal() as db: + node = db.get(ProxyNode, stale.id) + assert node is not None + assert node.status == "alive" + + def test_probe_averages_euniv_related_sites(monkeypatch) -> None: seen: list[str] = []