fix: 失效超过三十分钟的代理会重新检测
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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] = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user