|
| 1 | +import type { RuntimeEnv } from "../../runtime.js"; |
| 2 | + |
| 3 | +export type StallWatchdogTimeoutMeta = { |
| 4 | + idleMs: number; |
| 5 | + timeoutMs: number; |
| 6 | +}; |
| 7 | + |
| 8 | +export type ArmableStallWatchdog = { |
| 9 | + arm: (atMs?: number) => void; |
| 10 | + touch: (atMs?: number) => void; |
| 11 | + disarm: () => void; |
| 12 | + stop: () => void; |
| 13 | + isArmed: () => boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +export function createArmableStallWatchdog(params: { |
| 17 | + label: string; |
| 18 | + timeoutMs: number; |
| 19 | + checkIntervalMs?: number; |
| 20 | + abortSignal?: AbortSignal; |
| 21 | + runtime?: RuntimeEnv; |
| 22 | + onTimeout: (meta: StallWatchdogTimeoutMeta) => void; |
| 23 | +}): ArmableStallWatchdog { |
| 24 | + const timeoutMs = Math.max(1, Math.floor(params.timeoutMs)); |
| 25 | + const checkIntervalMs = Math.max( |
| 26 | + 100, |
| 27 | + Math.floor(params.checkIntervalMs ?? Math.min(5_000, Math.max(250, timeoutMs / 6))), |
| 28 | + ); |
| 29 | + |
| 30 | + let armed = false; |
| 31 | + let stopped = false; |
| 32 | + let lastActivityAt = Date.now(); |
| 33 | + let timer: ReturnType<typeof setInterval> | null = null; |
| 34 | + |
| 35 | + const clearTimer = () => { |
| 36 | + if (!timer) { |
| 37 | + return; |
| 38 | + } |
| 39 | + clearInterval(timer); |
| 40 | + timer = null; |
| 41 | + }; |
| 42 | + |
| 43 | + const disarm = () => { |
| 44 | + armed = false; |
| 45 | + }; |
| 46 | + |
| 47 | + const stop = () => { |
| 48 | + if (stopped) { |
| 49 | + return; |
| 50 | + } |
| 51 | + stopped = true; |
| 52 | + disarm(); |
| 53 | + clearTimer(); |
| 54 | + params.abortSignal?.removeEventListener("abort", stop); |
| 55 | + }; |
| 56 | + |
| 57 | + const arm = (atMs?: number) => { |
| 58 | + if (stopped) { |
| 59 | + return; |
| 60 | + } |
| 61 | + lastActivityAt = atMs ?? Date.now(); |
| 62 | + armed = true; |
| 63 | + }; |
| 64 | + |
| 65 | + const touch = (atMs?: number) => { |
| 66 | + if (stopped) { |
| 67 | + return; |
| 68 | + } |
| 69 | + lastActivityAt = atMs ?? Date.now(); |
| 70 | + }; |
| 71 | + |
| 72 | + const check = () => { |
| 73 | + if (!armed || stopped) { |
| 74 | + return; |
| 75 | + } |
| 76 | + const now = Date.now(); |
| 77 | + const idleMs = now - lastActivityAt; |
| 78 | + if (idleMs < timeoutMs) { |
| 79 | + return; |
| 80 | + } |
| 81 | + disarm(); |
| 82 | + params.runtime?.error?.( |
| 83 | + `[${params.label}] transport watchdog timeout: idle ${Math.round(idleMs / 1000)}s (limit ${Math.round(timeoutMs / 1000)}s)`, |
| 84 | + ); |
| 85 | + params.onTimeout({ idleMs, timeoutMs }); |
| 86 | + }; |
| 87 | + |
| 88 | + if (params.abortSignal?.aborted) { |
| 89 | + stop(); |
| 90 | + } else { |
| 91 | + params.abortSignal?.addEventListener("abort", stop, { once: true }); |
| 92 | + timer = setInterval(check, checkIntervalMs); |
| 93 | + timer.unref?.(); |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + arm, |
| 98 | + touch, |
| 99 | + disarm, |
| 100 | + stop, |
| 101 | + isArmed: () => armed, |
| 102 | + }; |
| 103 | +} |
0 commit comments