Skip to content

Commit 5d1e9fe

Browse files
committed
Merge pull request #998 from NodeRedis/2.5
2.5 pre-release Fixes #479 Fixes #905 Fixes #958
2 parents 8a43dea + 7c9c5e2 commit 5d1e9fe

36 files changed

+1988
-1156
lines changed

.npmignore

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
examples/
2-
benches/
2+
benchmarks/
33
test/
4-
diff_multi_bench_output.js
5-
generate_commands.js
6-
multi_bench.js
7-
test-unref.js
8-
changelog.md
9-
*.log
4+
.nyc_output/
5+
coverage/
6+
.tern-port
7+
*.log
8+
*.rdb
9+
*.out
10+
*.yml

README.md

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Please be aware that sending null, undefined and Boolean values will result in t
118118

119119
# API
120120

121-
## Connection Events
121+
## Connection and other Events
122122

123123
`client` will emit some events about the state of the connection to the Redis server.
124124

@@ -147,31 +147,36 @@ So please attach the error listener to node_redis.
147147

148148
`client` will emit `end` when an established Redis server connection has closed.
149149

150-
### "drain"
150+
### "drain" (deprecated)
151151

152152
`client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now
153153
writable. This event can be used to stream commands in to Redis and adapt to backpressure.
154154

155155
If the stream is buffering `client.should_buffer` is set to true. Otherwise the variable is always set to false.
156156
That way you can decide when to reduce your send rate and resume sending commands when you get `drain`.
157157

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).
159159
If false is returned the stream had to buffer.
160160

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)
162166

163167
`client` will emit `idle` when there are no outstanding commands that are awaiting a response.
164168

165169
## redis.createClient()
166170
If you have `redis-server` running on the same computer as node, then the defaults for
167171
port and host are probably fine and you don't need to supply any arguments. `createClient()` returns a `RedisClient` object.
168172

173+
If the redis server runs on the same machine as the client consider using unix sockets if possible to increase throughput.
174+
169175
### 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])`
175180

176181
#### `options` is an object with the following possible properties:
177182
* `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
200205
* `connect_timeout`: *3600000*; Setting `connect_timeout` limits total time for client to connect and reconnect.
201206
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.
202207
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`
204209
limits total amount of connection tries. Setting this to 1 will prevent any reconnect tries.
205210
* `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)
207212
* `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).
208213
* `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.
209214
* `disable_resubscribing`: *false*; If set to `true`, a client won't resubscribe after disconnecting
210215
* `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.
211216
* `tls`: an object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback),
212217
to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel).
213218
* `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.
214220

215221
```js
216-
var redis = require("redis"),
217-
client = redis.createClient({detect_buffers: true});
222+
var redis = require("redis");
223+
var client = redis.createClient({detect_buffers: true});
218224

219225
client.set("foo_rand000000000000", "OK");
220226

@@ -230,6 +236,28 @@ client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
230236
client.end();
231237
```
232238

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+
233261
## client.auth(password[, callback])
234262

235263
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
241269
you are doing this wrong, `client` will emit an error that looks
242270
something like this `Error: Ready check failed: ERR operation not permitted`.
243271

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+
244279
## client.end(flush)
245280

246281
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) {
267302
});
268303
```
269304

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!
271306

272307
## client.unref()
273308

benchmarks/buffer_bench.js

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)