Permalink
Browse files

resume REST api request after rate limit reset time

  • Loading branch information...
1 parent c2f762e commit d37146dda41df7079d6147c1e96aaccbeccd7c26 @mef mef committed Jan 17, 2013
Showing with 12 additions and 1 deletion.
  1. +12 −1 lib/tuiter.js
View
@@ -122,7 +122,18 @@ var APIRequest = function(endpoint, params, callback) {
} else {
req.end(function(res){
debug('data received for %s', endpoint.resource);
- callback(res.error, res.body);
+ if (res.statusCode == 429) {
+ sleep_duration = (res.header['x-rate-limit-reset']*1000 - new Date()) + 7000;
+ debug('rate limit reached, sleeping during', Math.ceil(sleep_duration/60000), 'minutes');
+ setTimeout(function() {
+ debug('rate limit reset, resuming extraction...');
+ APIRequest(endpoint, params, callback);
+ }
+ , sleep_duration);
+ }
+ else {
+ callback(res.error, res.body);
+ }
});
}
};

0 comments on commit d37146d

Please sign in to comment.