Skip to content

Commit

Permalink
feat(sentinel): support Sentinel instances with authentication. (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
nomura yoshihiro authored and luin committed Mar 18, 2019
1 parent 7684b66 commit 2437eae
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ redis.set('foo', 'bar');
The arguments passed to the constructor are different from the ones you use to connect to a single node, where:

* `name` identifies a group of Redis instances composed of a master and one or more slaves (`mymaster` in the example);
* `sentinelPassword` (optional) password for Sentinel instances.
* `sentinels` are a list of sentinels to connect to. The list does not need to enumerate all your sentinel instances, but a few so that if one is down the client will try the next one.
* `role` (optional) with a value of `slave` will return a random slave from the Sentinel group.
* `preferredSlaves` (optional) can be used to prefer a particular slave or set of slaves based on priority. It accepts a function or array.
Expand Down
2 changes: 2 additions & 0 deletions lib/connectors/SentinelConnector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type PreferredSlaves =
interface ISentinelConnectionOptions extends ITcpConnectionOptions {
role: 'master' | 'slave'
name: 'string'
sentinelPassword?: 'string'
sentinels: Array<ISentinelAddress>
sentinelRetryStrategy?: (retryAttempts: number) => number
preferredSlaves?: PreferredSlaves
Expand Down Expand Up @@ -219,6 +220,7 @@ export default class SentinelConnector extends AbstractConnector {
var client = new Redis({
port: endpoint.port || 26379,
host: endpoint.host,
password: this.options.sentinelPassword || null,
family: endpoint.family || (isIIpcConnectionOptions(this.options) ? undefined : this.options.family),
tls: this.options.sentinelTLS,
retryStrategy: null,
Expand Down
26 changes: 26 additions & 0 deletions test/functional/sentinel.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,33 @@ describe('sentinel', function () {
name: 'master'
});
});
it('should connect to sentinel with authentication successfully', function (done) {
var authed = false;
var redisServer = new MockServer('17380', function (argv) {
if (argv[0] === 'auth' && argv[1] === 'pass') {
authed = true;
} else if (argv[0] === 'get' && argv[1] === 'foo') {
expect(authed).to.eql(true);
redisServer.disconnect();
done();
}
})
var sentinel = new MockServer(27379, function (argv) {
if (argv[0] === 'sentinel' && argv[1] === 'get-master-addr-by-name') {
sentinel.disconnect(done);
return ['127.0.0.1', '17380'];
}
});

var redis = new Redis({
sentinelPassword: 'pass',
sentinels: [
{ host: '127.0.0.1', port: '27379' }
],
name: 'master'
});
redis.get('foo').catch(function () {});
});
});

describe('master', function () {
Expand Down

0 comments on commit 2437eae

Please sign in to comment.