perf(worker): 调高斗鱼任务与批次执行并发默认上限

This commit is contained in:
yml
2026-11-28 14:23:57 +08:00
parent 2013a9bdeb
commit 82f2cc21ac
11 changed files with 96 additions and 112 deletions
+36 -49
View File
@@ -1,4 +1,5 @@
import inspect
import os
from datetime import UTC, datetime, timedelta
import pytest
@@ -489,9 +490,7 @@ def test_batch_launcher_defers_database_session_to_worker(monkeypatch):
monkeypatch.setattr(
douyu_batch_launcher,
"WorkerSessionLocal",
lambda: (_ for _ in ()).throw(
AssertionError("队列阶段不能创建数据库 Session")
),
lambda: (_ for _ in ()).throw(AssertionError("队列阶段不能创建数据库 Session")),
)
monkeypatch.setattr(douyu_batch_launcher, "submit", fake_submit)
@@ -962,9 +961,7 @@ def test_confirm_bind_maps_cooldown_and_keeps_old_role(
account.game_name = old_role
account.role_bind_status = "bound"
db.commit()
can_change_time = int(
(datetime.now(UTC) + timedelta(days=2)).timestamp()
)
can_change_time = int((datetime.now(UTC) + timedelta(days=2)).timestamp())
old_bound = {
"act_alias": "20260120QYOOB",
"role_name": old_role,
@@ -1044,9 +1041,7 @@ def test_confirm_bind_maps_cooldown_and_keeps_old_role(
assert account.role_bind_status == "bound"
def test_qr_preflight_accepts_existing_action_alias_pending_from_har(
db, monkeypatch
):
def test_qr_preflight_accepts_existing_action_alias_pending_from_har(db, monkeypatch):
"""2026-11-22 成功 HAR:已有 bindAct=0 角色无需等新一轮状态变化。"""
_, account, task = _fixtures(db, status="running")
role = "忧郁情潇藏心中"
@@ -1110,9 +1105,7 @@ def test_qr_preflight_accepts_existing_action_alias_pending_from_har(
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
def test_qr_preflight_keeps_bound_role_waiting_for_new_scan_transition(
db, monkeypatch
):
def test_qr_preflight_keeps_bound_role_waiting_for_new_scan_transition(db, monkeypatch):
"""历史 bindAct=1 + needBindAct=1 不能直接进入待确认。"""
_, account, task = _fixtures(db, status="running")
role = "你哥奢皇"
@@ -1181,15 +1174,11 @@ def test_qr_preflight_keeps_bound_role_waiting_for_new_scan_transition(
assert action_decision["reason"] == "already_bound"
def test_qr_preflight_blocks_platform_account_change_cooldown_from_har(
db, monkeypatch
):
def test_qr_preflight_blocks_platform_account_change_cooldown_from_har(db, monkeypatch):
"""2026-11-22 失败 HAR:canChangeTime 限制当前斗鱼号,发码前就必须拦截。"""
_, account, task = _fixtures(db, status="running")
role = "狗鼻子弗尼亚bd"
can_change_time = int(
(datetime.now(UTC) + timedelta(days=2)).timestamp()
)
can_change_time = int((datetime.now(UTC) + timedelta(days=2)).timestamp())
activity = {
"act_alias": "20260120QYOOB",
"role_name": role,
@@ -1252,9 +1241,7 @@ def test_qr_preflight_blocks_platform_account_change_cooldown_from_har(
assert account.role_bind_status == "bound"
def test_binding_poll_accepts_existing_action_alias_pending_same_role(
db, monkeypatch
):
def test_binding_poll_accepts_existing_action_alias_pending_same_role(db, monkeypatch):
"""活动 alias 的同名待确认角色不能被 legacy(cjm) 的旧快照压掉。"""
_, account, task = _fixtures(db, status="running")
role = "忧郁情潇藏心中"
@@ -1322,17 +1309,14 @@ def test_binding_poll_accepts_existing_action_alias_pending_same_role(
assert task.result["bind_decision"]["alias"] == "20260120QYOOB"
assert task.result["bind_decision"]["reason"] == "same_alias_after_bound_transition"
assert {
row["alias"]: row["reason"]
for row in task.result["bind_candidate_decisions"]
row["alias"]: row["reason"] for row in task.result["bind_candidate_decisions"]
} == {
"cjm": "same_role_stale",
"20260120QYOOB": "same_alias_after_bound_transition",
}
def test_binding_poll_accepts_unchanged_existing_action_alias_pending(
db, monkeypatch
):
def test_binding_poll_accepts_unchanged_existing_action_alias_pending(db, monkeypatch):
"""2026-11-22 成功 HAR:已有动作 alias pending 不要求本轮状态变化。"""
_, account, task = _fixtures(db, status="running")
role = "只为爱杰宝"
@@ -1404,9 +1388,7 @@ def test_binding_poll_accepts_unchanged_existing_action_alias_pending(
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
def test_workbench_rebind_detects_new_cjm_scan_without_expected_role(
db, monkeypatch
):
def test_workbench_rebind_detects_new_cjm_scan_without_expected_role(db, monkeypatch):
"""HAR 11.22 用户中心换绑:已绑定账号发码后只有 cjm 变化,工作台轮询也必须识别。"""
_, account, task = _fixtures(db, status="running")
bound_activity = {
@@ -1586,9 +1568,7 @@ def test_binding_poll_empty_baseline_preserves_need_bind_act_on_candidates(
assert pending_binding_role_from_result(task.result) == "欧美真心"
def test_binding_poll_preserves_concurrently_written_expected_role(
db, monkeypatch
):
def test_binding_poll_preserves_concurrently_written_expected_role(db, monkeypatch):
"""轮询写回 task.result 时,不能覆盖客户并发提交进来的 expected_role_name。"""
_, account, task = _fixtures(db, status="running")
task.queue_lane = "binding_poll"
@@ -1807,47 +1787,51 @@ def test_active_batch_budget_returns_429(monkeypatch):
assert exc_info.value.status_code == 429
def test_douyu_batch_request_allows_ten_workers_for_payment_qr():
def test_douyu_batch_request_allows_fifteen_workers_for_payment_qr():
req = DouyuTaskBatchRequest(
account_ids=[1],
task_type="create_gold_qr",
handbook_scope="elite",
concurrency=10,
concurrency=15,
)
assert req.concurrency == 10
assert req.concurrency == 15
with pytest.raises(ValidationError):
DouyuTaskBatchRequest(
account_ids=[1],
task_type="create_gold_qr",
handbook_scope="elite",
concurrency=11,
concurrency=16,
)
def test_batch_task_concurrency_is_capped_at_five():
assert douyu_concurrency.clamp_batch_task_concurrency(20) == 5
assert douyu_concurrency.clamp_douyu_task_concurrency("query_points", 20) == 5
assert douyu_concurrency.clamp_douyu_task_concurrency("create_gold_qr", 20) == 10
def test_batch_task_concurrency_is_capped_at_ten(monkeypatch):
monkeypatch.setattr(douyu_concurrency, "DOUYU_BATCH_TASK_CONCURRENCY", 10)
monkeypatch.setattr(douyu_concurrency, "DOUYU_PAYMENT_QR_CONCURRENCY", 15)
assert douyu_concurrency.clamp_batch_task_concurrency(20) == 10
assert douyu_concurrency.clamp_douyu_task_concurrency("query_points", 20) == 10
assert douyu_concurrency.clamp_douyu_task_concurrency("create_gold_qr", 20) == 15
def test_runner_applies_task_specific_concurrency(db):
def test_runner_applies_task_specific_concurrency(db, monkeypatch):
monkeypatch.setattr(douyu_concurrency, "DOUYU_BATCH_TASK_CONCURRENCY", 10)
monkeypatch.setattr(douyu_concurrency, "DOUYU_PAYMENT_QR_CONCURRENCY", 15)
payment = DouyuBatchRunner(
db,
"payment-batch",
"create_gold_qr",
concurrency=10,
concurrency=15,
close_db=False,
)
overview = DouyuBatchRunner(
db,
"overview-batch",
"query_account_overview",
concurrency=10,
concurrency=15,
close_db=False,
)
assert payment.concurrency == 10
assert overview.concurrency == 5
assert payment.concurrency == 15
assert overview.concurrency == 10
def test_douyu_lanes_separate_interactive_query_payment_and_writes():
@@ -1855,10 +1839,13 @@ def test_douyu_lanes_separate_interactive_query_payment_and_writes():
assert douyu_lane_for_task("query_account_overview") == TaskLane.OVERVIEW_QUERY
assert douyu_lane_for_task("create_gold_qr") == TaskLane.PAYMENT_QR
assert douyu_lane_for_task("exchange_goods") == TaskLane.OPS_WRITE
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5
assert _DEFAULT_WORKERS[TaskLane.BINDING_POLL] == 6
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 12
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 6
expected_overview = max(1, int(os.getenv("DOUYU_MAX_ACTIVE_OVERVIEW_TASKS") or 10))
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == expected_overview
assert _DEFAULT_WORKERS[TaskLane.QUERY_BATCH] == 10
assert _DEFAULT_WORKERS[TaskLane.OPS_WRITE] == 6
assert _DEFAULT_WORKERS[TaskLane.BINDING_POLL] == 12
def test_slim_task_result_removes_debug_payloads():