perf(douyu): 拆分支付轮询并扩展查询并发

This commit is contained in:
yml
2026-11-21 10:38:39 +08:00
parent 743fa69630
commit 3987464035
21 changed files with 1151 additions and 531 deletions
+24 -26
View File
@@ -1,14 +1,12 @@
"""斗鱼批次级并发限制。
"""斗鱼批次与任务类型级并发限制。
Process-wide execution is owned by ``task_runtime`` lanes; this module only
keeps the per-batch cap and the overview sub-limit.
Process-wide execution is owned by ``task_runtime`` lanes; this module keeps
per-task-type admission caps before work reaches those lanes.
"""
from __future__ import annotations
import os
import threading
from collections.abc import Callable
def _env_positive_int(name: str, default: int) -> int:
@@ -23,38 +21,38 @@ DOUYU_BATCH_TASK_CONCURRENCY = _env_positive_int(
"DOUYU_BATCH_TASK_CONCURRENCY",
5,
)
DOUYU_MAX_ACTIVE_OVERVIEW_TASKS = _env_positive_int(
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
2,
DOUYU_PAYMENT_QR_CONCURRENCY = _env_positive_int(
"DOUYU_PAYMENT_QR_CONCURRENCY",
10,
)
_overview_slots = threading.BoundedSemaphore(DOUYU_MAX_ACTIVE_OVERVIEW_TASKS)
PAYMENT_QR_TASK_TYPES = frozenset(
{
"create_elite_qr",
"create_esports_qr",
"create_gold_qr",
}
)
def clamp_batch_task_concurrency(value: int) -> int:
return max(1, min(int(value), DOUYU_BATCH_TASK_CONCURRENCY))
def _acquire(slots: threading.BoundedSemaphore, stopped: Callable[[], bool]) -> bool:
while not stopped():
if slots.acquire(timeout=0.2):
return True
return False
def acquire_overview_slot(stopped: Callable[[], bool]) -> bool:
"""等待一个一键查询槽;该限制跨所有批次共享。"""
return _acquire(_overview_slots, stopped)
def release_overview_slot() -> None:
_overview_slots.release()
def clamp_douyu_task_concurrency(task_type: str, value: int) -> int:
"""按任务类型限制批次内并发,不把支付出码压回普通查询的上限。"""
limit = (
DOUYU_PAYMENT_QR_CONCURRENCY
if task_type in PAYMENT_QR_TASK_TYPES
else DOUYU_BATCH_TASK_CONCURRENCY
)
return max(1, min(int(value), limit))
__all__ = [
"DOUYU_BATCH_TASK_CONCURRENCY",
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
"acquire_overview_slot",
"DOUYU_PAYMENT_QR_CONCURRENCY",
"PAYMENT_QR_TASK_TYPES",
"clamp_batch_task_concurrency",
"release_overview_slot",
"clamp_douyu_task_concurrency",
]