UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

103 lines (102 loc) • 3.81 kB
//#region src/gateway/node-wake-state-store.ts const nodeWakeStateByOwner = /* @__PURE__ */ new Map(); const nodeWakeOwnerBySignal = /* @__PURE__ */ new WeakMap(); function nodeWakeStateKey(e, t) { return JSON.stringify([e.trim(), t?.trim() || null]); } //#endregion //#region src/gateway/node-wake-state.ts const NODE_WAKE_RECONNECT_WAIT_MS = 3e3; const NODE_WAKE_RECONNECT_RETRY_WAIT_MS = 12e3; function getOrCreateNodeWakeOwner(e, t) { const n = e.trim(); const r = nodeWakeStateKey(n, t); const i = nodeWakeStateByOwner.get(r); if (i) return i; const a = { nodeId: n, stateKey: r }; nodeWakeStateByOwner.set(r, a); return a; } function deleteIdleNodeWakeOwner(e) { if (e.lifecycle?.users || e.inFlightWake || e.lastWakeAtMs !== void 0 || e.lastNudgeAtMs !== void 0) return; e.lifecycle?.controller.abort(); if (e.lifecycle) nodeWakeOwnerBySignal.delete(e.lifecycle.controller.signal); nodeWakeStateByOwner.delete(e.stateKey); } function captureNodeWakeLifecycle(e, t) { const n = getOrCreateNodeWakeOwner(e, t); if (!n.lifecycle || n.lifecycle.controller.signal.aborted) { n.lifecycle = { controller: new AbortController(), users: 0 }; nodeWakeOwnerBySignal.set(n.lifecycle.controller.signal, n); } n.lifecycle.users += 1; return n.lifecycle.controller.signal; } function isNodeWakeLifecycleCurrent(e, t, n) { const r = nodeWakeOwnerBySignal.get(t); const i = nodeWakeStateKey(e, n); return !t.aborted && r?.nodeId === e.trim() && r.stateKey === i && nodeWakeStateByOwner.get(i) === r && r.lifecycle?.controller.signal === t; } function releaseNodeWakeLifecycle(e, t) { const n = nodeWakeOwnerBySignal.get(t); if (n?.nodeId !== e.trim() || nodeWakeStateByOwner.get(n.stateKey) !== n || n.lifecycle?.controller.signal !== t) return; n.lifecycle.users = Math.max(0, n.lifecycle.users - 1); deleteIdleNodeWakeOwner(n); } /** Owns wake dedupe and throttle state while the caller owns APNs policy and I/O. */ async function runNodeWakeAttempt(e) { const t = getOrCreateNodeWakeOwner(e.nodeId, e.pairingGeneration); if (t.inFlightWake) return await t.inFlightWake; if (!e.force && t.lastWakeAtMs !== void 0 && performance.now() - t.lastWakeAtMs < e.throttleMs) return { available: true, throttled: true, path: "throttled", durationMs: 0 }; const n = e.attempt(() => { t.lastWakeAtMs = performance.now(); }); t.inFlightWake = n; try { return await n; } finally { if (t.inFlightWake === n) t.inFlightWake = void 0; deleteIdleNodeWakeOwner(t); } } /** Owns reconnect-nudge throttling while the caller owns APNs policy and I/O. */ async function runNodeWakeNudgeAttempt(e) { const t = getOrCreateNodeWakeOwner(e.nodeId, e.pairingGeneration); if (t.lastNudgeAtMs !== void 0 && performance.now() - t.lastNudgeAtMs < e.throttleMs) return e.throttled(); const n = await e.attempt(); if (n.reason === "sent") t.lastNudgeAtMs = performance.now(); deleteIdleNodeWakeOwner(t); return n; } function clearNodeWakeState(e) { const t = e.trim(); for (const e of nodeWakeStateByOwner.values()) { if (e.nodeId !== t) continue; e.lastWakeAtMs = void 0; e.inFlightWake = void 0; e.lastNudgeAtMs = void 0; deleteIdleNodeWakeOwner(e); } } function invalidateNodeWakeState(e) { const t = e.trim(); for (const e of nodeWakeStateByOwner.values()) { if (e.nodeId !== t) continue; e.lifecycle?.controller.abort(); if (e.lifecycle) nodeWakeOwnerBySignal.delete(e.lifecycle.controller.signal); nodeWakeStateByOwner.delete(e.stateKey); } } //#endregion export { invalidateNodeWakeState as a, runNodeWakeAttempt as c, clearNodeWakeState as i, runNodeWakeNudgeAttempt as l, NODE_WAKE_RECONNECT_WAIT_MS as n, isNodeWakeLifecycleCurrent as o, captureNodeWakeLifecycle as r, releaseNodeWakeLifecycle as s, NODE_WAKE_RECONNECT_RETRY_WAIT_MS as t };