feat(gateway): add websocket pong timeout and channel auto-reconnect watchdog (#569)
This commit is contained in:
committed by
GitHub
Unverified
parent
554f894493
commit
1eda50ef44
@@ -4,18 +4,45 @@ type HealthResult = { ok: boolean; error?: string };
|
||||
|
||||
export class GatewayConnectionMonitor {
|
||||
private pingInterval: NodeJS.Timeout | null = null;
|
||||
private pongTimeout: NodeJS.Timeout | null = null;
|
||||
private healthCheckInterval: NodeJS.Timeout | null = null;
|
||||
|
||||
startPing(sendPing: () => void, intervalMs = 30000): void {
|
||||
startPing(
|
||||
sendPing: () => void,
|
||||
onPongTimeout?: () => void,
|
||||
intervalMs = 30000,
|
||||
timeoutMs = 15000,
|
||||
): void {
|
||||
if (this.pingInterval) {
|
||||
clearInterval(this.pingInterval);
|
||||
}
|
||||
if (this.pongTimeout) {
|
||||
clearTimeout(this.pongTimeout);
|
||||
this.pongTimeout = null;
|
||||
}
|
||||
|
||||
this.pingInterval = setInterval(() => {
|
||||
sendPing();
|
||||
|
||||
if (onPongTimeout) {
|
||||
if (this.pongTimeout) {
|
||||
clearTimeout(this.pongTimeout);
|
||||
}
|
||||
this.pongTimeout = setTimeout(() => {
|
||||
this.pongTimeout = null;
|
||||
onPongTimeout();
|
||||
}, timeoutMs);
|
||||
}
|
||||
}, intervalMs);
|
||||
}
|
||||
|
||||
handlePong(): void {
|
||||
if (this.pongTimeout) {
|
||||
clearTimeout(this.pongTimeout);
|
||||
this.pongTimeout = null;
|
||||
}
|
||||
}
|
||||
|
||||
startHealthCheck(options: {
|
||||
shouldCheck: () => boolean;
|
||||
checkHealth: () => Promise<HealthResult>;
|
||||
@@ -51,6 +78,10 @@ export class GatewayConnectionMonitor {
|
||||
clearInterval(this.pingInterval);
|
||||
this.pingInterval = null;
|
||||
}
|
||||
if (this.pongTimeout) {
|
||||
clearTimeout(this.pongTimeout);
|
||||
this.pongTimeout = null;
|
||||
}
|
||||
if (this.healthCheckInterval) {
|
||||
clearInterval(this.healthCheckInterval);
|
||||
this.healthCheckInterval = null;
|
||||
|
||||
@@ -109,6 +109,8 @@ export class GatewayManager extends EventEmitter {
|
||||
private reconnectAttemptsTotal = 0;
|
||||
private reconnectSuccessTotal = 0;
|
||||
private static readonly RELOAD_POLICY_REFRESH_MS = 15_000;
|
||||
public static readonly RESTART_COOLDOWN_MS = 5_000;
|
||||
private lastRestartAt = 0;
|
||||
|
||||
constructor(config?: Partial<ReconnectConfig>) {
|
||||
super();
|
||||
@@ -727,6 +729,9 @@ export class GatewayManager extends EventEmitter {
|
||||
getToken: async () => await import('../utils/store').then(({ getSetting }) => getSetting('gatewayToken')),
|
||||
onHandshakeComplete: (ws) => {
|
||||
this.ws = ws;
|
||||
this.ws.on('pong', () => {
|
||||
this.connectionMonitor.handlePong();
|
||||
});
|
||||
this.setStatus({
|
||||
state: 'running',
|
||||
port,
|
||||
@@ -802,11 +807,24 @@ export class GatewayManager extends EventEmitter {
|
||||
* Start ping interval to keep connection alive
|
||||
*/
|
||||
private startPing(): void {
|
||||
this.connectionMonitor.startPing(() => {
|
||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||
this.ws.ping();
|
||||
this.connectionMonitor.startPing(
|
||||
() => {
|
||||
if (this.ws?.readyState === WebSocket.OPEN) {
|
||||
this.ws.ping();
|
||||
}
|
||||
},
|
||||
() => {
|
||||
logger.error('Gateway WebSocket dead connection detected (pong timeout)');
|
||||
if (this.ws) {
|
||||
this.ws.terminate(); // Force close the dead connection immediately
|
||||
this.ws = null;
|
||||
}
|
||||
if (this.status.state === 'running') {
|
||||
this.setStatus({ state: 'error', error: 'WebSocket ping timeout' });
|
||||
this.scheduleReconnect();
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user