From 50a9db7a81544c6cba196c88b1f7561b4fd8f8db Mon Sep 17 00:00:00 2001 From: jschweik Date: Tue, 12 Mar 2019 03:51:45 -0500 Subject: [PATCH] feat: add updateSentinels option to control new sentinel values being added to the original list (#814) Closes #798. --- lib/connectors/SentinelConnector/index.ts | 6 +++ lib/redis.js | 3 ++ test/functional/sentinel.js | 63 +++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/lib/connectors/SentinelConnector/index.ts b/lib/connectors/SentinelConnector/index.ts index 07cbec1c..6f103cfa 100644 --- a/lib/connectors/SentinelConnector/index.ts +++ b/lib/connectors/SentinelConnector/index.ts @@ -30,6 +30,7 @@ interface ISentinelConnectionOptions extends ITcpConnectionOptions { connectTimeout?: number enableTLSForSentinelMode?: boolean sentinelTLS?: SecureContextOptions + updateSentinels?: boolean } export default class SentinelConnector extends AbstractConnector { @@ -134,6 +135,11 @@ export default class SentinelConnector extends AbstractConnector { } private updateSentinels (client, callback: NodeCallback): void { + + if (!this.options.updateSentinels) { + return callback(null) + } + client.sentinel('sentinels', this.options.name, (err, result) => { if (err) { client.disconnect() diff --git a/lib/redis.js b/lib/redis.js index c6ea1d92..504ac7ab 100644 --- a/lib/redis.js +++ b/lib/redis.js @@ -88,6 +88,8 @@ var PromiseContainer = require('./promiseContainer'); * strings. This option is necessary when dealing with big numbers (exceed the [-2^53, +2^53] range). * @param {boolean} [options.enableTLSForSentinelMode=false] - Whether to support the `tls` option * when connecting to Redis via sentinel mode. + * @param {boolean} [options.updateSentinels=true] - Update the given `sentinels` list with new IP + * addresses when communicating with existing sentinels. * @extends [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) * @extends Commander * @example @@ -173,6 +175,7 @@ Redis.defaultOptions = { return Math.min(times * 10, 1000); }, enableTLSForSentinelMode: false, + updateSentinels: true, // Status password: null, db: 0, diff --git a/test/functional/sentinel.js b/test/functional/sentinel.js index c7882861..75180a1a 100644 --- a/test/functional/sentinel.js +++ b/test/functional/sentinel.js @@ -124,6 +124,69 @@ describe('sentinel', function () { name: 'master' }); }); + + it('should add additionally discovered sentinels when resolving successfully', function (done) { + + var sentinels = [ + { host: '127.0.0.1', port: 27379 } + ]; + + var sentinel = new MockServer(27379, function (argv) { + if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') { + return ['127.0.0.1', '17380']; + } + else if (argv[0] === 'sentinel' && argv[1] === 'sentinels') { + return [['ip', '127.0.0.1', 'port', '27379'], ['ip', '127.0.0.1', 'port', '27380']]; + } + + }); + var master = new MockServer(17380); + sentinel.once('disconnect', function () { + redis.disconnect(); + master.disconnect(function () { + expect(sentinels.length).to.eql(2); + sentinel.disconnect(done); + }); + }); + + var redis = new Redis({ + sentinels: sentinels, + name: 'master' + }); + }); + + it('should skip additionally discovered sentinels even if they are resolved successfully', function (done) { + + var sentinels = [ + { host: '127.0.0.1', port: 27379 } + ]; + + var sentinel = new MockServer(27379, function (argv) { + if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') { + return ['127.0.0.1', '17380']; + } + else if (argv[0] === 'sentinel' && argv[1] === 'sentinels') { + return [['ip', '127.0.0.1', 'port', '27379'], ['ip', '127.0.0.1', 'port', '27380']]; + } + + }); + var master = new MockServer(17380); + sentinel.once('disconnect', function () { + redis.disconnect(); + master.disconnect(function () { + expect(sentinels.length).to.eql(1); + expect(sentinels[0].port).to.eql(27379); + sentinel.disconnect(done); + }); + }); + + var redis = new Redis({ + sentinels: sentinels, + updateSentinels: false, + name: 'master' + }); + }); + }); describe('master', function () {