Skip to content

Commit eb9500b

Browse files
author
Ruben Bridgewater
committed
Fix redis 2.4 auth support
1 parent 08a4537 commit eb9500b

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lib/individualCommands.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var utils = require('./utils');
44
var debug = require('./debug');
55
var Multi = require('./multi');
66
var no_password_is_set = /no password is set/;
7+
var loading = /LOADING/;
78
var RedisClient = require('../').RedisClient;
89

910
/********************************
@@ -91,12 +92,20 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, c
9192
this.auth_pass = pass;
9293
this.ready = this.offline_queue.length === 0; // keep the execution order intakt
9394
var tmp = this.send_command('auth', [pass], function (err, res) {
94-
if (err && no_password_is_set.test(err.message)) {
95-
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
96-
err = null;
97-
res = 'OK';
95+
if (err) {
96+
if (no_password_is_set.test(err.message)) {
97+
self.warn('Warning: Redis server does not require a password, but a password was supplied.');
98+
err = null;
99+
res = 'OK';
100+
} else if (loading.test(err.message)) {
101+
// If redis is still loading the db, it will not authenticate and everything else will fail
102+
debug('Redis still loading, trying to authenticate later');
103+
setTimeout(function () {
104+
self.auth(pass, callback);
105+
}, 200);
106+
return;
107+
}
98108
}
99-
100109
utils.callback_or_emit(self, callback, err, res);
101110
});
102111
this.ready = ready;

test/auth.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ describe("client authentication", function () {
4545
});
4646
});
4747

48+
it('support redis 2.4 with retrying auth commands if still loading', function (done) {
49+
if (helper.redisProcess().spawnFailed()) this.skip();
50+
51+
client = redis.createClient.apply(null, args);
52+
var time = Date.now();
53+
client.auth(auth, function (err, res) {
54+
assert.strictEqual('retry worked', res);
55+
assert(Date.now() - time >= 200, 'Time should be above 200 ms (the reconnect time)');
56+
assert(Date.now() - time < 300, 'Time should be below 300 ms (the reconnect should only take a bit above 200 ms)');
57+
done();
58+
});
59+
var tmp = client.command_queue.get(0).callback;
60+
client.command_queue.get(0).callback = function (err, res) {
61+
client.auth = function (pass, callback) {
62+
callback(null, 'retry worked');
63+
};
64+
tmp(new Error('ERR redis is still LOADING'));
65+
};
66+
});
67+
4868
it("emits error when auth is bad without callback", function (done) {
4969
if (helper.redisProcess().spawnFailed()) this.skip();
5070

0 commit comments

Comments
 (0)