From 6642278f96503a393371740373bbb9f9df9823e7 Mon Sep 17 00:00:00 2001 From: Samuel CHEMLA <43315561+phpbg@users.noreply.github.com> Date: Tue, 24 Jan 2023 15:15:14 +0100 Subject: [PATCH] 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 Co-authored-by: Leibale Eidelman --- README.md | 2 +- docs/client-configuration.md | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2a894f50c5..b06b7f3cfd 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/docs/client-configuration.md b/docs/client-configuration.md index a67cef462a..d57d0c5dd3 100644 --- a/docs/client-configuration.md +++ b/docs/client-configuration.md @@ -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.