Skip to content

Commit e3f14b2

Browse files
ddunkinluin
authored andcommitted
feat(sentinel): update sentinels after getting master
1 parent 976c077 commit e3f14b2

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

lib/connectors/sentinel_connector.js

+42-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ SentinelConnector.prototype.connect = function (callback) {
3535
if (typeof this.currentPoint !== 'number') {
3636
this.currentPoint = -1;
3737
}
38+
if (!Array.isArray(this.sentinels)) {
39+
this.sentinels = this.options.sentinels;
40+
}
3841

3942
var _this = this;
4043
var lastError;
4144
connectToNext();
4245

4346
function connectToNext() {
4447
_this.currentPoint += 1;
45-
if (_this.currentPoint === _this.options.sentinels.length) {
48+
if (_this.currentPoint === _this.sentinels.length) {
4649
_this.currentPoint = -1;
4750

4851
var retryDelay;
@@ -62,7 +65,7 @@ SentinelConnector.prototype.connect = function (callback) {
6265
return;
6366
}
6467

65-
var endpoint = _this.options.sentinels[_this.currentPoint];
68+
var endpoint = _this.sentinels[_this.currentPoint];
6669
_this.resolve(endpoint, function (err, resolved) {
6770
if (!_this.connecting) {
6871
callback(new Error('Connection is closed.'));
@@ -84,13 +87,48 @@ SentinelConnector.prototype.connect = function (callback) {
8487
}
8588
};
8689

90+
SentinelConnector.prototype.updateSentinels = function (client, callback) {
91+
var _this = this;
92+
client.sentinel('sentinels', this.options.name, function (err, result) {
93+
if (err) {
94+
client.disconnect();
95+
return callback(err);
96+
}
97+
if (Array.isArray(result)) {
98+
for (var i = 0; i < result.length; ++i) {
99+
var sentinel = utils.packObject(result[i]);
100+
var flags = sentinel.flags ? sentinel.flags.split(',') : [];
101+
if (flags.indexOf('disconnected') === -1 && sentinel.ip && sentinel.port) {
102+
var endpoint = { host: sentinel.ip, port: parseInt(sentinel.port) };
103+
var isDuplicate = _this.sentinels.some(function (o) {
104+
return o.host === endpoint.host && o.port === endpoint.port;
105+
});
106+
if (!isDuplicate) {
107+
debug('adding sentinel %s:%s', endpoint.host, endpoint.port);
108+
_this.sentinels.push(endpoint);
109+
}
110+
}
111+
}
112+
debug('sentinels', _this.sentinels);
113+
}
114+
callback(null);
115+
});
116+
};
117+
87118
SentinelConnector.prototype.resolveMaster = function (client, callback) {
119+
var _this = this;
88120
client.sentinel('get-master-addr-by-name', this.options.name, function (err, result) {
89-
client.disconnect();
90121
if (err) {
122+
client.disconnect();
91123
return callback(err);
92124
}
93-
callback(null, Array.isArray(result) ? { host: result[0], port: result[1] } : null);
125+
_this.updateSentinels(client, function (err) {
126+
client.disconnect();
127+
if (err) {
128+
return callback(err);
129+
}
130+
callback(null, Array.isArray(result) ? { host: result[0], port: result[1] } : null);
131+
});
94132
});
95133
};
96134

0 commit comments

Comments
 (0)