Skip to content

Commit f6f5d91

Browse files
author
Ruben Bridgewater
committed
Deprecate .end() by making the flush parameter mandatory and fix the docs
1 parent e89bcec commit f6f5d91

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If
246246
you are doing this wrong, `client` will emit an error that looks
247247
something like this `Error: Ready check failed: ERR operation not permitted`.
248248

249-
## client.end([flush])
249+
## client.end(flush)
250250

251251
Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed.
252252
If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies.
253253

254-
If flush is set to true, all still running commands will be rejected instead of ignored after using `.end`.
254+
You should set flush to true, if you are not absolutly sure you do not care about any other commands.
255+
If you set flush to false all still running commands will silently fail.
255256

256257
This example closes the connection to the Redis server before the replies have been read. You probably don't
257258
want to do this:
@@ -260,12 +261,15 @@ want to do this:
260261
var redis = require("redis"),
261262
client = redis.createClient();
262263

263-
client.set("foo_rand000000000000", "some fantastic value");
264-
client.end(); // No further commands will be processed
265-
client.get("foo_rand000000000000", function (err, reply) {
266-
// This won't be called anymore, since flush has not been set to true!
264+
client.set("foo_rand000000000000", "some fantastic value", function (err, reply) {
265+
// This will either result in an error (flush parameter is set to true)
266+
// or will silently fail and this callback will not be called at all (flush set to false)
267267
console.log(err);
268268
});
269+
client.end(true); // No further commands will be processed
270+
client.get("foo_rand000000000000", function (err, reply) {
271+
console.log(err); // => 'The connection has already been closed.'
272+
});
269273
```
270274

271275
`client.end()` without the flush parameter should not be used in production!

index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ RedisClient.prototype.connection_gone = function (why) {
550550
error.code = 'CONNECTION_BROKEN';
551551
this.flush_and_error(error);
552552
this.emit('error', error);
553-
this.end();
553+
this.end(false);
554554
return;
555555
}
556556

@@ -900,6 +900,17 @@ RedisClient.prototype.pub_sub_command = function (command_obj) {
900900
};
901901

902902
RedisClient.prototype.end = function (flush) {
903+
// Flush queue if wanted
904+
if (flush) {
905+
this.flush_and_error(new Error("The command can't be processed. The connection has already been closed."));
906+
} else if (flush === undefined) {
907+
console.warn(
908+
'node_redis: Using .end() without the flush parameter is deprecated. ' +
909+
'Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.\n' +
910+
'This will throw from v.3.0.0 on.'
911+
);
912+
}
913+
903914
this.stream._events = {};
904915

905916
// Clear retry_timer
@@ -909,11 +920,6 @@ RedisClient.prototype.end = function (flush) {
909920
}
910921
this.stream.on('error', noop);
911922

912-
// Flush queue if wanted
913-
if (flush) {
914-
this.flush_and_error(new Error("The command can't be processed. The connection has already been closed."));
915-
}
916-
917923
this.connected = false;
918924
this.ready = false;
919925
this.closing = true;

0 commit comments

Comments
 (0)