Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for rateLimitExceeded and userRateLimitExceeded errors #473

Merged
merged 1 commit into from
Mar 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,24 @@ module.exports.getNextRetryWait = getNextRetryWait;
* @return {boolean} True if the API request should be retried, false otherwise.
*/
function shouldRetry(err) {
return !!err && [429, 500, 503].indexOf(err.code) !== -1;
if (err) {
if ([429, 500, 503].indexOf(err.code) !== -1) {
return true;
}

if (err.errors) {
for (var i in err.errors) {
var reason = err.errors[i].reason;
if (reason === 'rateLimitExceeded') {
return true;
} else if (reason === 'userRateLimitExceeded') {
return true;
}
}
}
}

return false;
}

module.exports.shouldRetryErr = shouldRetry;
Expand Down
72 changes: 72 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,78 @@ describe('common/util', function() {
});
});

it('should retry rate limits on rateLimitExceeded', function(done) {
var attemptedRetries = 0;
var error = new Error('Rate Limit Error.');
error.code = 403; // not a rate limit code!
error.errors = [{ reason: 'rateLimitExceeded' }];

var authorizedReqOpts = { a: 'b', c: 'd' };

var old_setTimeout = setTimeout;
setTimeout = function(callback, time) {
var MIN_TIME = (Math.pow(2, attemptedRetries) * 1000);
var MAX_TIME = (Math.pow(2, attemptedRetries) * 1000) + 1000;
assert(time >= MIN_TIME && time <= MAX_TIME);
attemptedRetries++;
callback(); // make the request again
};

gsa_Override = function() {
return function authorize(reqOpts, callback) {
callback(null, authorizedReqOpts);
};
};

request_Override = function(reqOpts, callback) {
callback(null, null, { error: error });
};

var makeRequest = util.makeAuthorizedRequest({});
makeRequest({}, function(err) {
setTimeout = old_setTimeout;
assert.equal(attemptedRetries, 3);
assert.equal(err.message, 'Rate Limit Error.');
done();
});
});

it('should retry rate limits on userRateLimitExceeded', function(done) {
var attemptedRetries = 0;
var error = new Error('Rate Limit Error.');
error.code = 403; // not a rate limit code!
error.errors = [{ reason: 'userRateLimitExceeded' }];

var authorizedReqOpts = { a: 'b', c: 'd' };

var old_setTimeout = setTimeout;
setTimeout = function(callback, time) {
var MIN_TIME = (Math.pow(2, attemptedRetries) * 1000);
var MAX_TIME = (Math.pow(2, attemptedRetries) * 1000) + 1000;
assert(time >= MIN_TIME && time <= MAX_TIME);
attemptedRetries++;
callback(); // make the request again
};

gsa_Override = function() {
return function authorize(reqOpts, callback) {
callback(null, authorizedReqOpts);
};
};

request_Override = function(reqOpts, callback) {
callback(null, null, { error: error });
};

var makeRequest = util.makeAuthorizedRequest({});
makeRequest({}, function(err) {
setTimeout = old_setTimeout;
assert.equal(attemptedRetries, 3);
assert.equal(err.message, 'Rate Limit Error.');
done();
});
});

it('should retry rate limits 3x by default', function(done) {
var attemptedRetries = 0;
var error = new Error('Rate Limit Error.');
Expand Down