Skip to content

Commit

Permalink
fix: use exponential strategy for hanging duration
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Sep 5, 2024
1 parent 9422a34 commit 0187ca6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/client/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Socket {
// ----------------------------------------------------------
return new Promise(resolve => {
let connectionRefusedLogged = false;
let hangingTimeoutDuration = 5; // 5, 10, 20, 40

const trySocket = async () => {
const ws = new WebSocket(url, webSocketOptions);
Expand All @@ -118,7 +119,9 @@ class Socket {
const hangingTimeoutId = setTimeout(() => {
ws.terminate ? ws.terminate() : ws.close();
trySocket();
}, 5 * 1000);
}, hangingTimeoutDuration * 1000);
// exponentialy increase hangingTimeoutDuration on each try and clamp at 40sec
hangingTimeoutDuration = Math.min(hangingTimeoutDuration * 2, 40);

ws.addEventListener('open', openEvent => {
clearTimeout(hangingTimeoutId);
Expand Down

0 comments on commit 0187ca6

Please sign in to comment.