fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性
This commit is contained in:
@@ -12,7 +12,7 @@ import os
|
||||
import threading
|
||||
from collections.abc import Callable
|
||||
from concurrent.futures import Future, ThreadPoolExecutor
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from enum import StrEnum
|
||||
from typing import Any
|
||||
|
||||
@@ -97,6 +97,12 @@ class _LaneRuntime:
|
||||
config: LaneConfig
|
||||
executor: ThreadPoolExecutor
|
||||
admission: threading.BoundedSemaphore
|
||||
lock: threading.Lock = field(default_factory=threading.Lock)
|
||||
in_flight: int = 0
|
||||
active: int = 0
|
||||
waiting: int = 0
|
||||
submitted: int = 0
|
||||
rejected: int = 0
|
||||
|
||||
|
||||
_LOCK = threading.RLock()
|
||||
@@ -134,21 +140,44 @@ def _submit[T](
|
||||
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
|
||||
with runtime.lock:
|
||||
runtime.waiting += 1
|
||||
try:
|
||||
while True:
|
||||
if stopped is not None and stopped():
|
||||
return None
|
||||
if runtime.admission.acquire(timeout=0.2):
|
||||
break
|
||||
finally:
|
||||
with runtime.lock:
|
||||
runtime.waiting = max(0, runtime.waiting - 1)
|
||||
elif not runtime.admission.acquire(blocking=False):
|
||||
with runtime.lock:
|
||||
runtime.rejected += 1
|
||||
return None
|
||||
|
||||
with runtime.lock:
|
||||
runtime.in_flight += 1
|
||||
runtime.submitted += 1
|
||||
|
||||
def run() -> T:
|
||||
with runtime.lock:
|
||||
runtime.active += 1
|
||||
try:
|
||||
return fn(*args, **kwargs)
|
||||
finally:
|
||||
with runtime.lock:
|
||||
runtime.active = max(0, runtime.active - 1)
|
||||
runtime.in_flight = max(0, runtime.in_flight - 1)
|
||||
runtime.admission.release()
|
||||
|
||||
return runtime.executor.submit(run)
|
||||
try:
|
||||
return runtime.executor.submit(run)
|
||||
except Exception:
|
||||
with runtime.lock:
|
||||
runtime.in_flight = max(0, runtime.in_flight - 1)
|
||||
runtime.admission.release()
|
||||
raise
|
||||
|
||||
|
||||
def submit[T](
|
||||
@@ -196,7 +225,8 @@ def try_submit[T](
|
||||
def available_slots(lane: TaskLane) -> int:
|
||||
"""Return the number of tasks that can currently enter a lane immediately."""
|
||||
runtime = _runtime(lane)
|
||||
return max(0, int(runtime.admission._value))
|
||||
with runtime.lock:
|
||||
return max(0, runtime.config.max_in_flight - runtime.in_flight)
|
||||
|
||||
|
||||
def lane_stats() -> dict[str, dict[str, int]]:
|
||||
@@ -204,10 +234,23 @@ def lane_stats() -> dict[str, dict[str, int]]:
|
||||
runtimes = list(_RUNTIMES.items())
|
||||
stats: dict[str, dict[str, int]] = {}
|
||||
for lane, runtime in runtimes:
|
||||
with runtime.lock:
|
||||
in_flight = runtime.in_flight
|
||||
active = runtime.active
|
||||
waiting = runtime.waiting
|
||||
submitted = runtime.submitted
|
||||
rejected = runtime.rejected
|
||||
available = max(0, runtime.config.max_in_flight - in_flight)
|
||||
stats[lane.value] = {
|
||||
"workers": runtime.config.workers,
|
||||
"max_in_flight": runtime.config.max_in_flight,
|
||||
"admission_value": runtime.admission._value,
|
||||
"admission_value": available,
|
||||
"in_flight": in_flight,
|
||||
"active": active,
|
||||
"queued": max(0, in_flight - active),
|
||||
"waiting": waiting,
|
||||
"submitted": submitted,
|
||||
"rejected": rejected,
|
||||
}
|
||||
return stats
|
||||
|
||||
|
||||
Reference in New Issue
Block a user