Forcing net / http to use c-ares instead of getaddrinfo ()

As the doc says:

All methods in the dns module use C-Ares, with the exception of dns.lookup, which uses getaddrinfo (3) in the thread pool.

However, the net and http modules always use dns.lookup . Is there any way to change this? getaddrinfo is synchronous, and the pool will only allow 4 simultaneous requests.

+6
source share
1 answer

Looking at this seems to be the only way to make a DNS query manually. I am using something like this:

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

Source: https://habr.com/ru/post/955258/


All Articles