From f6f5d91709519ec0b4eee4468d2a85fb96e45071 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 6 Dec 2015 03:51:18 +0100 Subject: [PATCH] Deprecate .end() by making the flush parameter mandatory and fix the docs --- README.md | 16 ++++++++++------ index.js | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 42857e52e8..aa448a09bc 100644 --- a/README.md +++ b/README.md @@ -246,12 +246,13 @@ NOTE: Your call to `client.auth()` should not be inside the ready handler. If you are doing this wrong, `client` will emit an error that looks something like this `Error: Ready check failed: ERR operation not permitted`. -## client.end([flush]) +## client.end(flush) Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed. If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies. -If flush is set to true, all still running commands will be rejected instead of ignored after using `.end`. +You should set flush to true, if you are not absolutly sure you do not care about any other commands. +If you set flush to false all still running commands will silently fail. This example closes the connection to the Redis server before the replies have been read. You probably don't want to do this: @@ -260,12 +261,15 @@ want to do this: var redis = require("redis"), client = redis.createClient(); -client.set("foo_rand000000000000", "some fantastic value"); -client.end(); // No further commands will be processed -client.get("foo_rand000000000000", function (err, reply) { - // This won't be called anymore, since flush has not been set to true! +client.set("foo_rand000000000000", "some fantastic value", function (err, reply) { + // This will either result in an error (flush parameter is set to true) + // or will silently fail and this callback will not be called at all (flush set to false) console.log(err); }); +client.end(true); // No further commands will be processed +client.get("foo_rand000000000000", function (err, reply) { + console.log(err); // => 'The connection has already been closed.' +}); ``` `client.end()` without the flush parameter should not be used in production! diff --git a/index.js b/index.js index 179902aedf..bbc6dd439e 100644 --- a/index.js +++ b/index.js @@ -550,7 +550,7 @@ RedisClient.prototype.connection_gone = function (why) { error.code = 'CONNECTION_BROKEN'; this.flush_and_error(error); this.emit('error', error); - this.end(); + this.end(false); return; } @@ -900,6 +900,17 @@ RedisClient.prototype.pub_sub_command = function (command_obj) { }; RedisClient.prototype.end = function (flush) { + // Flush queue if wanted + if (flush) { + this.flush_and_error(new Error("The command can't be processed. The connection has already been closed.")); + } else if (flush === undefined) { + console.warn( + 'node_redis: Using .end() without the flush parameter is deprecated. ' + + 'Please check the doku (https://github.com/NodeRedis/node_redis) and explictly use flush.\n' + + 'This will throw from v.3.0.0 on.' + ); + } + this.stream._events = {}; // Clear retry_timer @@ -909,11 +920,6 @@ RedisClient.prototype.end = function (flush) { } this.stream.on('error', noop); - // Flush queue if wanted - if (flush) { - this.flush_and_error(new Error("The command can't be processed. The connection has already been closed.")); - } - this.connected = false; this.ready = false; this.closing = true;