fix(douyu): 修复斗鱼链路 P0/P1 隐患并增强 P2 运维队列可观测性

This commit is contained in:
yml
2026-11-29 14:10:24 +08:00
parent be7146a9ef
commit d12d9e02b3
51 changed files with 2510 additions and 561 deletions
@@ -24,6 +24,17 @@ _thread: threading.Thread | None = None
_lock = threading.Lock()
_in_flight: set[int] = set()
_in_flight_lock = threading.Lock()
_metrics_lock = threading.Lock()
_metrics: dict[str, object] = {
"last_tick_at": None,
"last_dispatched_at": None,
"last_claimed": 0,
"last_submitted": 0,
"total_dispatched": 0,
"consecutive_errors": 0,
"last_error": None,
"last_error_at": None,
}
def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
@@ -128,6 +139,9 @@ def _release_callback(_future: Future) -> None:
def run_binding_poll_dispatch_once(limit: int = DEFAULT_BATCH_SIZE) -> int:
capacity = available_slots(TaskLane.BINDING_POLL)
if capacity <= 0:
with _metrics_lock:
_metrics["last_claimed"] = 0
_metrics["last_submitted"] = 0
return 0
claimed = _claim_due_binding_tasks(min(max(1, limit), capacity))
submitted = 0
@@ -144,6 +158,12 @@ def run_binding_poll_dispatch_once(limit: int = DEFAULT_BATCH_SIZE) -> int:
break
future.add_done_callback(_release_callback)
submitted += 1
with _metrics_lock:
_metrics["last_claimed"] = len(claimed)
_metrics["last_submitted"] = submitted
if submitted:
_metrics["total_dispatched"] = int(_metrics["total_dispatched"] or 0) + submitted
_metrics["last_dispatched_at"] = datetime.now(UTC).isoformat()
return submitted
@@ -155,6 +175,9 @@ def _run() -> None:
30,
)
while not _stop_event.is_set():
now_iso = datetime.now(UTC).isoformat()
with _metrics_lock:
_metrics["last_tick_at"] = now_iso
try:
batch_size = _env_int(
"BINDING_POLL_DISPATCH_BATCH",
@@ -163,7 +186,15 @@ def _run() -> None:
200,
)
run_binding_poll_dispatch_once(batch_size)
with _metrics_lock:
_metrics["consecutive_errors"] = 0
except Exception as exc: # noqa: BLE001 - scheduler must survive one bad tick
with _metrics_lock:
_metrics["consecutive_errors"] = (
int(_metrics["consecutive_errors"] or 0) + 1
)
_metrics["last_error"] = f"{type(exc).__name__}: {exc}"[:500]
_metrics["last_error_at"] = now_iso
logger.warning("绑定扫码调度轮次失败: {}", exc)
_stop_event.wait(interval)
@@ -199,10 +230,13 @@ def scheduler_status() -> dict[str, object]:
running = _thread is not None and _thread.is_alive()
with _in_flight_lock:
in_flight = len(_in_flight)
with _metrics_lock:
metrics = dict(_metrics)
return {
"running": running,
"in_flight": in_flight,
"claim_seconds": CLAIM_SECONDS,
**metrics,
}