From 971861010485f0ce7b77d24491a7cbe6fb84d8c7 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 11 Nov 2024 16:26:30 +0000 Subject: [PATCH 1/3] regularly ping acceleration websocket server --- backend/src/api/services/acceleration.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/src/api/services/acceleration.ts b/backend/src/api/services/acceleration.ts index 5fa6dc554..cb84ab8dc 100644 --- a/backend/src/api/services/acceleration.ts +++ b/backend/src/api/services/acceleration.ts @@ -49,6 +49,7 @@ class AccelerationApi { private websocketPath = config.MEMPOOL_SERVICES?.API ? `${config.MEMPOOL_SERVICES.API.replace('https://', 'wss://').replace('http://', 'ws://')}/accelerator/ws` : '/'; private _accelerations: Record = {}; private lastPoll = 0; + private lastPing = 0; private forcePoll = false; private myAccelerations: Record = {}; @@ -267,12 +268,27 @@ class AccelerationApi { this.ws.on('message', (data, isBinary) => { try { - const parsedMsg = JSON.parse((isBinary ? data : data.toString()) as string); + const msg = (isBinary ? data : data.toString()) as string; + const parsedMsg = msg?.length ? JSON.parse(msg) : null; this.handleWebsocketMessage(parsedMsg); } catch (e) { logger.warn('Failed to parse acceleration websocket message: ' + (e instanceof Error ? e.message : e)); } }); + + this.ws.on('ping', () => { + logger.debug('received ping from acceleration websocket server'); + }); + + this.ws.on('pong', () => { + logger.debug('received pong from acceleration websocket server'); + }); + } else { + if (Date.now() - this.lastPing > 30000) { + logger.debug('sending ping to acceleration websocket server'); + this.ws.ping(); + this.lastPing = Date.now(); + } } await new Promise(resolve => setTimeout(resolve, 5000)); } From ef498b55edf43f82c6da39253872e64229121dd8 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Mon, 11 Nov 2024 16:59:08 +0000 Subject: [PATCH 2/3] reset acceleration websocket if unresponsive --- backend/src/api/services/acceleration.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/src/api/services/acceleration.ts b/backend/src/api/services/acceleration.ts index cb84ab8dc..e86f0f200 100644 --- a/backend/src/api/services/acceleration.ts +++ b/backend/src/api/services/acceleration.ts @@ -50,6 +50,7 @@ class AccelerationApi { private _accelerations: Record = {}; private lastPoll = 0; private lastPing = 0; + private lastPong = 0; private forcePoll = false; private myAccelerations: Record = {}; @@ -284,7 +285,12 @@ class AccelerationApi { logger.debug('received pong from acceleration websocket server'); }); } else { - if (Date.now() - this.lastPing > 30000) { + if (this.lastPing > this.lastPong && Date.now() - this.lastPing > 5000) { + logger.warn('No pong received within 5 seconds, terminating connection'); + this.ws.terminate(); + this.ws = null; + this.websocketConnected = false; + } else if (Date.now() - this.lastPing > 30000) { logger.debug('sending ping to acceleration websocket server'); this.ws.ping(); this.lastPing = Date.now(); From 8719b424e5cbd8e6ec2c2bee3bd49a5452c70152 Mon Sep 17 00:00:00 2001 From: wiz Date: Tue, 12 Nov 2024 02:03:56 +0900 Subject: [PATCH 3/3] Increase websocket from 5s to 10s --- backend/src/api/services/acceleration.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/api/services/acceleration.ts b/backend/src/api/services/acceleration.ts index e86f0f200..4e0d87b5a 100644 --- a/backend/src/api/services/acceleration.ts +++ b/backend/src/api/services/acceleration.ts @@ -285,8 +285,8 @@ class AccelerationApi { logger.debug('received pong from acceleration websocket server'); }); } else { - if (this.lastPing > this.lastPong && Date.now() - this.lastPing > 5000) { - logger.warn('No pong received within 5 seconds, terminating connection'); + if (this.lastPing > this.lastPong && Date.now() - this.lastPing > 10000) { + logger.warn('No pong received within 10 seconds, terminating connection'); this.ws.terminate(); this.ws = null; this.websocketConnected = false;