Below is my code. I init each array element with its index. Then I use async.each
to iterate over the array and call to retrieve url contents.
Request timeout is set to 500ms.
var async = require('async');
var request = require('request');
var logger = require('log4js').getLogger();
var url = "http://www.wordpress.com";
var arr=new Array(100);
for ( var i=0; i<arr.length; i++){ arr[i]=i; }
async.each(arr, function(a, cb) {
var ts1 = (new Date()).getTime();
request(url, {timeout: 500}, function( err, res, body ) {
var ts2 = (new Date()).getTime();
logger.debug(`a=${a}, dt=${ts2-ts1}`);
if ( err ) {
logger.debug(`Error: ${err}, dt=${ts2-ts1}`);
return cb(null);
}
else {
//logger.debug(`OK: ${a}`);
cb(null);
}
});
},
function( err, result) {
});
When array size is 100 I get 12 timeout errors:
[root@njs testreq]# node main.js | grep ETIME | wc -l
12
[root@njs testreq]#
When array size is 1000 I get 1000 timeout errors:
[root@njs testreq]# node main.js | grep ETIME | wc -l
1000
[root@njs testreq]#
What is the cause? How can I avoid it?