fix: 代理瞬时失败显示真实延迟和原因
This commit is contained in:
@@ -161,6 +161,10 @@ def page(db: Session, q: str = "", status: str = "", offset: int = 0, limit: int
|
|||||||
return payload
|
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]:
|
def probe(scheme: str, host: str, port: int) -> tuple[bool, int, str]:
|
||||||
url = build_proxy_url(scheme, host, port)
|
url = build_proxy_url(scheme, host, port)
|
||||||
samples: list[int] = []
|
samples: list[int] = []
|
||||||
@@ -176,19 +180,19 @@ def probe(scheme: str, host: str, port: int) -> tuple[bool, int, str]:
|
|||||||
started = time.perf_counter()
|
started = time.perf_counter()
|
||||||
try:
|
try:
|
||||||
response = client.get(target)
|
response = client.get(target)
|
||||||
latency = int((time.perf_counter() - started) * 1000)
|
latency = _elapsed_ms(started)
|
||||||
if response.status_code >= 500:
|
if response.status_code >= 500:
|
||||||
samples.append(max(latency, LATENCY_LIMIT_MS))
|
samples.append(latency)
|
||||||
reasons.append(f"HTTP {response.status_code}")
|
reasons.append(f"HTTP {response.status_code}")
|
||||||
continue
|
continue
|
||||||
samples.append(latency)
|
samples.append(latency)
|
||||||
except Exception as exc:
|
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))
|
reasons.append(_short_reason(exc))
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return False, LATENCY_LIMIT_MS, _short_reason(exc)
|
return False, 0, _short_reason(exc)
|
||||||
if not samples:
|
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)))
|
average = int(round(sum(samples) / len(samples)))
|
||||||
if reasons:
|
if reasons:
|
||||||
return False, average, reasons[0]
|
return False, average, reasons[0]
|
||||||
@@ -431,7 +435,7 @@ def _probe_many(nodes: list[tuple[int, str, str, int]]) -> list[tuple[int, bool,
|
|||||||
try:
|
try:
|
||||||
ok, latency, reason = future.result()
|
ok, latency, reason = future.result()
|
||||||
except Exception as exc:
|
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))
|
results.append((node_id, ok, latency, reason))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|||||||
@@ -219,6 +219,29 @@ def test_probe_averages_euniv_related_sites(monkeypatch) -> None:
|
|||||||
assert reason == ""
|
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:
|
def test_admin_can_probe_one_and_all_proxies(monkeypatch) -> None:
|
||||||
import euniv.api as api_module
|
import euniv.api as api_module
|
||||||
|
|
||||||
|
|||||||
@@ -323,7 +323,9 @@ onUnmounted(() => stopPoll());
|
|||||||
</span>
|
</span>
|
||||||
<template v-else>{{ formatLatency(row.latency_ms) }}</template>
|
<template v-else>{{ formatLatency(row.latency_ms) }}</template>
|
||||||
</td>
|
</td>
|
||||||
<td :class="statusClass(row)">{{ row.status_text }}</td>
|
<td :class="statusClass(row)" :title="row.note || undefined">
|
||||||
|
{{ row.status_text }}<template v-if="row.note"> · {{ row.note }}</template>
|
||||||
|
</td>
|
||||||
<td class="when">{{ formatTime(row.last_checked_at) || "—" }}</td>
|
<td class="when">{{ formatTime(row.last_checked_at) || "—" }}</td>
|
||||||
<td>
|
<td>
|
||||||
<n-button
|
<n-button
|
||||||
|
|||||||
Reference in New Issue
Block a user