Skip to content

Commit f34fde2

Browse files
Don't attempt to connect to master while initialization is in progress
1 parent a757cda commit f34fde2

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

index.js

+30-10
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ function RedisClient(stream, options) {
6969

7070
this.old_state = null;
7171

72-
var self = this;
72+
if(this.stream){
73+
this.initialize_stream_listeners();
74+
}
7375

76+
events.EventEmitter.call(this);
77+
}
78+
util.inherits(RedisClient, events.EventEmitter);
79+
exports.RedisClient = RedisClient;
80+
81+
RedisClient.prototype.initialize_stream_listeners = function () {
82+
var self = this;
7483
this.stream.on("connect", function () {
7584
self.on_connect();
7685
});
@@ -95,11 +104,7 @@ function RedisClient(stream, options) {
95104
self.should_buffer = false;
96105
self.emit("drain");
97106
});
98-
99-
events.EventEmitter.call(this);
100-
}
101-
util.inherits(RedisClient, events.EventEmitter);
102-
exports.RedisClient = RedisClient;
107+
};
103108

104109
RedisClient.prototype.initialize_retry_vars = function () {
105110
this.retry_timer = null;
@@ -468,6 +473,12 @@ RedisClient.prototype.connection_gone = function (why) {
468473
};
469474

470475
RedisClient.prototype.forceReconnectionAttempt = function (){
476+
if(!this.stream){
477+
this.stream = net.createConnection(this.port, this.host);
478+
this.initialize_stream_listeners();
479+
return;
480+
}
481+
471482
clearTimeout(this.retry_timer);
472483
this.initialize_retry_vars();
473484
this.connection_gone();
@@ -1098,11 +1109,20 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
10981109

10991110

11001111
exports.createClient = function (port_arg, host_arg, options) {
1101-
var port = port_arg || default_port,
1102-
host = host_arg || default_host,
1103-
redis_client, net_client;
1112+
var redis_client;
1113+
var net_client;
1114+
1115+
var port = port_arg,
1116+
host = host_arg;
1117+
1118+
options = options || {};
11041119

1105-
net_client = net.createConnection(port, host);
1120+
if(options.allowNoSocket !== true || (port_arg !== null && host_arg !== null)){
1121+
host = host || default_host;
1122+
port = port || default_port;
1123+
1124+
net_client = net.createConnection(port, host);
1125+
}
11061126

11071127
redis_client = new RedisClient(net_client, options);
11081128

sentinel.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ function reply_to_object(reply) {
2424

2525
function RedisMetaClient(masterName, startingSentinels) {
2626
this.healthy = false;
27-
this.master = null;
27+
this.master = {
28+
host: null,
29+
port: null
30+
};
2831
this.masterClientId = 0;
2932
this.masterClients = [];
3033
this.masterName = masterName;
@@ -458,6 +461,8 @@ RedisMetaClient.prototype.masterAvailable = function(availableMaster) {
458461
};
459462

460463
RedisMetaClient.prototype.createMasterClient = function(options) {
464+
options = options || {};
465+
options.allowNoSocket = true;
461466
var client = RedisSingleClient.createClient(this.master.port, this.master.host, options);
462467
var id = ++this.masterClientId;
463468
var self = this;

0 commit comments

Comments
 (0)