我正在尝试使用 NODE.JS 测试服务器可用性,例如服务器监控。 我在其中创建了包含 5000域的测试CSV文件,并使用了以下代码:
var net = require('net');
function Daemon(address,name, port) {
this.address = address;
this.name = name;
this.port = port;
}
var fs = require('fs');
var array = fs.readFileSync('domains.csv').toString().split("\n");
for(i in array) {
console.log(array[i]);
daemons.push(new Daemon(array[i],'http', 80));
}
// loop through each daemon in the array
daemons.forEach(function(d) {
// create the TCP stream to the server
var stream = net.createConnection(d.port, d.address);
console.log('[' + d.address + ']\t connected');
// listen for connection
stream.on('connect', function(){
// connection success
//console.log('[' + d.name + ']\t connected');
stream.end(); // close the stream
});
// listen for any errors
stream.on('error', function(error){
console.log('[' + d.name + ']\t error: ' + error);
stream.destroy(); // close the stream
});
});
特定域名有很多超时:
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: getaddrinfo ENOTFOUND
[http] error: Error: connect ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: read ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: read ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
[http] error: Error: read ETIMEDOUT
[http] error: Error: connect ETIMEDOUT
但这段代码很慢。完成它需要将近30分钟。如何加速此代码(异步连接)?
我需要脚本能够每5分钟测试 100.000个域名
答案 0 :(得分:0)
我的猜测是,在某一点上,打开5000个出站TCP连接对您的系统不利。您应该将并发连接尝试限制为合理的数量。
我建议你使用async.queue并发为100或200。