61 lines
1.5 KiB
Python
61 lines
1.5 KiB
Python
"""斗鱼批次级并发限制。
|
|
|
|
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
|
|
|
|
import os
|
|
import threading
|
|
from collections.abc import Callable
|
|
|
|
|
|
def _env_positive_int(name: str, default: int) -> int:
|
|
try:
|
|
value = int(os.getenv(name, str(default)))
|
|
except ValueError:
|
|
return default
|
|
return value if value > 0 else default
|
|
|
|
|
|
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,
|
|
)
|
|
|
|
_overview_slots = threading.BoundedSemaphore(DOUYU_MAX_ACTIVE_OVERVIEW_TASKS)
|
|
|
|
|
|
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()
|
|
|
|
|
|
__all__ = [
|
|
"DOUYU_BATCH_TASK_CONCURRENCY",
|
|
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
|
|
"acquire_overview_slot",
|
|
"clamp_batch_task_concurrency",
|
|
"release_overview_slot",
|
|
]
|