From 46e2dc2de52b8f90555845a8b5d146822aa5feef Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 18 Sep 2015 00:56:17 +0200 Subject: [PATCH] Fix memory leak. See #723 and thx to @rahar --- index.js | 38 ++++++++++++++++++------------------- test/commands/multi.spec.js | 4 ++-- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index c8a8b1a981..e94745ed48 100644 --- a/index.js +++ b/index.js @@ -394,9 +394,25 @@ RedisClient.prototype.send_offline_queue = function () { } }; -RedisClient.prototype.connection_gone = function (why) { - var self = this; +var retry_connection = function (self) { + debug("Retrying connection..."); + self.emit("reconnecting", { + delay: self.retry_delay, + attempt: self.attempts + }); + + self.retry_totaltime += self.retry_delay; + self.attempts += 1; + self.retry_delay = Math.round(self.retry_delay * self.retry_backoff); + + self.stream = net.createConnection(self.connectionOption); + self.install_stream_listeners(); + + self.retry_timer = null; +}; + +RedisClient.prototype.connection_gone = function (why) { // If a retry is already in progress, just let that happen if (this.retry_timer) { return; @@ -452,23 +468,7 @@ RedisClient.prototype.connection_gone = function (why) { debug("Retry connection in " + this.retry_delay + " ms"); - this.retry_timer = setTimeout(function () { - debug("Retrying connection..."); - - self.emit("reconnecting", { - delay: self.retry_delay, - attempt: self.attempts - }); - - self.retry_totaltime += self.retry_delay; - self.attempts += 1; - self.retry_delay = Math.round(self.retry_delay * self.retry_backoff); - - self.stream = net.createConnection(self.connectionOption); - self.install_stream_listeners(); - - self.retry_timer = null; - }, this.retry_delay); + this.retry_timer = setTimeout(retry_connection, this.retry_delay, this); }; RedisClient.prototype.on_data = function (data) { diff --git a/test/commands/multi.spec.js b/test/commands/multi.spec.js index d2cfd097a3..a18478b3de 100644 --- a/test/commands/multi.spec.js +++ b/test/commands/multi.spec.js @@ -250,11 +250,11 @@ describe("The 'multi' method", function () { }); it('reports EXECABORT exceptions when they occur (while queueing)', function (done) { - client.multi().config("bar").set("foo").exec(function (err, reply) { + client.multi().config("bar").set("foo").set("bar").exec(function (err, reply) { assert.equal(err.code, "EXECABORT"); assert.equal(reply, undefined, "The reply should have been discarded"); assert(err.message.match(/^EXECABORT/), "Error message should begin with EXECABORT"); - assert.equal(err.errors.length, 1, "err.errors should have 1 items"); + assert.equal(err.errors.length, 2, "err.errors should have 2 items"); assert.strictEqual(err.errors[0].command_used, 'SET'); assert.strictEqual(err.errors[0].position, 1); assert(/^ERR/.test(err.errors[0].message), "Actuall error message should begin with ERR");