Peach: Local-first video metadata scraper and media manager.
fix: 链接可达性探测扛住 TLS 抖动
longmeidaoe3ec8b19b4aa
diff --git a/scripts/install_entity_links.py b/scripts/install_entity_links.pyindex 24e5827d5..0168d0671 100644--- a/scripts/install_entity_links.py+++ b/scripts/install_entity_links.py@@ -110,22 +110,33 @@ def plan(connection: sqlite3.Connection, rows: list[dict]) -> list[dict]:GONE_STATUSES = {404, 410}-def resolves(url: str, timeout: float = 12.0) -> tuple[bool, str]:+def resolves(url: str, timeout: float = 12.0,+ tries: int = 3, pause: float = 3.0) -> tuple[bool, str]:"""这个地址现在还能打开吗。返回(能不能, 说明)。每个请求新建 client 并立刻关掉:这批地址分布在上百个互不相同的主机上,其中不少连不上,而失败的连接会在共享池里漏掉槽位,几十个请求之后一切都变成 PoolTimeout。++ 传输层异常重试,状态码不重试。这条出口的 TLS 大约三次断一次+ (`[SSL: UNEXPECTED_EOF_WHILE_READING]`),一次失败就记「打不开」是把抖动写成结论:+ eltra.jp 2026-09-05 连着两趟这样被判死,而每次重试一下就 200。404 重试三次还是 404。"""- try:- with httpx.Client(follow_redirects=True, timeout=timeout,- limits=httpx.Limits(max_connections=4,- max_keepalive_connections=0)) as client:- response = client.get(url, headers={"User-Agent": USER_AGENT})- except Exception as exc:- return False, f"取不到:{type(exc).__name__}"- if response.status_code != 200:- return False, f"HTTP {response.status_code}"- return True, "可打开"+ note = "取不到"+ for attempt in range(tries):+ try:+ with httpx.Client(follow_redirects=True, timeout=timeout,+ limits=httpx.Limits(max_connections=4,+ max_keepalive_connections=0)) as client:+ response = client.get(url, headers={"User-Agent": USER_AGENT})+ except Exception as exc:+ note = f"取不到:{type(exc).__name__}"+ if attempt + 1 < tries:+ time.sleep(pause)+ continue+ if response.status_code != 200:+ return False, f"HTTP {response.status_code}"+ return True, "可打开"+ return False, notedef is_gone(note: str) -> bool:diff --git a/tests/test_entity_link_install.py b/tests/test_entity_link_install.pyindex 6273ec1a9..6171a0670 100644--- a/tests/test_entity_link_install.py+++ b/tests/test_entity_link_install.py@@ -215,6 +215,63 @@ class InstallTests(unittest.TestCase):self.assertEqual(probed, [])+class ResolvesTests(unittest.TestCase):+ """可达性探测:抖动要重试,状态码是一次成局。"""++ class Client:+ """`httpx.Client` 的替身,按剧本逐次抛错或返回状态码。"""++ def __init__(self, script):+ self.script = script++ def __call__(self, **kwargs):+ return self++ def __enter__(self):+ return self++ def __exit__(self, *exc):+ return False++ def get(self, url, headers=None):+ step = self.script.pop(0)+ if isinstance(step, Exception):+ raise step+ return type("Response", (), {"status_code": step})()++ def setUp(self):+ # `time` 和 `httpx` 是进程共享的真模块,替身必须还回去:这一组用例先跑完,+ # 后面每个测试的 sleep 就都成了 list.append。+ self.module = load_module()+ self.slept = []+ for holder, name in ((self.module.time, "sleep"), (self.module.httpx, "Client")):+ self.addCleanup(setattr, holder, name, getattr(holder, name))+ self.module.time.sleep = self.slept.append++ def use(self, *script):+ self.module.httpx.Client = self.Client(list(script))++ def test_a_blip_on_the_first_try_is_not_a_conclusion(self):+ """这条出口的 TLS 大约三次断一次,一次失败就判死会把好站写成死链。"""+ self.use(ConnectionError("SSL: UNEXPECTED_EOF_WHILE_READING"), 200)+ self.assertEqual(self.module.resolves("https://eltra.jp/"), (True, "可打开"))+ self.assertEqual(len(self.slept), 1)++ def test_a_status_code_is_answered_at_once(self):+ """404 重试三次还是 404,多敲两次只是浪费对方的带宽。"""+ self.use(404, 200)+ self.assertEqual(self.module.resolves("https://x.jp/gone"), (False, "HTTP 404"))+ self.assertEqual(self.slept, [])++ def test_every_try_failing_keeps_the_reason(self):+ """全程取不到时说明必须留着:那不是「页面没了」,下一步是换个时间复查。"""+ self.use(*[ConnectionError("boom")] * 3)+ ok, note = self.module.resolves("https://down.jp/")+ self.assertFalse(ok)+ self.assertIn("ConnectionError", note)+ self.assertFalse(self.module.is_gone(note), "取不到不等于确证没了")++class NormalizeHostsTests(unittest.TestCase):"""twitter.com → x.com 的主机归一:改写要保真,撞上已有的新写法要删而不是炸。