Skip to content

Instantly share code, notes, and snippets.

@CMarker
Created July 13, 2015 02:36
Show Gist options
  • Save CMarker/c30dd1b44922799f62a0 to your computer and use it in GitHub Desktop.
Save CMarker/c30dd1b44922799f62a0 to your computer and use it in GitHub Desktop.
var request = require('request');
request = request.defaults({
pool: {maxSockets: 100},
timeout: 30000
});
var RateLimiter = require('limiter').RateLimiter;
var limiter = new RateLimiter(1, 100);
var throttledRequest = function(options, cb) {
limiter.removeTokens(1, function() {
request(options, cb);
});
};
function getData(query, cb) {
var options = {
url: "https://www.website.com" + query,
json: true
};
throttledRequest(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
cb(error, body);
} else {
cb(error);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment