From 0338677403b60922d2835b8514ac99a72016979d Mon Sep 17 00:00:00 2001 From: luin Date: Sun, 25 Sep 2016 00:16:13 +0800 Subject: [PATCH] fix: prevent sentinel from getting duplicated nodes --- lib/connectors/sentinel_connector.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/connectors/sentinel_connector.js b/lib/connectors/sentinel_connector.js index a4bb5af9..30b168e0 100644 --- a/lib/connectors/sentinel_connector.js +++ b/lib/connectors/sentinel_connector.js @@ -99,10 +99,8 @@ SentinelConnector.prototype.updateSentinels = function (client, callback) { var sentinel = utils.packObject(result[i]); var flags = sentinel.flags ? sentinel.flags.split(',') : []; if (flags.indexOf('disconnected') === -1 && sentinel.ip && sentinel.port) { - var endpoint = { host: sentinel.ip, port: parseInt(sentinel.port) }; - var isDuplicate = _this.sentinels.some(function (o) { - return o.host === endpoint.host && o.port === endpoint.port; - }); + var endpoint = { host: sentinel.ip, port: parseInt(sentinel.port, 10) }; + var isDuplicate = _this.sentinels.some(_.bind(isSentinelEql, null, endpoint)); if (!isDuplicate) { debug('adding sentinel %s:%s', endpoint.host, endpoint.port); _this.sentinels.push(endpoint); @@ -178,4 +176,9 @@ SentinelConnector.prototype.resolve = function (endpoint, callback) { function noop() {} +function isSentinelEql(a, b) { + return ((a.host || '127.0.0.1') === (b.host || '127.0.0.1')) && + ((a.port || 6379) === (b.port || 6379)); +} + module.exports = SentinelConnector;