fix(platform): 防止绑定任务压垮连接池和事件循环

This commit is contained in:
yml
2026-11-21 15:22:46 +08:00
parent d199e04d27
commit e1d979a296
13 changed files with 336 additions and 46 deletions
+77 -14
View File
@@ -19,6 +19,7 @@ from typing import Any
class TaskLane(StrEnum):
INTERACTIVE_BINDING = "interactive_binding"
BATCH_COORDINATOR = "batch_coordinator"
BINDING_POLL = "binding_poll"
LOGIN = "login"
ACCOUNT_CHECK = "account_check"
@@ -51,6 +52,7 @@ class LaneConfig:
_DEFAULT_WORKERS: dict[TaskLane, int] = {
TaskLane.INTERACTIVE_BINDING: 3,
TaskLane.BATCH_COORDINATOR: 2,
TaskLane.BINDING_POLL: 2,
TaskLane.LOGIN: 4,
TaskLane.ACCOUNT_CHECK: 4,
@@ -71,9 +73,19 @@ _DEFAULT_WORKERS: dict[TaskLane, int] = {
def _lane_config(lane: TaskLane) -> LaneConfig:
key = lane.value.upper()
workers = _env_int(f"TASK_LANE_{key}_WORKERS", _DEFAULT_WORKERS[lane])
default_in_flight = (
workers
if lane
in {
TaskLane.BATCH_COORDINATOR,
TaskLane.BINDING_POLL,
TaskLane.PAYMENT_POLL,
}
else max(workers * 32, workers)
)
max_in_flight = _env_int(
f"TASK_LANE_{key}_MAX_IN_FLIGHT",
max(workers * 32, workers),
default_in_flight,
)
return LaneConfig(workers=workers, max_in_flight=max(max_in_flight, workers))
@@ -107,6 +119,36 @@ def _runtime(lane: TaskLane) -> _LaneRuntime:
return runtime
def _submit[T](
lane: TaskLane,
fn: Callable[..., T],
/,
*args: Any,
stopped: Callable[[], bool] | None = None,
wait: bool,
**kwargs: Any,
) -> Future[T] | None:
runtime = _runtime(lane)
if stopped is not None and stopped():
return None
if wait:
while True:
if stopped is not None and stopped():
return None
if runtime.admission.acquire(timeout=0.2):
break
elif not runtime.admission.acquire(blocking=False):
return None
def run() -> T:
try:
return fn(*args, **kwargs)
finally:
runtime.admission.release()
return runtime.executor.submit(run)
def submit[T](
lane: TaskLane,
fn: Callable[..., T],
@@ -120,20 +162,39 @@ def submit[T](
Batch coordinators are cheap threads and are allowed to block here. Worker
threads themselves must not recursively submit to the same lane.
"""
return _submit(
lane,
fn,
*args,
stopped=stopped,
wait=True,
**kwargs,
)
def try_submit[T](
lane: TaskLane,
fn: Callable[..., T],
/,
*args: Any,
stopped: Callable[[], bool] | None = None,
**kwargs: Any,
) -> Future[T] | None:
"""Submit without blocking when the lane admission budget is full."""
return _submit(
lane,
fn,
*args,
stopped=stopped,
wait=False,
**kwargs,
)
def available_slots(lane: TaskLane) -> int:
"""Return the number of tasks that can currently enter a lane immediately."""
runtime = _runtime(lane)
while True:
if stopped is not None and stopped():
return None
if runtime.admission.acquire(timeout=0.2):
break
def run() -> T:
try:
return fn(*args, **kwargs)
finally:
runtime.admission.release()
return runtime.executor.submit(run)
return max(0, int(runtime.admission._value))
def lane_stats() -> dict[str, dict[str, int]]:
@@ -194,8 +255,10 @@ def douyu_lane_for_task(task_type: str) -> TaskLane:
__all__ = [
"TaskLane",
"available_slots",
"douyu_lane_for_task",
"lane_stats",
"shutdown",
"submit",
"try_submit",
]