Node.js 强制net/http使用c-ares而不是getaddrinfo()
正如政府所说: dns模块中的所有方法都使用C-Ares,dns.lookup除外,它在线程池中使用getaddrinfo(3)Node.js 强制net/http使用c-ares而不是getaddrinfo(),node.js,http,dns,Node.js,Http,Dns,正如政府所说: dns模块中的所有方法都使用C-Ares,dns.lookup除外,它在线程池中使用getaddrinfo(3) 但是,net和http模块始终使用dns.lookup。有没有办法改变这一点getaddrinfo是同步的,池将只允许4个并发请求。仔细研究后,似乎唯一的方法是手动执行DNS请求。我用这样的方式: var dns = require('dns'), http = require('http'); function httpRequest(options, b
但是,
net
和http
模块始终使用dns.lookup
。有没有办法改变这一点getaddrinfo
是同步的,池将只允许4个并发请求。仔细研究后,似乎唯一的方法是手动执行DNS请求。我用这样的方式:
var dns = require('dns'),
http = require('http');
function httpRequest(options, body, done) {
var hostname = options.hostname || options.host;
if (typeof(body) === 'function') {
done = body;
body = null;
}
// async resolve with C-ares
dns.resolve(hostname, function (err, addresses) {
if (err)
return done(err);
// Pass the host in the headers so the remote webserver
// can use the correct vhost.
var headers = options.headers || {};
headers.host = hostname;
options.headers = headers;
// pass the resolved address so http doesn't feel the need to
// call dns.lookup in a thread pool
options.hostname = addresses[0];
options.host = undefined;
var req = http.request(options, done);
req.on('error', done);
if (body)
req.write(body);
req.end();
});
}