UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

52 lines (50 loc) 1.65 kB
import { ConnectMessage, GatewayMessageType } from "../../../../proto/src/components/connect/protobuf/connect.js"; import { ensureUnsharedArrayBuffer } from "../../buffer.js"; import { WAKE_REASON } from "./types.js"; //#region src/components/connect/strategies/core/heartbeat.ts var HeartbeatManager = class { interval; intervalMs = 1e4; onHeartbeatSent; constructor(accessor, wakeSignal, logger) { this.accessor = accessor; this.wakeSignal = wakeSignal; this.logger = logger; } /** * Update the heartbeat interval. Restarts the timer if the interval changed * or if it wasn't running yet. */ updateInterval(ms) { if (ms === this.intervalMs && this.interval) return; this.intervalMs = ms; this.stop(); this.start(); } /** Stop the heartbeat timer. */ stop() { clearInterval(this.interval); this.interval = void 0; } start() { if (this.interval) return; this.interval = setInterval(() => this.tick(), this.intervalMs); } tick() { const conn = this.accessor.activeConnection; if (!conn || conn.ws.readyState !== WebSocket.OPEN) return; if (conn.pendingHeartbeats >= 2) { this.logger.warn({ connectionId: conn.id }, "Consecutive heartbeats missed, reconnecting"); conn.dead = true; this.wakeSignal.wake(WAKE_REASON.HeartbeatMissed); return; } conn.pendingHeartbeats++; conn.ws.send(ensureUnsharedArrayBuffer(ConnectMessage.encode(ConnectMessage.create({ kind: GatewayMessageType.WORKER_HEARTBEAT })).finish())); this.logger.debug({ connectionId: conn.id }, "Heartbeat sent"); this.onHeartbeatSent?.(); } }; //#endregion export { HeartbeatManager }; //# sourceMappingURL=heartbeat.js.map