Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default reconnect strategy uses exponential backoff and jitter #2736

Open
wants to merge 5 commits into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/client-configuration.md
Expand Up @@ -13,7 +13,7 @@
| socket.keepAlive | `true` | Toggle [`keep-alive`](https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay) functionality |
| socket.keepAliveInitialDelay | `5000` | If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket |
| socket.tls | | See explanation and examples [below](#TLS) |
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
| socket.reconnectStrategy | `((retries^2) * 50 ms) + 0-200 ms` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ^ operator is "Bitwise XOR" in JavaScript, not power.. we need to find a better way to document this..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with a plain English explanation of the strategy in 38eb603, rather than try to figure out how to pseudo-code this in a legible way.

| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
| password | | ACL password or the old "--requirepass" password |
| name | | Client name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname)) |
Expand All @@ -35,12 +35,19 @@ When the socket closes unexpectedly (without calling `.quit()`/`.disconnect()`),
2. `number` -> wait for `X` milliseconds before reconnecting.
3. `(retries: number, cause: Error) => false | number | Error` -> `number` is the same as configuring a `number` directly, `Error` is the same as `false`, but with a custom error.

By default the strategy is `Math.min(retries * 50, 500)`, but it can be overwritten like so:
By default the strategy uses exponential backoff, but it can be overwritten like so:

```javascript
createClient({
socket: {
reconnectStrategy: retries => Math.min(retries * 50, 1000)
reconnectStrategy: retries => {
// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
leibale marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}
}
});
```
Expand Down
13 changes: 11 additions & 2 deletions packages/client/lib/client/socket.ts
Expand Up @@ -82,6 +82,15 @@ export default class RedisSocket extends EventEmitter {
}

#createReconnectStrategy(options?: RedisSocketOptions): ReconnectStrategyFunction {
const defaultStrategy = (retries: number) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be moved to a #defaultReconnectStrategy function and used from lines 109 and 114

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in e7304ad 👍

// 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;
}

const strategy = options?.reconnectStrategy;
if (strategy === false || typeof strategy === 'number') {
return () => strategy;
Expand All @@ -97,12 +106,12 @@ export default class RedisSocket extends EventEmitter {
return retryIn;
} catch (err) {
this.emit('error', err);
return Math.min(retries * 50, 500);
return defaultStrategy(retries);
}
};
}

return retries => Math.min(retries * 50, 500);
return defaultStrategy;
}

#createSocketFactory(options?: RedisSocketOptions) {
Expand Down