Skip to content

Commit a94d316

Browse files
authored
Merge 80eddda into 6642278
2 parents 6642278 + 80eddda commit a94d316

40 files changed

+2176
-655
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,18 @@ Check out the [Clustering Guide](./docs/clustering.md) when using Node Redis to
373373

374374
The Node Redis client class is an Nodejs EventEmitter and it emits an event each time the network status changes:
375375

376-
| Event name | Scenes | Arguments to be passed to the listener |
377-
|----------------|-------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
378-
| `connect` | The client is initiating a connection to the server. | _No argument_ |
379-
| `ready` | The client successfully initiated the connection to the server. | _No argument_ |
380-
| `end` | The client disconnected the connection to the server via `.quit()` or `.disconnect()`. | _No argument_ |
381-
| `error` | When a network error has occurred, such as unable to connect to the server or the connection closed unexpectedly. | 1 argument: The error object, such as `SocketClosedUnexpectedlyError: Socket closed unexpectedly` or `Error: connect ECONNREFUSED [IP]:[PORT]` |
382-
| `reconnecting` | The client is trying to reconnect to the server. | _No argument_ |
383-
384-
The client will not emit [any other events](./docs/v3-to-v4.md#all-the-removed-events) beyond those listed above.
376+
| Name | When | Listener arguments |
377+
|-------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|
378+
| `connect` | Initiating a connection to the server | *No arguments* |
379+
| `ready` | Client is ready to use | *No arguments* |
380+
| `end` | Connection has been closed (via `.quit()` or `.disconnect()`) | *No arguments* |
381+
| `error` | An error has occurred—usually a network issue such as "Socket closed unexpectedly" | `(error: Error)` |
382+
| `reconnecting` | Client is trying to reconnect to the server | *No arguments* |
383+
| `sharded-channel-moved` | The ["cluster slot"](https://redis.io/docs/reference/cluster-spec/#key-distribution-model) of a subscribed [sharded PubSub](https://redis.io/docs/manual/pubsub/#sharded-pubsub) channel has been moved | `(channel: string, listeners: { buffers: Set<Listener<Buffer>>, strings: Set<Listener<string>> })` |
384+
385+
> :warning: You **MUST** listen to `error` events. If a client doesn't have at least one `error` listener registered and an `error` occurs, that error will be thrown and the Node.js process will exit. See the [`EventEmitter` docs](https://nodejs.org/api/events.html#events_error_events) for more details.
386+
387+
> The client will not emit [any other events](./docs/v3-to-v4.md#all-the-removed-events) beyond those listed above.
385388
386389
## Supported Redis versions
387390

docs/client-configuration.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,18 @@
2929

3030
## Reconnect Strategy
3131

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).
32+
When the socket closes unexpectedly (without calling `.quit()`/`.disconnect()`) the client uses `reconnectStrategy` to decide what to do:
33+
1. `false` -> do not reconnect, close the client and flush all commands in the queue.
34+
2. `number` -> wait for `X` milliseconds before reconnecting.
35+
3. `(retries: number, cause: Error) => number | Error` -> `number` is the same as configuration a `number` directly, `Error` is the same as `false`, but with a custom error.
3336

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:
37-
38-
- Receives the number of retries attempted so far.
39-
- Returns `number | Error`:
40-
- `number`: wait time in milliseconds prior to attempting a reconnect.
41-
- `Error`: closes the client and flushes internal command queues.
42-
43-
The example below shows the default `reconnectStrategy` and how to override it.
37+
By default the strategy is `Math.min(retries * 50, 500)`, but it can be overriten:
4438

4539
```typescript
46-
import { createClient } from 'redis';
47-
48-
const client = createClient({
49-
socket: {
50-
reconnectStrategy: (retries) => Math.min(retries * 50, 500)
51-
}
40+
createClient({
41+
socket: {
42+
reconnectStrategy: retries => Math.min(retries * 50, 1000)
43+
}
5244
});
5345
```
5446

docs/clustering.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const value = await cluster.get('key');
3535
| rootNodes | | An array of root nodes that are part of the cluster, which will be used to get the cluster topology. Each element in the array is a client configuration object. There is no need to specify every node in the cluster, 3 should be enough to reliably connect and obtain the cluster configuration from the server |
3636
| defaults | | The default configuration values for every client in the cluster. Use this for example when specifying an ACL user to connect with |
3737
| useReplicas | `false` | When `true`, distribute load by executing readonly commands (such as `GET`, `GEOSEARCH`, etc.) across all cluster nodes. When `false`, only use master nodes |
38+
| minimizeConnections | `false` | When `true`, `.connect()` will only discover the cluster topology, without actually connecting to all the nodes. Useful for short-term or PubSub-only connections. |
3839
| maxCommandRedirections | `16` | The maximum number of times a command will be redirected due to `MOVED` or `ASK` errors |
3940
| nodeAddressMap | | Defines the [node address mapping](#node-address-map) |
4041
| modules | | Included [Redis Modules](../README.md#packages) |

0 commit comments

Comments
 (0)