Skip to content

Commit 5bcb29d

Browse files
committed
Return early when channels mismatch to skip expensive msgpack decoding
1 parent e8e8b01 commit 5bcb29d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,21 @@ function adapter(uri, opts){
6868

6969
this.uid = uid;
7070
this.prefix = prefix;
71+
this.channel = prefix + '#' + nsp.name + '#';
72+
if (String.prototype.startsWith) {
73+
this.channelMatches = function (messageChannel, subscribedChannel) {
74+
return messageChannel.startsWith(subscribedChannel);
75+
}
76+
} else { // Fallback to slow indexOf impl for older Node.js
77+
this.channelMatches = function (messageChannel, subscribedChannel) {
78+
return messageChannel.indexOf(subscribedChannel) === 0;
79+
}
80+
}
7181
this.pubClient = pub;
7282
this.subClient = sub;
7383

7484
var self = this;
75-
sub.subscribe(prefix + '#' + nsp.name + '#', function(err){
85+
sub.subscribe(this.channel, function(err){
7686
if (err) self.emit('error', err);
7787
});
7888
sub.on(subEvent, this.onmessage.bind(this));
@@ -91,6 +101,9 @@ function adapter(uri, opts){
91101
*/
92102

93103
Redis.prototype.onmessage = function(channel, msg){
104+
if (!this.channelMatches(channel.toString(), this.channel)) {
105+
return debug('ignore different channel');
106+
}
94107
var args = msgpack.decode(msg);
95108
var packet;
96109

0 commit comments

Comments
 (0)