1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

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>
This commit is contained in:
Samuel CHEMLA
2023-01-24 15:15:14 +01:00
committed by GitHub
parent abf2b4bc82
commit 6642278f96
2 changed files with 18 additions and 2 deletions

View File

@@ -53,7 +53,7 @@ import { createClient } from 'redis';
const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();

View File

@@ -29,13 +29,29 @@
## Reconnect Strategy
You can implement a custom reconnect strategy as a function:
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).
This strategy can be overridden by providing a `socket.reconnectStrategy` option during the client's creation.
The `socket.reconnectStrategy` is a function that:
- Receives the number of retries attempted so far.
- Returns `number | Error`:
- `number`: wait time in milliseconds prior to attempting a reconnect.
- `Error`: closes the client and flushes internal command queues.
The example below shows the default `reconnectStrategy` and how to override it.
```typescript
import { createClient } from 'redis';
const client = createClient({
socket: {
reconnectStrategy: (retries) => Math.min(retries * 50, 500)
}
});
```
## TLS
To enable TLS, set `socket.tls` to `true`. Below are some basic examples.