Forked from jdurack/Node issue #5545 reproduction script
Last active
April 8, 2018 01:36
-
-
Save isaacs/5785971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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