Skip to content

Commit

Permalink
feat: add updateSentinels option to control new sentinel values being…
Browse files Browse the repository at this point in the history
… added to the original list (#814)

Closes #798.
  • Loading branch information
jschweik authored and luin committed Mar 12, 2019
1 parent 0985ae8 commit 50a9db7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/connectors/SentinelConnector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface ISentinelConnectionOptions extends ITcpConnectionOptions {
connectTimeout?: number
enableTLSForSentinelMode?: boolean
sentinelTLS?: SecureContextOptions
updateSentinels?: boolean
}

export default class SentinelConnector extends AbstractConnector {
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -173,6 +175,7 @@ Redis.defaultOptions = {
return Math.min(times * 10, 1000);
},
enableTLSForSentinelMode: false,
updateSentinels: true,
// Status
password: null,
db: 0,
Expand Down
63 changes: 63 additions & 0 deletions test/functional/sentinel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 50a9db7

Please sign in to comment.