Skip to content

Commit

Permalink
Merge 80eddda into 6642278
Browse files Browse the repository at this point in the history
  • Loading branch information
leibale authored Jan 24, 2023
2 parents 6642278 + 80eddda commit a94d316
Show file tree
Hide file tree
Showing 40 changed files with 2,176 additions and 655 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,18 @@ Check out the [Clustering Guide](./docs/clustering.md) when using Node Redis to

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

| Event name | Scenes | Arguments to be passed to the listener |
|----------------|-------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| `connect` | The client is initiating a connection to the server. | _No argument_ |
| `ready` | The client successfully initiated the connection to the server. | _No argument_ |
| `end` | The client disconnected the connection to the server via `.quit()` or `.disconnect()`. | _No argument_ |
| `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]` |
| `reconnecting` | The client is trying to reconnect to the server. | _No argument_ |

The client will not emit [any other events](./docs/v3-to-v4.md#all-the-removed-events) beyond those listed above.
| Name | When | Listener arguments |
|-------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------|
| `connect` | Initiating a connection to the server | *No arguments* |
| `ready` | Client is ready to use | *No arguments* |
| `end` | Connection has been closed (via `.quit()` or `.disconnect()`) | *No arguments* |
| `error` | An error has occurred—usually a network issue such as "Socket closed unexpectedly" | `(error: Error)` |
| `reconnecting` | Client is trying to reconnect to the server | *No arguments* |
| `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>> })` |

> :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.
> The client will not emit [any other events](./docs/v3-to-v4.md#all-the-removed-events) beyond those listed above.
## Supported Redis versions

Expand Down
26 changes: 9 additions & 17 deletions docs/client-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,18 @@

## Reconnect Strategy

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).
When the socket closes unexpectedly (without calling `.quit()`/`.disconnect()`) the client uses `reconnectStrategy` to decide what to do:
1. `false` -> do not reconnect, close the client and flush all commands in the queue.
2. `number` -> wait for `X` milliseconds before reconnecting.
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.

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.
By default the strategy is `Math.min(retries * 50, 500)`, but it can be overriten:

```typescript
import { createClient } from 'redis';

const client = createClient({
socket: {
reconnectStrategy: (retries) => Math.min(retries * 50, 500)
}
createClient({
socket: {
reconnectStrategy: retries => Math.min(retries * 50, 1000)
}
});
```

Expand Down
1 change: 1 addition & 0 deletions docs/clustering.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const value = await cluster.get('key');
| 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 |
| defaults | | The default configuration values for every client in the cluster. Use this for example when specifying an ACL user to connect with |
| 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 |
| 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. |
| maxCommandRedirections | `16` | The maximum number of times a command will be redirected due to `MOVED` or `ASK` errors |
| nodeAddressMap | | Defines the [node address mapping](#node-address-map) |
| modules | | Included [Redis Modules](../README.md#packages) |
Expand Down
Loading

0 comments on commit a94d316

Please sign in to comment.