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

Fix rate limit requests #472

Merged
merged 2 commits 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
16 changes: 9 additions & 7 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,15 @@ function makeAuthorizedRequest(config) {
}

function handleRateLimitResp(err, res, body) {
if (shouldRetry(err) && autoRetry && MAX_RETRIES > attemptedRetries) {
setTimeout(function() {
request(authorizedReqOpts, handleRateLimitResp);
}, getNextRetryWait(attemptedRetries++));
} else {
handleResp(err, res, body, callback);
}
handleResp(err, res, body, function(err, body, resp) {
if (shouldRetry(err) && autoRetry && MAX_RETRIES > attemptedRetries) {
setTimeout(function() {
request(authorizedReqOpts, handleRateLimitResp);
}, getNextRetryWait(attemptedRetries++));
} else {
callback(err, body, resp);
}
});
}

if (callback.onAuthorized) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@
"docs": "./scripts/docs.sh",
"lint": "jshint lib/ regression/ test/",
"test": "mocha test/*",
"regression-test": "mocha regression/* --timeout 20000",
"cover": "istanbul cover -x 'regression/*' _mocha -- --timeout 20000 test/* regression/*",
"coveralls": "istanbul cover -x 'regression/*' _mocha --report lcovonly -- --timeout 20000 test/* regression/* -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
"regression-test": "mocha regression/* --timeout 30000",
"cover": "istanbul cover -x 'regression/*' _mocha -- --timeout 30000 test/* regression/*",
"coveralls": "istanbul cover -x 'regression/*' _mocha --report lcovonly -- --timeout 30000 test/* regression/* -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"license": "Apache 2"
}
37 changes: 37 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,43 @@ describe('common/util', function() {
});
});

it('should retry rate limits on API errors', function(done) {
var attemptedRetries = 0;
var codes = [429, 503, 500, 'done'];
var error = new Error('Rate Limit Error.');
error.code = codes[0]; // Rate limit error

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++;
error.code = codes[attemptedRetries]; // test a new code
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(err.message, 'Rate Limit Error.');
assert.equal(err.code, 'done');
done();
});
});

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