Skip to content

Commit

Permalink
feat(redis): support readonly mode for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Feb 7, 2016
1 parent 3e68925 commit 0a4186e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Creates a Redis instance
| [options.keyPrefix] | <code>string</code> | <code>&quot;&#x27;&#x27;&quot;</code> | The prefix to prepend to all keys in a command. ```javascript var redis = new Redis({ lazyConnect: true }); // No attempting to connect to the Redis server here. // Now let's connect to the Redis server redis.get('foo', function () { }); ``` |
| [options.retryStrategy] | <code>function</code> | | See "Quick Start" section |
| [options.reconnectOnError] | <code>function</code> | | See "Quick Start" section |
| [options.readOnly] | <code>boolean</code> | <code>false</code> | Enable READONLY mode for the connection. Only available for cluster mode. |

**Example**
```js
Expand Down
5 changes: 4 additions & 1 deletion lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ var ScanStream = require('./scan_stream');
* ```
* @param {function} [options.retryStrategy] - See "Quick Start" section
* @param {function} [options.reconnectOnError] - See "Quick Start" section
* @param {boolean} [options.readOnly=false] - Enable READONLY mode for the connection.
* Only available for cluster mode.
* @extends [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
* @extends Commander
* @example
Expand Down Expand Up @@ -166,7 +168,8 @@ Redis.defaultOptions = {
autoResendUnfulfilledCommands: true,
lazyConnect: false,
keyPrefix: '',
reconnectOnError: null
reconnectOnError: null,
readOnly: false
};

Redis.prototype.resetCommandQueue = function () {
Expand Down
5 changes: 5 additions & 0 deletions lib/redis/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ exports.readyHandler = function (self) {
self.client('setname', self.options.connectionName);
}

if (self.options.readOnly) {
debug('set the connection to readonly mode');
self.readonly();
}

if (self.prevCondition) {
var condition = self.prevCondition;
self.prevCondition = null;
Expand Down
38 changes: 28 additions & 10 deletions test/functional/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,50 @@ describe('connection', function () {
describe('connectionName', function () {
it('shoud name the connection if options.connectionName is not null', function (done) {
var redis = new Redis({ connectionName: 'niceName' });
redis.once('ready', function() {
redis.client('getname', function(err, res) {
redis.once('ready', function () {
redis.client('getname', function (err, res) {
expect(res).to.eql('niceName');
done();
});
});
redis.set('foo', 1);
});

it('should set the name before any subscribe command if reconnected', function(done) {
it('should set the name before any subscribe command if reconnected', function (done) {
var redis = new Redis({ connectionName: 'niceName' });
var pub = new Redis();
redis.once('ready', function () {
redis.subscribe('l', function() {
redis.subscribe('l', function () {
redis.disconnect(true);
redis.unsubscribe('l', function() {
redis.client('getname', function(err, res) {
expect(res).to.eql('niceName');
done();
});
redis.unsubscribe('l', function () {
redis.client('getname', function (err, res) {
expect(res).to.eql('niceName');
done();
});
});
});
});
});
});

describe('readOnly', function () {
it('shoud send readonly command before other commands', function (done) {
var called = false;
var redis = new Redis({ port: 30001, readOnly: true, showFriendlyErrorStack: true });
var node = new MockServer(30001, function (argv) {
if (argv[0] === 'readonly') {
called = true;
} else if (argv[0] === 'get' && argv[1] === 'foo') {
expect(called).to.eql(true);
redis.disconnect();
node.disconnect(function () {
done();
});
}
});
redis.get('foo').catch(function () {});
});
});

describe('autoResendUnfulfilledCommands', function () {
it('should resend unfulfilled commands to the correct db when reconnected', function (done) {
var redis = new Redis({ db: 3 });
Expand Down

0 comments on commit 0a4186e

Please sign in to comment.