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 00bab85 commit 90fa70f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ class Client {
this.#socket = new ClientSocket(this.#role, this.#config, {
path: 'socket',
retryConnectionRefusedTimeout: 1000,
retryHangingTimeout: 5 * 1000,
});
this.#contextManager = new ClientContextManager();
this.#pluginManager = new ClientPluginManager(this);
Expand Down
6 changes: 5 additions & 1 deletion src/client/ClientSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class ClientSocket {
// ----------------------------------------------------------
return new Promise(resolve => {
let connectionRefusedLogged = false;
let hangingTimeoutDuration = 5;

const trySocket = async () => {
const ws = new WebSocket(url, webSocketOptions);
Expand All @@ -124,7 +125,10 @@ class ClientSocket {
const hangingTimeoutId = setTimeout(() => {
ws.terminate ? ws.terminate() : ws.close();
trySocket();
}, this.#socketOptions.retryHangingTimeout);
}, hangingTimeoutDuration * 1000);
// Exponentialy increase hangingTimeoutDuration on each try and clamp at 40sec
// i.e.: 5, 10, 20, 40, 40, 40, ...
hangingTimeoutDuration = Math.min(hangingTimeoutDuration * 2, 40);

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

0 comments on commit 90fa70f

Please sign in to comment.