Skip to content

Commit

Permalink
wire in the wildcard cache
Browse files Browse the repository at this point in the history
  • Loading branch information
nomilous committed Jul 4, 2016
1 parent 5f494a6 commit 91d5be3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
8 changes: 5 additions & 3 deletions lib/services/pubsub/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ PubSubService.prototype.getAudienceGroup = function(channel, opts) {

if (opts.hasWildcard) return this['__listeners_wildcard_' + opts.targetAction];
return this['__listeners_' + opts.targetAction];

}

PubSubService.prototype.emitToAudience = function(publication, channel, opts) {
Expand Down Expand Up @@ -835,8 +835,10 @@ PubSubService.prototype.publish = function(message, payload) {

opts.hasWildcard = true; // all remaining emit attempts are to wildcard subscribers

var cache = this.happn.services.wildcardCache;

for (var allPath in this.__listeners_wildcard_ALL){
if (this.happn.utils.wildcardMatch(allPath.replace('/ALL@/','/*@/'), messageChannel)) {
if (this.happn.utils.wildcardMatch(cache, allPath.replace('/ALL@/','/*@/'), messageChannel)) {
this.emitToAudience(payload, allPath, opts);
}
}
Expand All @@ -845,7 +847,7 @@ PubSubService.prototype.publish = function(message, payload) {

var wildcardActionGroup = this['__listeners_wildcard_' + action];
for (var actionPath in wildcardActionGroup){
if (this.happn.utils.wildcardMatch(actionPath, messageChannel)){
if (this.happn.utils.wildcardMatch(cache, actionPath, messageChannel)){
this.emitToAudience(payload, actionPath, opts);
}
}
Expand Down
8 changes: 2 additions & 6 deletions lib/services/wildcard-cache/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ function WildcardCache(opts) {
}

WildcardCache.prototype.initialize = function(config, callback) {
if (!config.max) {
// don't create the LRU cache
// (its absence signifies a disabled WildcardCache)
return callback();
}
this.enabled = config.max ? true : false;
if (!this.enabled) return callback();
this.cache = LRU(config);
console.log(this.cache);
callback();
};

Expand Down
22 changes: 17 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ module.exports.clone = function(obj, circular){
// return cloned;
}

module.exports.wildcardMatch = function(pattern, matchTo){
var regex = new RegExp(pattern.replace(/[*]/g,'.*'));
var matchResult = matchTo.match(regex);
module.exports.wildcardMatch = function(cache, pattern, matchTo){
var key, regex, matchResult;

if (cache.enabled) {
key = pattern + "##" + matchTo;
matchResult = cache.get(key);
if (typeof matchResult !== 'undefined') {
return matchResult;
}
}

if (matchResult) return true;
return false;
regex = new RegExp(pattern.replace(/[*]/g,'.*'));
matchResult = matchTo.match(regex) ? true : false;

if (cache.enabled) {
cache.set(key, matchResult);
}
return matchResult;
}

module.exports.wildcardAggregate = function(wildcardDict){
Expand Down
14 changes: 7 additions & 7 deletions test/test-benchmark/9_multiple_subscribers_benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ describe(name, function() {
// require('benchmarket').start();
// after(require('benchmarket').store());

xcontext('with no cache and 20 same wildcard subscribers', function() {
context('with no cache and 20 same wildcard subscribers', function() {

subscriberCount = 20;
eventCount = 10;
Expand All @@ -218,7 +218,7 @@ describe(name, function() {

});

xcontext('with no cache and 200 same wildcard subscribers', function() {
context('with no cache and 200 same wildcard subscribers', function() {

subscriberCount = 200;
eventCount = 10;
Expand All @@ -233,8 +233,8 @@ describe(name, function() {

subscriberCount = 20;
eventCount = 10;
emitCount = 1000;
loglevel = 'info';
emitCount = 10000;
loglevel = 'warn';
wildcardCache = {};

testMultipleDifferentWildcardSubscribersRepeating(subscriberCount, eventCount, emitCount, loglevel, wildcardCache);
Expand All @@ -245,11 +245,11 @@ describe(name, function() {

subscriberCount = 20;
eventCount = 10;
emitCount = 1000;
loglevel = 'info';
emitCount = 10000;
loglevel = 'warn';
wildcardCache = {
config: {
max: 100
max: 1000
}
};

Expand Down

0 comments on commit 91d5be3

Please sign in to comment.