Skip to content

Commit 6642278

Browse files
phpbgsamuelchemlaleibale
authored
Add 'Network error handling' section to documentation (#2250)
* Add 'Network error handling' section to documentation * Merge 'Network error handling' section with existing doc * typo * Update README.md * typos Co-authored-by: Samuel CHEMLA <samuel.chemla@orange.com> Co-authored-by: Leibale Eidelman <me@leibale.com>
1 parent abf2b4b commit 6642278

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { createClient } from 'redis';
5353

5454
const client = createClient();
5555

56-
client.on('error', (err) => console.log('Redis Client Error', err));
56+
client.on('error', err => console.log('Redis Client Error', err));
5757

5858
await client.connect();
5959

docs/client-configuration.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,29 @@
2929

3030
## Reconnect Strategy
3131

32-
You can implement a custom reconnect strategy as a function:
32+
When a network error occurs the client will automatically try to reconnect, following a default linear strategy (the more attempts, the more waiting before trying to reconnect).
33+
34+
This strategy can be overridden by providing a `socket.reconnectStrategy` option during the client's creation.
35+
36+
The `socket.reconnectStrategy` is a function that:
3337

3438
- Receives the number of retries attempted so far.
3539
- Returns `number | Error`:
3640
- `number`: wait time in milliseconds prior to attempting a reconnect.
3741
- `Error`: closes the client and flushes internal command queues.
3842

43+
The example below shows the default `reconnectStrategy` and how to override it.
44+
45+
```typescript
46+
import { createClient } from 'redis';
47+
48+
const client = createClient({
49+
socket: {
50+
reconnectStrategy: (retries) => Math.min(retries * 50, 500)
51+
}
52+
});
53+
```
54+
3955
## TLS
4056

4157
To enable TLS, set `socket.tls` to `true`. Below are some basic examples.

0 commit comments

Comments
 (0)