强制 net/http 使用 c-ares 而不是 getaddrinfo()。

6
根据文档所述:

dns模块中的所有方法都使用C-Ares,除了使用线程池中的getaddrinfo(3)的dns.lookup。

然而,nethttp模块始终使用dns.lookup。是否有办法改变这种情况?getaddrinfo是同步的,而线程池只允许4个并发请求。
1个回答

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();
  });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接