1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Default reconnect strategy uses exponential backoff and jitter (#2736)

* Default reconnect strategy uses exponential backoff and jitter

Both are recommended parts of client reconnect strategies to prevent
thundering herd problems when many clients lose their connection at once
(for example, during a Redis upgrade).

* Move default retry strategy to constant

* Plain english explanation of default 'socket.reconnectStrategy'

* Extract default connect strategy into helper function
This commit is contained in:
John Olmsted
2024-05-28 10:16:52 -04:00
committed by GitHub
parent f9252356ae
commit 31c881e90e
2 changed files with 21 additions and 5 deletions

View File

@@ -97,12 +97,12 @@ export default class RedisSocket extends EventEmitter {
return retryIn;
} catch (err) {
this.emit('error', err);
return Math.min(retries * 50, 500);
return this.defaultReconnectStrategy(retries);
}
};
}
return retries => Math.min(retries * 50, 500);
return this.defaultReconnectStrategy;
}
#createSocketFactory(options?: RedisSocketOptions) {
@@ -333,4 +333,13 @@ export default class RedisSocket extends EventEmitter {
this.#isSocketUnrefed = true;
this.#socket?.unref();
}
defaultReconnectStrategy(retries: number) {
// Generate a random jitter between 0 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
return delay + jitter;
}
}