perf(douyu): 拆分支付轮询并扩展查询并发
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import inspect
|
||||
import threading
|
||||
import time
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
@@ -19,13 +17,18 @@ from web.backend.services import (
|
||||
business_events,
|
||||
douyu_concurrency,
|
||||
douyu_service,
|
||||
payment_poll_scheduler,
|
||||
)
|
||||
from web.backend.services.douyu_runner import DouyuBatchRunner
|
||||
from web.backend.services.douyu_runner_core import (
|
||||
DouyuBatchRunnerCore,
|
||||
slim_douyu_task_result_for_storage,
|
||||
)
|
||||
from web.backend.services.task_runtime import TaskLane, douyu_lane_for_task
|
||||
from web.backend.services.task_runtime import (
|
||||
_DEFAULT_WORKERS,
|
||||
TaskLane,
|
||||
douyu_lane_for_task,
|
||||
)
|
||||
|
||||
|
||||
def _fixtures(db, *, status: str = "planned") -> tuple[User, Account, DouyuTask]:
|
||||
@@ -183,6 +186,26 @@ def test_cleanup_preserves_recoverable_binding_poll(db):
|
||||
assert task.queue_lane == "binding_poll"
|
||||
|
||||
|
||||
def test_cleanup_preserves_recoverable_payment_poll(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
task.task_type = "create_gold_qr"
|
||||
task.queue_lane = "payment_poll"
|
||||
task.next_run_at = datetime.now(UTC) + timedelta(seconds=5)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
db.commit()
|
||||
|
||||
cleaned = douyu_service.cleanup_orphan_douyu_tasks(
|
||||
db,
|
||||
active_batch_ids=set(),
|
||||
statuses=("running",),
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert cleaned == 0
|
||||
assert task.status == "running"
|
||||
assert task.queue_lane == "payment_poll"
|
||||
|
||||
|
||||
def test_binding_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
due_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
@@ -202,6 +225,47 @@ def test_binding_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
binding_poll_scheduler._in_flight.discard(task.id)
|
||||
|
||||
|
||||
def test_payment_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
due_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
task.task_type = "create_gold_qr"
|
||||
task.queue_lane = "payment_poll"
|
||||
task.next_run_at = due_at
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
task.result = {"pay_url": "https://example.test/pay"}
|
||||
db.commit()
|
||||
|
||||
try:
|
||||
claimed = payment_poll_scheduler._claim_due_payment_tasks(10)
|
||||
db.refresh(task)
|
||||
assert claimed == [task.id]
|
||||
assert task.next_run_at is not None
|
||||
assert task.next_run_at > due_at
|
||||
assert task.attempt_count == 1
|
||||
finally:
|
||||
with payment_poll_scheduler._in_flight_lock:
|
||||
payment_poll_scheduler._in_flight.discard(task.id)
|
||||
|
||||
|
||||
def test_payment_poll_marks_expired_task_failed(db, monkeypatch):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
task.task_type = "create_elite_qr"
|
||||
task.queue_lane = "payment_poll"
|
||||
task.next_run_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
task.run_deadline_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
task.result = {"pay_url": "https://example.test/pay"}
|
||||
db.commit()
|
||||
|
||||
monkeypatch.setattr(payment_poll_scheduler, "WorkerSessionLocal", make_test_session)
|
||||
payment_poll_scheduler.poll_payment_task_once(task.id)
|
||||
|
||||
db.rollback()
|
||||
db.refresh(task)
|
||||
assert task.status == "failed"
|
||||
assert task.queue_lane == ""
|
||||
assert "超时" in task.message
|
||||
|
||||
|
||||
def test_bind_qr_schedules_poll_and_releases_worker(db, monkeypatch):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
runner = DouyuBatchRunner(
|
||||
@@ -375,49 +439,57 @@ def test_active_batch_budget_returns_429(monkeypatch):
|
||||
assert exc_info.value.status_code == 429
|
||||
|
||||
|
||||
def test_douyu_batch_request_rejects_more_than_five_workers():
|
||||
def test_douyu_batch_request_allows_ten_workers_for_payment_qr():
|
||||
req = DouyuTaskBatchRequest(
|
||||
account_ids=[1],
|
||||
task_type="create_gold_qr",
|
||||
handbook_scope="elite",
|
||||
concurrency=10,
|
||||
)
|
||||
assert req.concurrency == 10
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
DouyuTaskBatchRequest(
|
||||
account_ids=[1],
|
||||
task_type="query_points",
|
||||
task_type="create_gold_qr",
|
||||
handbook_scope="elite",
|
||||
concurrency=6,
|
||||
concurrency=11,
|
||||
)
|
||||
|
||||
|
||||
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_runner_applies_task_specific_concurrency(db):
|
||||
payment = DouyuBatchRunner(
|
||||
db,
|
||||
"payment-batch",
|
||||
"create_gold_qr",
|
||||
concurrency=10,
|
||||
close_db=False,
|
||||
)
|
||||
overview = DouyuBatchRunner(
|
||||
db,
|
||||
"overview-batch",
|
||||
"query_account_overview",
|
||||
concurrency=10,
|
||||
close_db=False,
|
||||
)
|
||||
assert payment.concurrency == 10
|
||||
assert overview.concurrency == 5
|
||||
|
||||
|
||||
def test_douyu_lanes_separate_interactive_query_payment_and_writes():
|
||||
assert douyu_lane_for_task("get_bind_qr") == TaskLane.INTERACTIVE_BINDING
|
||||
assert douyu_lane_for_task("query_account_overview") == TaskLane.QUERY_BATCH
|
||||
assert douyu_lane_for_task("create_gold_qr") == TaskLane.PAYMENT_WAIT
|
||||
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
|
||||
|
||||
|
||||
def test_overview_limiter_blocks_extra_workers(monkeypatch):
|
||||
slots = threading.BoundedSemaphore(1)
|
||||
monkeypatch.setattr(douyu_concurrency, "_overview_slots", slots)
|
||||
acquired: list[bool] = []
|
||||
started = threading.Event()
|
||||
|
||||
assert douyu_concurrency.acquire_overview_slot(lambda: False) is True
|
||||
|
||||
def worker():
|
||||
started.set()
|
||||
acquired.append(douyu_concurrency.acquire_overview_slot(lambda: False))
|
||||
|
||||
thread = threading.Thread(target=worker)
|
||||
thread.start()
|
||||
started.wait(timeout=1)
|
||||
time.sleep(0.05)
|
||||
assert acquired == []
|
||||
|
||||
douyu_concurrency.release_overview_slot()
|
||||
thread.join(timeout=1)
|
||||
assert acquired == [True]
|
||||
douyu_concurrency.release_overview_slot()
|
||||
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10
|
||||
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3
|
||||
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5
|
||||
|
||||
|
||||
def test_slim_task_result_removes_debug_payloads():
|
||||
|
||||
Reference in New Issue
Block a user