Skip to content

Commit 8a83467

Browse files
Improvements on stability.
1 parent a71a034 commit 8a83467

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ function Command(command, args, sub_command, buffer_args, callback) {
645645
}
646646

647647
RedisClient.prototype.send_command = function (command, args, callback) {
648-
var arg, command_obj, i, il, elem_count, buffer_args, stream = this.stream, command_str = "", buffered_writes = 0, last_arg_type;
648+
var arg, command_obj, i, il, elem_count, buffer_args, stream = this.stream, command_str = "", buffered_writes = 0, last_arg_type, self = this;
649649

650650
if (typeof command !== "string") {
651651
throw new Error("First argument to send_command must be the command name string, not " + typeof command);
@@ -710,7 +710,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
710710
if (command_obj.callback) {
711711
command_obj.callback(not_writeable_error);
712712
} else {
713-
throw not_writeable_error;
713+
return self.emit("error", new Error(not_writeable_error));
714714
}
715715
}
716716

sentinel.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ var RedisSingleClient = require("./index");
66

77
exports.debug_mode = true;
88

9+
var states = {
10+
NOT_INITIALIZED: 1,
11+
HEALTHY: 2,
12+
UNHEALTHY: 3
13+
};
14+
915
function RedisMetaClient(masterName, startingSentinels) {
10-
this.healthy = false;
16+
this.state = states.NOT_INITIALIZED;
1117
this.master = {
1218
host: null,
1319
port: null
@@ -34,15 +40,20 @@ function RedisMetaClient(masterName, startingSentinels) {
3440

3541
this.getSentinels(startingSentinels, function(error, sentinelsConfig) {
3642
if(error) {
43+
self.emit('stateChange', states.UNHEALTHY);
3744
console.log(error);
3845
return;
3946
}
4047

4148
self.initSentinelsConnection(sentinelsConfig);
4249
});
4350

44-
this.on('healthyChange', function(newState) {
45-
this.healthy = newState;
51+
this.on('stateChange', function(newState) {
52+
console.log("stateChange: " + newState);
53+
this.state = newState;
54+
if(this.state === states.UNHEALTHY){
55+
self.masterUnavailable();
56+
}
4657
});
4758

4859
this.on('masterAvailable', function(availableMaster) {
@@ -213,13 +224,13 @@ RedisMetaClient.prototype.initSentinelConnection = function(sentinelConfig) {
213224

214225
case '+odown':
215226
if(parts[0] === 'master') {
216-
self.emit('healthyChange', false);
227+
self.emit('stateChange', states.UNHEALTHY);
217228
}
218229
break;
219230

220231
case '-odown':
221232
if(parts[0] === 'master') {
222-
self.emit('healthyChange', true);
233+
self.emit('stateChange', states.HEALTHY);
223234
}
224235
break;
225236

@@ -228,7 +239,7 @@ RedisMetaClient.prototype.initSentinelConnection = function(sentinelConfig) {
228239

229240
case '+failover-triggered':
230241
if(parts[0] === 'master') {
231-
self.emit('healthyChange', false);
242+
self.emit('stateChange', states.UNHEALTHY);
232243
}
233244
break;
234245

@@ -326,6 +337,9 @@ RedisMetaClient.prototype.quit = function(){
326337
};
327338

328339
RedisMetaClient.prototype.sentinelsConfigured = function() {
340+
if(exports.debug_mode){
341+
console.log("sentinelsConfigured");
342+
}
329343
this.setupMasterConnection();
330344
};
331345

@@ -366,6 +380,9 @@ RedisMetaClient.prototype.setupMasterConnection = function() {
366380
if(selectedMaster) {
367381
self.emit('masterAvailable', selectedMaster);
368382
}
383+
else {
384+
self.emit('stateChange', states.UNHEALTHY);
385+
}
369386
};
370387

371388
var onResultFromSentinel = function(error, result) {
@@ -443,9 +460,18 @@ RedisMetaClient.prototype.masterAvailable = function(availableMaster) {
443460
masterClient.client.forceReconnectionAttempt();
444461
});
445462

446-
this.emit('healthyChange', true);
463+
this.emit('stateChange', states.HEALTHY);
447464
};
448465

466+
RedisMetaClient.prototype.masterUnavailable = function() {
467+
console.log("masterUnavailable");
468+
this.masterClients.forEach(function(masterClient){
469+
masterClient.client.flush_and_error("Master not available");
470+
masterClient.client.enable_offline_queue = false;
471+
});
472+
};
473+
474+
449475
// Return a RedisSingleCLient pointing to the master (or to nothing if there is no master yet)
450476
RedisMetaClient.prototype.createMasterClient = function(options) {
451477
options = options || {};

0 commit comments

Comments
 (0)