Skip to content

Commit 7c9c5e2

Browse files
author
Ruben Bridgewater
committed
Update readme to include more details about retry_strategy and backpressure
1 parent e48e1e8 commit 7c9c5e2

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ limits total amount of connection tries. Setting this to 1 will prevent any reco
216216
* `tls`: an object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback),
217217
to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel).
218218
* `prefix`: *null*; pass a string to prefix all used keys with that string as prefix e.g. 'namespace:test'
219+
* `retry_strategy`: *function*; pass a function that receives a options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number no further retry is going to happen and all offline commands are flushed with errors. Return a error to return that specific error to all offline commands. Check out the example too.
219220

220221
```js
221-
var redis = require("redis"),
222-
client = redis.createClient({detect_buffers: true});
222+
var redis = require("redis");
223+
var client = redis.createClient({detect_buffers: true});
223224

224225
client.set("foo_rand000000000000", "OK");
225226

@@ -235,6 +236,28 @@ client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
235236
client.end();
236237
```
237238

239+
retry_strategy example
240+
```js
241+
var client = redis.createClient({
242+
retry_strategy: function (options) {
243+
if (options.error.code === 'ECONNREFUSED') {
244+
// End reconnecting on a specific error and flush all commands with a individual error
245+
return new Error('The server refused the connection');
246+
}
247+
if (options.total_retry_time > 1000 * 60 * 60) {
248+
// End reconnecting after a specific timeout and flush all commands with a individual error
249+
return new Error('Retry time exhausted');
250+
}
251+
if (options.times_connected > 10) {
252+
// End reconnecting with built in error
253+
return undefined;
254+
}
255+
// reconnect after
256+
return Math.max(options.attempt * 100, 3000);
257+
}
258+
});
259+
```
260+
238261
## client.auth(password[, callback])
239262

240263
When connecting to a Redis server that requires authentication, the `AUTH` command must be sent as the
@@ -246,6 +269,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
246269
you are doing this wrong, `client` will emit an error that looks
247270
something like this `Error: Ready check failed: ERR operation not permitted`.
248271

272+
## backpressure
273+
274+
### stream
275+
276+
The client exposed the used [stream](https://nodejs.org/api/stream.html) in `client.stream` and if the stream or client had to [buffer](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback) the command in `client.should_buffer`.
277+
In combination this can be used to implement backpressure by checking the buffer state before sending a command and listening to the stream [drain](https://nodejs.org/api/stream.html#stream_event_drain) event.
278+
249279
## client.end(flush)
250280

251281
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
@@ -272,7 +302,7 @@ client.get("foo_rand000000000000", function (err, reply) {
272302
});
273303
```
274304

275-
`client.end()` without the flush parameter should not be used in production!
305+
`client.end()` without the flush parameter should NOT be used in production!
276306

277307
## client.unref()
278308

0 commit comments

Comments
 (0)