Skip to content

Commit

Permalink
Merge pull request #325 from rollbar/maxitems
Browse files Browse the repository at this point in the history
fix the rate limiter payload
  • Loading branch information
rokob authored Jun 29, 2017
2 parents 4be9ab3 + bd2fb7f commit 0e945e7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/browser/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var errorParser = require('./errorParser');
function Rollbar(options, client) {
this.options = _.extend(true, defaultOptions, options);
var api = new API(this.options, transport, urllib);
this.client = client || new Client(this.options, api, logger);
this.client = client || new Client(this.options, api, logger, 'browser');
addTransformsToNotifier(this.client.notifier);
addPredicatesToQueue(this.client.queue);
if (this.options.captureUncaught) {
Expand Down
47 changes: 36 additions & 11 deletions src/rateLimiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function RateLimiter(options) {
this.startTime = (new Date()).getTime();
this.counter = 0;
this.perMinCounter = 0;
this.platform = null;
this.platformOptions = {};
this.configureGlobal(options);
}

Expand Down Expand Up @@ -62,15 +64,20 @@ RateLimiter.prototype.shouldSend = function(item, now) {
var globalRateLimitPerMin = RateLimiter.globalSettings.itemsPerMinute;

if (checkRate(item, globalRateLimit, this.counter)) {
return shouldSendValue(globalRateLimit + ' max items reached', false);
return shouldSendValue(this.platform, this.platformOptions, globalRateLimit + ' max items reached', false);
} else if (checkRate(item, globalRateLimitPerMin, this.perMinCounter)) {
return shouldSendValue(globalRateLimitPerMin + ' items per minute reached', false);
return shouldSendValue(this.platform, this.platformOptions, globalRateLimitPerMin + ' items per minute reached', false);
}
this.counter++;
this.perMinCounter++;

var shouldSend = !checkRate(item, globalRateLimit, this.counter);
return shouldSendValue(null, shouldSend, globalRateLimit);
return shouldSendValue(this.platform, this.platformOptions, null, shouldSend, globalRateLimit);
};

RateLimiter.prototype.setPlatformOptions = function(platform, options) {
this.platform = platform;
this.platformOptions = options;
};

/* Helpers */
Expand All @@ -79,25 +86,43 @@ function checkRate(item, limit, counter) {
return !item.ignoreRateLimit && limit >= 1 && counter >= limit;
}

function shouldSendValue(error, shouldSend, globalRateLimit) {
function shouldSendValue(platform, options, error, shouldSend, globalRateLimit) {
var payload = null;
if (error) {
error = new Error(error);
}
if (!error && !shouldSend) {
payload = rateLimitPayload(globalRateLimit);
payload = rateLimitPayload(platform, options, globalRateLimit);
}
return {error: error, shouldSend: shouldSend, payload: payload};
}

function rateLimitPayload(globalRateLimit) {
return {
message: 'maxItems has been hit. Ignoring errors until reset.',
err: null,
custom: {
maxItems: globalRateLimit
function rateLimitPayload(platform, options, globalRateLimit) {
var environment = options.environment || (options.payload && options.payload.environment);
var item = {
body: {
message: {
body: 'maxItems has been hit. Ignoring errors until reset.',
extra: {
maxItems: globalRateLimit
}
}
},
language: 'javascript',
environment: environment,
notifier: {
version: (options.notifier && options.notifier.version) || options.version
}
};
if (platform === 'browser') {
item.platform = 'browser';
item.framework = 'browser-js';
item.notifier.name = 'rollbar-browser-js';
} else if (platform === 'server') {
item.framework = options.framework || 'node-js';
item.notifier.name = options.notifier.name;
}
return item;
}

module.exports = RateLimiter;
3 changes: 2 additions & 1 deletion src/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ var _ = require('./utility');
* @param api
* @param logger
*/
function Rollbar(options, api, logger) {
function Rollbar(options, api, logger, platform) {
this.options = _.extend(true, {}, options);
this.logger = logger;
Rollbar.rateLimiter.setPlatformOptions(platform, options);
this.queue = new Queue(Rollbar.rateLimiter, api, logger, this.options);
this.notifier = new Notifier(this.queue, this.options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/rollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Rollbar(options, client) {
this.options = _.extend(true, {}, Rollbar.defaultOptions, options);
this.options.environment = this.options.environment || 'unspecified';
var api = new API(this.options, transport, urllib, jsonBackup);
this.client = client || new Client(this.options, api, logger);
this.client = client || new Client(this.options, api, logger, 'server');
addTransformsToNotifier(this.client.notifier);
addPredicatesToQueue(this.client.queue);

Expand Down
2 changes: 1 addition & 1 deletion test/rateLimiter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('shouldSend', function() {
expect(result4.shouldSend).to.not.be.ok();
expect(result4.error).to.not.be.ok();
expect(result4.payload).to.be.ok();
expect(result4.payload.custom.maxItems).to.eql(options.maxItems);
expect(result4.payload.body.message.extra.maxItems).to.eql(options.maxItems);

done();
});
Expand Down

0 comments on commit 0e945e7

Please sign in to comment.