Skip to content

Commit

Permalink
Merge pull request #177 from cgiovanacci/connection-setname
Browse files Browse the repository at this point in the history
allow the connection to be named even if susbscribed and reconnected
  • Loading branch information
luin committed Oct 15, 2015
2 parents 48d9701 + e45a154 commit 56f03ae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Redis.defaultOptions = {
return Math.min(times * 2, 2000);
},
keepAlive: 0,
connectionName: null,
// Sentinel
sentinels: null,
name: null,
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 @@ -123,6 +123,11 @@ exports.readyHandler = function (self) {
var finalSelect = self.prevCondition ? self.prevCondition.select : self.condition.select;
self.condition.select = 0;

if (self.options.connectionName) {
debug('set the connection mane [%s]', self.options.connectionName);
self.client('setname', self.options.connectionName);
}

if (self.prevCondition) {
var condition = self.prevCondition;
self.prevCondition = null;
Expand Down
29 changes: 29 additions & 0 deletions test/functional/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ 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) {
expect(res).to.eql('niceName');
done();
});
});
redis.set('foo', 1);
});

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.disconnect(true);
redis.unsubscribe('l', function() {
redis.client('getname', function(err, res) {
expect(res).to.eql('niceName');
done();
});
});
});
});
});
});

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 56f03ae

Please sign in to comment.