Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/5785971 to your computer and use it in GitHub Desktop.
Save isaacs/5785971 to your computer and use it in GitHub Desktop.
var http = require('http');
var timeInterval = 20; //ms
var maxOutstandingRequests = 10;
var successCount = 0;
var outstandingRequests = 0;
var options = {
hostname: 'www.google.com'
, agent: false
};
var makeRequest = function() {
if ( outstandingRequests >= maxOutstandingRequests ) {
return;
}
outstandingRequests++;
var req = http.get(options, function(res) {
if ( ! res || ! res.statusCode || res.statusCode !== 200 ) {
outstandingRequests--;
console.log('Fail. successCount: ', successCount, ', outstandingRequests: ', outstandingRequests);
} else {
res.resume(); // or res.on('data', fn) or repeatedly call res.read() to pull the data out
res.on('end', function() {
outstandingRequests--;
successCount++;
});
}
});
req.on('error', function(e) {
outstandingRequests--;
console.log('error: ', e.message, ', successCount: ', successCount);
});
};
setInterval(makeRequest, timeInterval);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment