From 213be57055381004ab028a434883ac0deaa43374 Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Fri, 23 Oct 2026 08:58:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(monitor):=20=E8=A7=A3=E6=9E=90=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E9=93=BE=E6=8E=A5=E6=97=B6=EF=BC=8C=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=B1=82=E6=8A=96=E5=8A=A8=E9=87=8D=E8=AF=95=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 容器刚起来第一发会撞上 DNS 解析失败(实测 [Errno -3]),隔半秒就正常。 批量解析时这种一次性失败只会让某一行莫名其妙地红掉。空 body / 403 仍然不重试 —— 那是风控信号。 --- api/monitor/video_link.py | 20 ++++++++++++++++- tests/test_monitor_video_link.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/api/monitor/video_link.py b/api/monitor/video_link.py index 507bff1..8b4bc2b 100644 --- a/api/monitor/video_link.py +++ b/api/monitor/video_link.py @@ -19,6 +19,8 @@ import asyncio import re from typing import Any, Dict, List +import httpx + from . import adapters, douyin_api, service # 一次最多解析这么多条。限流不是怕算不过来 —— 是怕一不小心粘进来几百条,把详情接口 @@ -26,6 +28,8 @@ from . import adapters, douyin_api, service MAX_LINKS = 20 # 每两条之间歇一下。同一个登录态连续请求,节律太密比数量更容易被识别。 REQUEST_GAP_SECONDS = 0.4 +# 网络层失败后隔多久重试那一次。 +_DETAIL_RETRY_SLEEP_SECONDS = 1.0 _URL_RE = re.compile(r"https?://[^\s,。;、))\"'<>]+") # 19 位作品 id,但放宽到 15–20:平台偶尔给别的位数,只按 19 位写会漏。 @@ -69,6 +73,20 @@ def aweme_id_from_url(value: str) -> str: return "" +async def _detail(aweme_id: str) -> Dict[str, Any]: + """取一次作品详情,网络层抖动(DNS/连接/超时)重试一次。 + + 实测容器刚起来时第一发会撞上 ``[Errno -3] Temporary failure in name resolution``, + 隔半秒就正常 —— 批量解析时这种一次性的失败只会让某一行莫名其妙地红掉,用户还得 + 整段重贴。**「200 + 空 body」不重试**:那是风控信号,重试只会让它更确定。 + """ + try: + return await douyin_api.video_detail(aweme_id) + except httpx.TransportError: + await asyncio.sleep(_DETAIL_RETRY_SLEEP_SECONDS) + return await douyin_api.video_detail(aweme_id) + + async def _aweme_id(value: str) -> str: """短链先展开,再抠 id。""" expanded = value @@ -125,7 +143,7 @@ async def resolve(text: str) -> Dict[str, Any]: continue try: - record = await douyin_api.video_detail(aweme_id) + record = await _detail(aweme_id) except Exception as exc: item["error"] = str(exc) or "接口没有返回这条作品" continue diff --git a/tests/test_monitor_video_link.py b/tests/test_monitor_video_link.py index ea070b2..70ae42c 100644 --- a/tests/test_monitor_video_link.py +++ b/tests/test_monitor_video_link.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- """视频链接 → 博主主页:链接抠取、id 解析、批量解析的容错。""" +import httpx import pytest from api.monitor import douyin_api, service, video_link @@ -149,6 +150,43 @@ async def test_a_missing_sec_uid_is_reported_not_faked(monkeypatch): assert "sec_uid" in item["error"] +@pytest.mark.asyncio +async def test_a_transport_hiccup_is_retried_once(monkeypatch): + """容器刚起来时第一发可能撞上 DNS 失败;重试一次就过去了,别让它红掉一行。""" + calls = [] + + async def flaky_detail(aweme_id, **_): + calls.append(aweme_id) + if len(calls) == 1: + raise httpx.ConnectError("[Errno -3] Temporary failure in name resolution") + return {"aweme_id": aweme_id, "nickname": "锋哥", "creator_id": SEC_UID} + + monkeypatch.setattr(douyin_api, "video_detail", flaky_detail) + monkeypatch.setattr(video_link, "_DETAIL_RETRY_SLEEP_SECONDS", 0) + + result = await video_link.resolve("https://www.douyin.com/video/7353137957861084427") + + assert len(calls) == 2 + assert result["results"][0]["ok"] is True + + +@pytest.mark.asyncio +async def test_an_empty_body_is_not_retried(monkeypatch): + """空 body 是风控信号 —— 重试只会让它更确定。""" + calls = [] + + async def blocked_detail(aweme_id, **_): + calls.append(aweme_id) + raise douyin_api.DouyinApiError("接口返回了空内容") + + monkeypatch.setattr(douyin_api, "video_detail", blocked_detail) + + result = await video_link.resolve("https://www.douyin.com/video/7353137957861084427") + + assert len(calls) == 1 + assert result["results"][0]["ok"] is False + + @pytest.mark.asyncio async def test_more_than_the_limit_is_truncated(monkeypatch): async def fake_detail(aweme_id, **_):