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
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user