From b978b3bc2f8beb099632465dc8b00c03af10b338 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Mon, 5 Jun 2023 11:00:14 -0400 Subject: [PATCH] add optional ws ping (#370) --- lib/config.js | 3 ++- lib/utils/ws-requestor.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/config.js b/lib/config.js index 7dd1b1d3..268eab83 100644 --- a/lib/config.js +++ b/lib/config.js @@ -51,7 +51,7 @@ const JAMBONES_SBCS = process.env.JAMBONES_SBCS; /* websockets */ const JAMBONES_WS_HANDSHAKE_TIMEOUT_MS = parseInt(process.env.JAMBONES_WS_HANDSHAKE_TIMEOUT_MS, 10) || 1500; const JAMBONES_WS_MAX_PAYLOAD = parseInt(process.env.JAMBONES_WS_MAX_PAYLOAD, 10) || 24 * 1024; - +const JAMBONES_WS_PING_INTERVAL_MS = parseInt(process.env.JAMBONES_WS_PING_INTERVAL_MS, 10) || 0; const MAX_RECONNECTS = 5; const RESPONSE_TIMEOUT_MS = parseInt(process.env.JAMBONES_WS_API_MSG_RESPONSE_TIMEOUT, 10) || 5000; @@ -186,6 +186,7 @@ module.exports = { RESPONSE_TIMEOUT_MS, JAMBONES_WS_HANDSHAKE_TIMEOUT_MS, JAMBONES_WS_MAX_PAYLOAD, + JAMBONES_WS_PING_INTERVAL_MS, MAX_RECONNECTS, GCP_JSON_KEY, MICROSOFT_REGION, diff --git a/lib/utils/ws-requestor.js b/lib/utils/ws-requestor.js index ffc58903..2c0f14d4 100644 --- a/lib/utils/ws-requestor.js +++ b/lib/utils/ws-requestor.js @@ -6,6 +6,7 @@ const Websocket = require('ws'); const snakeCaseKeys = require('./snakecase-keys'); const { RESPONSE_TIMEOUT_MS, + JAMBONES_WS_PING_INTERVAL_MS, MAX_RECONNECTS, JAMBONES_WS_HANDSHAKE_TIMEOUT_MS, JAMBONES_WS_MAX_PAYLOAD @@ -178,9 +179,17 @@ class WsRequestor extends BaseRequestor { }); } + _stopPingTimer() { + if (this._pingTimer) { + clearInterval(this._pingTimer); + this._pingTimer = null; + } + } + close() { this.closedGracefully = true; this.logger.debug('WsRequestor:close closing socket'); + this._stopPingTimer(); try { if (this.ws) { this.ws.close(1000); @@ -195,6 +204,7 @@ class WsRequestor extends BaseRequestor { _connect() { assert(!this.ws); + this._stopPingTimer(); return new Promise((resolve, reject) => { const handshakeTimeout = JAMBONES_WS_HANDSHAKE_TIMEOUT_MS ? parseInt(JAMBONES_WS_HANDSHAKE_TIMEOUT_MS) : @@ -255,10 +265,15 @@ class WsRequestor extends BaseRequestor { this.connectInProgress = false; this.connections++; this.emit('ready', ws); + + if (JAMBONES_WS_PING_INTERVAL_MS > 15000) { + this._pingTimer = setInterval(() => this.ws?.ping(), JAMBONES_WS_PING_INTERVAL_MS); + } } _onClose(code) { this.logger.info(`WsRequestor(${this.id}) - closed from far end ${code}`); + this._stopPingTimer(); if (this.connections > 0 && code !== 1000) { this.logger.info({url: this.url}, 'WsRequestor - socket closed unexpectedly from remote side'); this.emit('socket-closed'); @@ -283,6 +298,7 @@ class WsRequestor extends BaseRequestor { _onSocketClosed() { this.ws = null; this.emit('connection-dropped'); + this._stopPingTimer(); if (this.connections > 0 && this.connections < MAX_RECONNECTS && !this.closedGracefully) { if (!this._initMsgId) this._clearPendingMessages(); this.logger.debug(`WsRequestor:_onSocketClosed waiting ${this.backoffMs} to reconnect`);