From 7a0019f45fa386d1788633aaebbd42d7cdde3084 Mon Sep 17 00:00:00 2001 From: MiyakoMeow <110924386+MiyakoMeow@users.noreply.github.com> Date: Mon, 3 Mar 2025 06:11:15 +0800 Subject: [PATCH] feat: do not select when all timeout --- fetch_ips.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/fetch_ips.py b/fetch_ips.py index a6578f5e8..817e79c53 100644 --- a/fetch_ips.py +++ b/fetch_ips.py @@ -19,16 +19,18 @@ from retry import retry from common import GITHUB_URLS, write_hosts_content -PING_LIST: Dict[str, int] = dict() +PING_TIMEOUT_SEC: int = 1 DISCARD_LIST: List[str] = ["1.0.1.1", "1.2.1.1", "127.0.0.1"] +PING_LIST: Dict[str, int] = dict() + + def ping_cached(ip: str) -> int: global PING_LIST if ip in PING_LIST: return PING_LIST[ip] - ping_timeout_sec = 1 - ping_times = [ping(ip, timeout=ping_timeout_sec).rtt_avg_ms for _ in range(3)] + ping_times = [ping(ip, timeout=PING_TIMEOUT_SEC).rtt_avg_ms for _ in range(3)] ping_times.sort() print(f'Ping {ip}: {ping_times} ms') PING_LIST[ip] = ping_times[1] # 取中位数 @@ -41,6 +43,10 @@ def select_ip_from_list(ip_list: List[str]) -> Optional[str]: ping_results = [(ip, ping_cached(ip)) for ip in ip_list] ping_results.sort(key=lambda x: x[1]) best_ip = ping_results[0][0] + # 全都超时?那就不选了 + if ping_results[0][1] == PING_TIMEOUT_SEC * 1000: + print(f"{ping_results}, no selection") + return None print(f"{ping_results}, selected {best_ip}") return best_ip