fix(monitor): 超级编导查询复用连接池,别再每个请求重解析 DNS
实测容器里解析 api.superdir.cn 要 5 秒,而每个请求都新建 httpx.AsyncClient 就要重新解析 + 握手:一次查询几十个请求,58~90 秒都见过,界面上一直转。 一次查询共用一条连接池后同一主机走 keep-alive,实测 0.1 秒级。
This commit is contained in:
@@ -62,13 +62,13 @@ class TestPreview:
|
||||
async def test_only_activities_created_in_the_range_are_fetched(self, monkeypatch):
|
||||
fetched: list = []
|
||||
|
||||
async def fake_activities(token):
|
||||
async def fake_activities(token, client):
|
||||
return [
|
||||
_activity(1, date(2026, 10, 19), "区间外"),
|
||||
_activity(2, date(2026, 10, 21), "目标活动"),
|
||||
]
|
||||
|
||||
async def fake_videos(token, activity_id):
|
||||
async def fake_videos(token, activity_id, client):
|
||||
fetched.append(activity_id)
|
||||
return [_video(AWEME_A)]
|
||||
|
||||
@@ -88,10 +88,10 @@ class TestPreview:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_unpublished_and_private_videos_are_skipped(self, monkeypatch):
|
||||
async def fake_activities(token):
|
||||
async def fake_activities(token, client):
|
||||
return [_activity(2, date(2026, 10, 21))]
|
||||
|
||||
async def fake_videos(token, activity_id):
|
||||
async def fake_videos(token, activity_id, client):
|
||||
return [
|
||||
_video(AWEME_A),
|
||||
# 还没发布:没有 share_url
|
||||
@@ -116,13 +116,13 @@ class TestPreview:
|
||||
async def test_the_same_video_under_two_activities_is_one_row(self, monkeypatch):
|
||||
"""分发活动被复制过,同一件作品会挂在两个活动下面 —— 建任务时不能重复。"""
|
||||
|
||||
async def fake_activities(token):
|
||||
async def fake_activities(token, client):
|
||||
return [
|
||||
_activity(2, date(2026, 10, 21)),
|
||||
_activity(3, date(2026, 10, 21)),
|
||||
]
|
||||
|
||||
async def fake_videos(token, activity_id):
|
||||
async def fake_videos(token, activity_id, client):
|
||||
return [_video(AWEME_A)]
|
||||
|
||||
async def fake_token(session, platform=superdir.adapters.PLATFORM_DY):
|
||||
@@ -138,13 +138,13 @@ class TestPreview:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_one_broken_activity_does_not_sink_the_preview(self, monkeypatch):
|
||||
async def fake_activities(token):
|
||||
async def fake_activities(token, client):
|
||||
return [
|
||||
_activity(2, date(2026, 10, 21), "坏的"),
|
||||
_activity(3, date(2026, 10, 21), "好的"),
|
||||
]
|
||||
|
||||
async def fake_videos(token, activity_id):
|
||||
async def fake_videos(token, activity_id, client):
|
||||
if activity_id == 2:
|
||||
raise superdir.SuperDirError(500, "取作品失败")
|
||||
return [_video(AWEME_A)]
|
||||
@@ -163,10 +163,10 @@ class TestPreview:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_more_than_the_limit_is_truncated_not_silently_dropped(self, monkeypatch):
|
||||
async def fake_activities(token):
|
||||
async def fake_activities(token, client):
|
||||
return [_activity(2, date(2026, 10, 21))]
|
||||
|
||||
async def fake_videos(token, activity_id):
|
||||
async def fake_videos(token, activity_id, client):
|
||||
return [_video(str(7000000000000000000 + i)) for i in range(3)]
|
||||
|
||||
async def fake_token(session, platform=superdir.adapters.PLATFORM_DY):
|
||||
@@ -211,7 +211,7 @@ class TestPagination:
|
||||
async def test_pages_stop_at_total(self, monkeypatch):
|
||||
calls: list = []
|
||||
|
||||
async def fake_request(path, params, token):
|
||||
async def fake_request(path, params, token, client):
|
||||
calls.append(params["page"])
|
||||
if params["page"] == 1:
|
||||
return {"items": [{"id": 1}, {"id": 2}], "total": 3}
|
||||
@@ -220,7 +220,7 @@ class TestPagination:
|
||||
monkeypatch.setattr(superdir, "_request", fake_request)
|
||||
|
||||
items = await superdir._paginate(
|
||||
"/x", {}, "token", page_size=2, max_pages=10
|
||||
"/x", {}, "token", None, page_size=2, max_pages=10
|
||||
)
|
||||
|
||||
assert [item["id"] for item in items] == [1, 2, 3]
|
||||
@@ -233,7 +233,7 @@ class TestActivityPaging:
|
||||
"""活动列表没有日期过滤,每次查询都要把整个历史翻完 —— 一页都不能少。"""
|
||||
calls: list = []
|
||||
|
||||
async def fake_request(path, params, token):
|
||||
async def fake_request(path, params, token, client):
|
||||
calls.append(params["page"])
|
||||
pages = {
|
||||
1: [{"id": 1}, {"id": 2}],
|
||||
@@ -245,7 +245,7 @@ class TestActivityPaging:
|
||||
monkeypatch.setattr(superdir, "_request", fake_request)
|
||||
monkeypatch.setattr(superdir, "ACTIVITY_PAGE_SIZE", 2)
|
||||
|
||||
items = await superdir._fetch_activities("token")
|
||||
items = await superdir._fetch_activities("token", None)
|
||||
|
||||
assert sorted(calls) == [1, 2, 3]
|
||||
assert [item["id"] for item in items] == [1, 2, 3, 4, 5]
|
||||
@@ -254,7 +254,7 @@ class TestActivityPaging:
|
||||
async def test_a_failed_page_fails_the_whole_list_not_a_partial_one(self, monkeypatch):
|
||||
"""少一页 = 少一批活动,看上去却像「这段时间就这些」—— 必须报错。"""
|
||||
|
||||
async def fake_request(path, params, token):
|
||||
async def fake_request(path, params, token, client):
|
||||
if params["page"] == 2:
|
||||
raise superdir.SuperDirError(500, "这一页没取到")
|
||||
return {"items": [{"id": 1}, {"id": 2}], "total": 5}
|
||||
@@ -263,7 +263,7 @@ class TestActivityPaging:
|
||||
monkeypatch.setattr(superdir, "ACTIVITY_PAGE_SIZE", 2)
|
||||
|
||||
with pytest.raises(superdir.SuperDirError):
|
||||
await superdir._fetch_activities("token")
|
||||
await superdir._fetch_activities("token", None)
|
||||
|
||||
|
||||
class TestErrorHints:
|
||||
|
||||
Reference in New Issue
Block a user