59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
"""斗鱼批次与任务类型级并发限制。
|
|
|
|
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
|
|
|
|
|
|
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_PAYMENT_QR_CONCURRENCY = _env_positive_int(
|
|
"DOUYU_PAYMENT_QR_CONCURRENCY",
|
|
10,
|
|
)
|
|
|
|
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 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_PAYMENT_QR_CONCURRENCY",
|
|
"PAYMENT_QR_TASK_TYPES",
|
|
"clamp_batch_task_concurrency",
|
|
"clamp_douyu_task_concurrency",
|
|
]
|