From 61c961c1a4d9ac28a501a6937d5aa92515ecf385 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 5 Oct 2023 13:56:02 -0400 Subject: [PATCH 1/3] feat: throttle socket attempts and set max attempts --- package.json | 2 +- src/store/actions/socket.js | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 9e576021..433d0591 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cadt-ui", - "version": "1.2.7", + "version": "1.2.8", "private": true, "author": "Chia Network Inc. ", "homepage": "./", diff --git a/src/store/actions/socket.js b/src/store/actions/socket.js index 51ed4d05..f4b8a345 100644 --- a/src/store/actions/socket.js +++ b/src/store/actions/socket.js @@ -24,6 +24,7 @@ export const SOCKET_STATUS = keyMirror( let socket; let interval; let reconnectInterval = 1000; +let reconnectAttempts = 0; const notifyRefresh = _.debounce(dispatch => { NotificationManager.info( @@ -33,7 +34,7 @@ const notifyRefresh = _.debounce(dispatch => { () => dispatch(refreshApp(true)), true, ); -}, 100); +}, 1000); const initListenersForEachMessageType = dispatch => { Object.keys(messageTypes).forEach(key => { @@ -84,19 +85,28 @@ export const emitAction = actionCreator => { }; const reconnectSocket = _.debounce(dispatch => { - if (!socket || (socket && !socket.connected)) { + if (reconnectAttempts <= 10 && (!socket || !socket.connected)) { + console.log( + 'Attempting to reconnect to socket server...', + reconnectAttempts, + ); dispatch(initiateSocket()); setTimeout(() => { - if (!socket || (socket && !socket.connected)) { + if (!socket || !socket.connected) { reconnectSocket(dispatch); } - reconnectInterval = reconnectInterval * 2; + reconnectInterval *= 2; }, reconnectInterval); + } else if (reconnectAttempts > 10) { + console.log("Couldn't connect to socket server. Max attempts reached."); + dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE)); + clearInterval(interval); } -}, 100); +}, 1000); export const initiateSocket = remoteHost => { disconnectSocket(); + reconnectAttempts++; const WS_HOST = `${remoteHost || constants.API_HOST}/ws`; const transports = ['websocket']; @@ -158,6 +168,7 @@ export const initiateSocket = remoteHost => { console.log('Attempting to connect to socket_id: ', socket.id); console.log('### Socket Connected ###'); dispatch(setSocketStatus(SOCKET_STATUS.CONNECTED)); + reconnectAttempts = 0; socket.emit('authentication'); }); From 6782ba5de650f24064ff9616415f0c4e30b39650 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 5 Oct 2023 14:00:33 -0400 Subject: [PATCH 2/3] feat: additional socket tweak: --- src/store/actions/socket.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/store/actions/socket.js b/src/store/actions/socket.js index f4b8a345..24db53bc 100644 --- a/src/store/actions/socket.js +++ b/src/store/actions/socket.js @@ -175,10 +175,14 @@ export const initiateSocket = remoteHost => { initListenersForEachMessageType(dispatch); interval = setInterval(() => { - if (!socket || (socket && !socket.connected)) { + if (reconnectAttempts <= 10 && (!socket || !socket.connected)) { dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE)); clearInterval(interval); reconnectSocket(dispatch); + } else if (reconnectAttempts > 10) { + console.log("Couldn't connect to socket server. Max attempts reached."); + dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE)); + clearInterval(interval); } }, 5000); }; From 8131c47b3c7b07014413e107e10e2e27a085d278 Mon Sep 17 00:00:00 2001 From: Michael Taylor Date: Thu, 5 Oct 2023 14:16:20 -0400 Subject: [PATCH 3/3] fix: socket tweaks --- src/store/actions/socket.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/store/actions/socket.js b/src/store/actions/socket.js index 24db53bc..a2785e11 100644 --- a/src/store/actions/socket.js +++ b/src/store/actions/socket.js @@ -100,12 +100,18 @@ const reconnectSocket = _.debounce(dispatch => { } else if (reconnectAttempts > 10) { console.log("Couldn't connect to socket server. Max attempts reached."); dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE)); + clearInterval(interval); } }, 1000); export const initiateSocket = remoteHost => { disconnectSocket(); + + if (reconnectAttempts > 10) { + return; + } + reconnectAttempts++; const WS_HOST = `${remoteHost || constants.API_HOST}/ws`; @@ -183,6 +189,7 @@ export const initiateSocket = remoteHost => { console.log("Couldn't connect to socket server. Max attempts reached."); dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE)); clearInterval(interval); + disconnectSocket(); } }, 5000); };