Skip to content

Commit 274f846

Browse files
Tanguy Le BarzicTanguy Le Barzic
Tanguy Le Barzic
authored and
Tanguy Le Barzic
committed
Introduce utils, for methods shared between client.js and sentinel.js
1 parent 917b30a commit 274f846

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

index.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var net = require("net"),
44
util = require("./lib/util"),
5+
reply_to_object = require("./lib/utils").reply_to_object,
56
Queue = require("./lib/queue"),
67
to_array = require("./lib/to_array"),
78
events = require("events"),
@@ -542,23 +543,6 @@ function try_callback(callback, reply) {
542543
}
543544
}
544545

545-
// hgetall converts its replies to an Object. If the reply is empty, null is returned.
546-
function reply_to_object(reply) {
547-
var obj = {}, j, jl, key, val;
548-
549-
if (reply.length === 0) {
550-
return null;
551-
}
552-
553-
for (j = 0, jl = reply.length; j < jl; j += 2) {
554-
key = reply[j].toString();
555-
val = reply[j + 1];
556-
obj[key] = val;
557-
}
558-
559-
return obj;
560-
}
561-
562546
function reply_to_strings(reply) {
563547
var i;
564548

lib/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// hgetall converts its replies to an Object. If the reply is empty, null is returned.
2+
function reply_to_object(reply) {
3+
var obj = {}, j, jl, key, val;
4+
5+
if (reply.length === 0) {
6+
return null;
7+
}
8+
9+
for (j = 0, jl = reply.length; j < jl; j += 2) {
10+
key = reply[j].toString();
11+
val = reply[j + 1];
12+
obj[key] = val;
13+
}
14+
15+
return obj;
16+
}
17+
18+
module.exports = {
19+
reply_to_object: reply_to_object
20+
};

sentinel.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
var events = require("events");
2+
var reply_to_object = require("./lib/utils").reply_to_object;
23
var util = require("util");
34

4-
exports.debug_mode = true;
5-
65
var RedisSingleClient = require("./index");
76

8-
function reply_to_object(reply) {
9-
var obj = {},
10-
j, jl, key, val;
11-
12-
if(reply.length === 0) {
13-
return null;
14-
}
15-
16-
for(j = 0, jl = reply.length; j < jl; j += 2) {
17-
key = reply[j].toString();
18-
val = reply[j + 1];
19-
obj[key] = val;
20-
}
21-
22-
return obj;
23-
}
7+
exports.debug_mode = true;
248

259
function RedisMetaClient(masterName, startingSentinels) {
2610
this.healthy = false;
@@ -193,8 +177,6 @@ RedisMetaClient.prototype.initSentinelConnection = function(sentinelConfig) {
193177
var parts = message.split(' ');
194178
switch(channel) {
195179
case '+reset-master':
196-
console.log(channel);
197-
console.log(message);
198180
break;
199181

200182
case '+slave':
@@ -323,18 +305,22 @@ RedisMetaClient.prototype.initSentinelConnection = function(sentinelConfig) {
323305

324306

325307
default:
326-
console.log(channel + "::" + message);
327-
break;
308+
if(exports.debug_mode){
309+
console.log(channel + "::" + message);
310+
break;
311+
}
328312
}
329313
});
330314

315+
// TODO: seems a bit rude
331316
sentinelClient.on('error', function() {
332317
self.removeSentinel(sentinelConfig);
333318
});
334319

335320
sentinelClient.psubscribe('*');
336321
};
337322

323+
// There are surely other things to do here
338324
RedisMetaClient.prototype.quit = function(){
339325
clearTimeout(this.cleanMessagesReceived);
340326
};
@@ -460,12 +446,14 @@ RedisMetaClient.prototype.masterAvailable = function(availableMaster) {
460446
this.emit('healthyChange', true);
461447
};
462448

449+
// Return a RedisSingleCLient pointing to the master (or to nothing if there is no master yet)
463450
RedisMetaClient.prototype.createMasterClient = function(options) {
464451
options = options || {};
465452
options.allowNoSocket = true;
466453
var client = RedisSingleClient.createClient(this.master.port, this.master.host, options);
467454
var self = this;
468455
var connection_id = client.connection_id;
456+
469457
client.on('end', function(){
470458
for (var i = 0, len = self.masterClients.length; i < len; i++) {
471459
var cl = self.masterClients[i];
@@ -474,7 +462,9 @@ RedisMetaClient.prototype.createMasterClient = function(options) {
474462
}
475463
}
476464
});
465+
477466
this.masterClients.push({config: this.master, client: client});
467+
478468
return client;
479469
};
480470

0 commit comments

Comments
 (0)