From 6340309e6662a62daa3b5572057da01974ff1197 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Sun, 15 Nov 2026 05:26:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BB=A3=E7=90=86=E7=9E=AC=E6=97=B6?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=98=BE=E7=A4=BA=E7=9C=9F=E5=AE=9E=E5=BB=B6?= =?UTF-8?q?=E8=BF=9F=E5=92=8C=E5=8E=9F=E5=9B=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/euniv/proxy_pool.py | 16 ++++++++++------ backend/tests/test_proxy_pool.py | 23 +++++++++++++++++++++++ web/src/features/admin/ProxiesView.vue | 4 +++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/backend/euniv/proxy_pool.py b/backend/euniv/proxy_pool.py index efc8a0c..164ec37 100644 --- a/backend/euniv/proxy_pool.py +++ b/backend/euniv/proxy_pool.py @@ -161,6 +161,10 @@ def page(db: Session, q: str = "", status: str = "", offset: int = 0, limit: int return payload +def _elapsed_ms(started: float) -> int: + return max(0, int((time.perf_counter() - started) * 1000)) + + def probe(scheme: str, host: str, port: int) -> tuple[bool, int, str]: url = build_proxy_url(scheme, host, port) samples: list[int] = [] @@ -176,19 +180,19 @@ def probe(scheme: str, host: str, port: int) -> tuple[bool, int, str]: started = time.perf_counter() try: response = client.get(target) - latency = int((time.perf_counter() - started) * 1000) + latency = _elapsed_ms(started) if response.status_code >= 500: - samples.append(max(latency, LATENCY_LIMIT_MS)) + samples.append(latency) reasons.append(f"HTTP {response.status_code}") continue samples.append(latency) except Exception as exc: - samples.append(int((time.perf_counter() - started) * 1000) or LATENCY_LIMIT_MS) + samples.append(_elapsed_ms(started)) reasons.append(_short_reason(exc)) except Exception as exc: - return False, LATENCY_LIMIT_MS, _short_reason(exc) + return False, 0, _short_reason(exc) if not samples: - return False, LATENCY_LIMIT_MS, reasons[0] if reasons else "无法连接" + return False, 0, reasons[0] if reasons else "无法连接" average = int(round(sum(samples) / len(samples))) if reasons: return False, average, reasons[0] @@ -431,7 +435,7 @@ def _probe_many(nodes: list[tuple[int, str, str, int]]) -> list[tuple[int, bool, try: ok, latency, reason = future.result() except Exception as exc: - ok, latency, reason = False, LATENCY_LIMIT_MS, _short_reason(exc) + ok, latency, reason = False, 0, _short_reason(exc) results.append((node_id, ok, latency, reason)) return results diff --git a/backend/tests/test_proxy_pool.py b/backend/tests/test_proxy_pool.py index 803caa1..35eab72 100644 --- a/backend/tests/test_proxy_pool.py +++ b/backend/tests/test_proxy_pool.py @@ -219,6 +219,29 @@ def test_probe_averages_euniv_related_sites(monkeypatch) -> None: assert reason == "" +def test_probe_instant_connect_error_keeps_elapsed_latency(monkeypatch) -> None: + class FakeClient: + def __init__(self, **_kwargs) -> None: + return None + + def __enter__(self): + return self + + def __exit__(self, *_args) -> bool: + return False + + def get(self, _url: str): + raise httpx.ConnectError("boom") + + ticks = iter([0.0, 0.012, 0.012, 0.018, 0.018, 0.025]) + monkeypatch.setattr("euniv.proxy_pool.httpx.Client", FakeClient) + monkeypatch.setattr("euniv.proxy_pool.time.perf_counter", lambda: next(ticks)) + ok, latency, reason = probe("socks4", "10.0.0.9", 1080) + assert ok is False + assert latency == 8 + assert reason == "无法连接" + + def test_admin_can_probe_one_and_all_proxies(monkeypatch) -> None: import euniv.api as api_module diff --git a/web/src/features/admin/ProxiesView.vue b/web/src/features/admin/ProxiesView.vue index 05c801d..71ce511 100644 --- a/web/src/features/admin/ProxiesView.vue +++ b/web/src/features/admin/ProxiesView.vue @@ -323,7 +323,9 @@ onUnmounted(() => stopPoll()); - {{ row.status_text }} + + {{ row.status_text }} + {{ formatTime(row.last_checked_at) || "—" }}