You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+49-14Lines changed: 49 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ Please be aware that sending null, undefined and Boolean values will result in t
118
118
119
119
# API
120
120
121
-
## Connection Events
121
+
## Connection and other Events
122
122
123
123
`client` will emit some events about the state of the connection to the Redis server.
124
124
@@ -147,31 +147,36 @@ So please attach the error listener to node_redis.
147
147
148
148
`client` will emit `end` when an established Redis server connection has closed.
149
149
150
-
### "drain"
150
+
### "drain" (deprecated)
151
151
152
152
`client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now
153
153
writable. This event can be used to stream commands in to Redis and adapt to backpressure.
154
154
155
155
If the stream is buffering `client.should_buffer` is set to true. Otherwise the variable is always set to false.
156
156
That way you can decide when to reduce your send rate and resume sending commands when you get `drain`.
157
157
158
-
You can also check the return value of each command as it will also return the backpressure indicator.
158
+
You can also check the return value of each command as it will also return the backpressure indicator (deprecated).
159
159
If false is returned the stream had to buffer.
160
160
161
-
### "idle"
161
+
### "warning"
162
+
163
+
`client` will emit `warning` when password was set but none is needed and if a deprecated option / function / similar is used.
164
+
165
+
### "idle" (deprecated)
162
166
163
167
`client` will emit `idle` when there are no outstanding commands that are awaiting a response.
164
168
165
169
## redis.createClient()
166
170
If you have `redis-server` running on the same computer as node, then the defaults for
167
171
port and host are probably fine and you don't need to supply any arguments. `createClient()` returns a `RedisClient` object.
168
172
173
+
If the redis server runs on the same machine as the client consider using unix sockets if possible to increase throughput.
174
+
169
175
### overloading
170
-
*`redis.createClient()`
171
-
*`redis.createClient(options)`
172
-
*`redis.createClient(unix_socket, options)`
173
-
*`redis.createClient(redis_url, options)`
174
-
*`redis.createClient(port, host, options)`
176
+
*`redis.createClient([options])`
177
+
*`redis.createClient(unix_socket[, options])`
178
+
*`redis.createClient(redis_url[, options])`
179
+
*`redis.createClient(port[, host][, options])`
175
180
176
181
#### `options` is an object with the following possible properties:
177
182
*`host`: *127.0.0.1*; The host to connect to
@@ -200,21 +205,22 @@ This delay normally grows infinitely, but setting `retry_max_delay` limits it to
200
205
*`connect_timeout`: *3600000*; Setting `connect_timeout` limits total time for client to connect and reconnect.
201
206
The value is provided in milliseconds and is counted from the moment on a new client is created / a connection is lost. The last retry is going to happen exactly at the timeout time.
202
207
Default is to try connecting until the default system socket timeout has been exceeded and to try reconnecting until 1h passed.
203
-
*`max_attempts`: *0*; By default client will try reconnecting until connected. Setting `max_attempts`
208
+
*`max_attempts`: *0*; (Deprecated, please use `retry_strategy` instead) By default client will try reconnecting until connected. Setting `max_attempts`
204
209
limits total amount of connection tries. Setting this to 1 will prevent any reconnect tries.
205
210
*`retry_unfulfilled_commands`: *false*; If set to true, all commands that were unfulfulled while the connection is lost will be retried after the connection has reestablished again. Use this with caution, if you use state altering commands (e.g. *incr*). This is especially useful if you use blocking commands.
206
-
*`password`: *null*; If set, client will run redis auth command on connect. Alias `auth_pass`
211
+
*`password`: *null*; If set, client will run redis auth command on connect. Alias `auth_pass` (node_redis < 2.5 have to use auth_pass)
207
212
*`db`: *null*; If set, client will run redis select command on connect. This is [not recommended](https://groups.google.com/forum/#!topic/redis-db/vS5wX8X4Cjg).
208
213
*`family`: *IPv4*; You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules how to use the family type.
209
214
*`disable_resubscribing`: *false*; If set to `true`, a client won't resubscribe after disconnecting
210
215
*`rename_commands`: *null*; pass a object with renamed commands to use those instead of the original functions. See the [redis security topics](http://redis.io/topics/security) for more info.
211
216
*`tls`: an object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback),
212
217
to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel).
213
218
*`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.
var client =redis.createClient({detect_buffers:true});
218
224
219
225
client.set("foo_rand000000000000", "OK");
220
226
@@ -230,6 +236,28 @@ client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
230
236
client.end();
231
237
```
232
238
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
+
returnnewError('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
+
returnnewError('Retry time exhausted');
250
+
}
251
+
if (options.times_connected>10) {
252
+
// End reconnecting with built in error
253
+
returnundefined;
254
+
}
255
+
// reconnect after
256
+
returnMath.max(options.attempt*100, 3000);
257
+
}
258
+
});
259
+
```
260
+
233
261
## client.auth(password[, callback])
234
262
235
263
When connecting to a Redis server that requires authentication, the `AUTH` command must be sent as the
@@ -241,6 +269,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
241
269
you are doing this wrong, `client` will emit an error that looks
242
270
something like this `Error: Ready check failed: ERR operation not permitted`.
243
271
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
+
244
279
## client.end(flush)
245
280
246
281
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
@@ -267,7 +302,7 @@ client.get("foo_rand000000000000", function (err, reply) {
267
302
});
268
303
```
269
304
270
-
`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!
0 commit comments