perf(douyu): 拆分任务槽并改造绑定扫码调度

This commit is contained in:
yml
2026-11-20 22:03:20 +08:00
parent 8869e427d2
commit 071d45ad1d
40 changed files with 1635 additions and 245 deletions
+3 -20
View File
@@ -1,7 +1,7 @@
"""斗鱼后台任务的进程级并发限制。
"""斗鱼批次级并发限制。
任务线程和数据库连接池共享同一个 Uvicorn 进程。这里把「每批并发」和
「全进程同时执行数」分开限制,避免多个批次叠加后把请求线程和连接池挤空。
Process-wide execution is owned by ``task_runtime`` lanes; this module only
keeps the per-batch cap and the overview sub-limit.
"""
from __future__ import annotations
@@ -23,16 +23,11 @@ DOUYU_BATCH_TASK_CONCURRENCY = _env_positive_int(
"DOUYU_BATCH_TASK_CONCURRENCY",
5,
)
DOUYU_MAX_RUNNING_TASKS = _env_positive_int(
"DOUYU_MAX_RUNNING_TASKS",
12,
)
DOUYU_MAX_ACTIVE_OVERVIEW_TASKS = _env_positive_int(
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
2,
)
_task_slots = threading.BoundedSemaphore(DOUYU_MAX_RUNNING_TASKS)
_overview_slots = threading.BoundedSemaphore(DOUYU_MAX_ACTIVE_OVERVIEW_TASKS)
@@ -47,15 +42,6 @@ def _acquire(slots: threading.BoundedSemaphore, stopped: Callable[[], bool]) ->
return False
def acquire_running_task_slot(stopped: Callable[[], bool]) -> bool:
"""等待一个全进程任务槽;停止时返回 False。"""
return _acquire(_task_slots, stopped)
def release_running_task_slot() -> None:
_task_slots.release()
def acquire_overview_slot(stopped: Callable[[], bool]) -> bool:
"""等待一个一键查询槽;该限制跨所有批次共享。"""
return _acquire(_overview_slots, stopped)
@@ -68,10 +54,7 @@ def release_overview_slot() -> None:
__all__ = [
"DOUYU_BATCH_TASK_CONCURRENCY",
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
"DOUYU_MAX_RUNNING_TASKS",
"acquire_overview_slot",
"acquire_running_task_slot",
"clamp_batch_task_concurrency",
"release_overview_slot",
"release_running_task_slot",
]