From 719d4f74cc931c34c4329599468c43b08a6b9b2b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 8 May 2011 19:57:27 +0200 Subject: [PATCH] Fixed a bug when the client tries to authenticate to the redis server and the server is still loading the database: Now the client tries to authenticate again every 2 seconds until actually authenticated. "connect" is only emitted after authentication if authentication is required. --- index.js | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index c4f8611c22..428595a043 100644 --- a/index.js +++ b/index.js @@ -158,13 +158,25 @@ RedisClient.prototype.on_connect = function () { this.stream.setTimeout(0); if (this.auth_pass) { + var self = this; + + // if redis is still loading the db, it will not authenticate and everything else will fail + function cmd_do_auth() { + if (exports.debug_mode) { - console.log("Sending auth to " + this.host + ":" + this.port + " fd " + this.stream.fd); + console.log("Sending auth to " + self.host + ":" + self.port + " fd " + self.stream.fd); } self.send_anyway = true; - self.send_command("auth", this.auth_pass, function (err, res) { + self.send_command("auth", self.auth_pass, function (err, res) { if (err) { - return self.emit("error", "Auth error: " + err); + if (err.toString().match("LOADING")) { + // still loading, try to authenticate later + + console.log("Redis still loading, trying to authenticate later"); + setTimeout(cmd_do_auth,2000); + return; + } + else return self.emit("error", "Auth error: " + err); } if (res.toString() !== "OK") { return self.emit("error", "Auth failed: " + res.toString()); @@ -176,18 +188,34 @@ RedisClient.prototype.on_connect = function () { self.auth_callback(err, res); self.auth_callback = null; } + + // now we are really connected + self.emit("connect"); + + if (self.options.no_ready_check) { + self.ready = true; + self.send_offline_queue(); + } else { + self.ready_check(); + } + }); self.send_anyway = false; - } + } - this.emit("connect"); - - if (this.options.no_ready_check) { - this.ready = true; - this.send_offline_queue(); + cmd_do_auth(); } else { - this.ready_check(); + + this.emit("connect"); + + if (this.options.no_ready_check) { + this.ready = true; + this.send_offline_queue(); + } else { + this.ready_check(); + } } + }; RedisClient.prototype.ready_check = function () {