diff --git a/index.js b/index.js index 73d2af16f0..5c1b21881b 100644 --- a/index.js +++ b/index.js @@ -305,6 +305,7 @@ function RedisClient(stream) { this.retry_delay = 250; this.retry_backoff = 1.7; this.subscriptions = false; + this.closing = false; var self = this; @@ -349,6 +350,9 @@ function RedisClient(stream) { }); this.stream.on("error", function (msg) { + if (this.closing) { + return; + } if (exports.debug_mode) { console.warn("Connecting to redis server: " + msg); } @@ -377,6 +381,7 @@ sys.inherits(RedisClient, events.EventEmitter); RedisClient.prototype.connection_gone = function () { var self = this; + // If a retry is already in progress, just let that happen if (self.retry_timer) { return; } @@ -391,6 +396,13 @@ RedisClient.prototype.connection_gone = function () { args[2]("Server connection closed"); } }); + + // If this is a requested shutdown, then don't retry + if (self.closing) { + self.retry_timer = null; + return; + } + if (exports.debug_mode) { console.log("Retry conneciton in " + self.retry_delay + " ms"); } @@ -526,6 +538,9 @@ RedisClient.prototype.send_command = function () { if (this.subscriptions === true) { throw new Error("Connection in pub/sub mode, only pub/sub commands may be used"); } + if (command === "QUIT") { + this.closing = true; + } this.command_queue.push(command_obj); } this.commands_sent += 1; @@ -592,6 +607,8 @@ RedisClient.prototype.end = function () { // http://code.google.com/p/redis/wiki/CommandReference exports.commands = [ + // Connection handling + "QUIT", "AUTH", // Commands operating on all value types "EXISTS", "DEL", "TYPE", "KEYS", "RANDOMKEY", "RENAME", "RENAMENX", "DBSIZE", "EXPIRE", "TTL", "SELECT", "MOVE", "FLUSHDB", "FLUSHALL", diff --git a/test.js b/test.js index f11041d2c6..0f0a372e5c 100644 --- a/test.js +++ b/test.js @@ -369,8 +369,8 @@ function run_next_test() { tests[test_name](); } else { console.log('\n completed \x1b[32m%d\x1b[0m tests in \x1b[33m%d\x1b[0m ms\n', test_count, new Date - all_start); - client.end(); - client2.end(); + client.quit(); + client2.quit(); } } diff --git a/test_start_stop.js b/test_start_stop.js new file mode 100644 index 0000000000..a7a892d5dc --- /dev/null +++ b/test_start_stop.js @@ -0,0 +1,5 @@ +var redis = require("redis"), + client = redis.createClient(); + +redis.debug_mode = true; +client.quit();