简体   繁体   中英

Node.js DNS lookup - how to set timeout?

I'm very new to Node.js and I'm having an issue using node.dns.resolveNs function.

Some domains are completely down and it takes about a minute to get the response, which is usually "queryNs ETIMEOUT". Is there a way for me to set it to a shorter period, for example 10 seconds?

I am not sure of any way to set a timeout directly on the function call, but you could create a small wrapper around the call to handle timing out yourself:

var dns = require('dns');

var nsLookup = function(domain, timeout, callback) {
  var callbackCalled = false;
  var doCallback = function(err, domains) {
    if (callbackCalled) return;
    callbackCalled = true;
    callback(err, domains);
  };

  setTimeout(function() {
    doCallback(new Error("Timeout exceeded"), null);
  }, timeout);

  dns.resolveNs(domain, doCallback);
};

nsLookup('stackoverflow.com', 1000, function(err, addresses) {
  console.log("Results for stackoverflow.com, timeout 1000:");
  if (err) {
    console.log("Err: " + err);
    return;
  }
  console.log(addresses);
});

nsLookup('stackoverflow.com', 1, function(err, addresses) {
  console.log("Results for stackoverflow.com, timeout 1:");
  if (err) {
    console.log("Err: " + err);
    return;
  }
  console.log(addresses);
});

The output for the above script:

Results for stackoverflow.com, timeout 1:
Err: Error: Timeout exceeded
Results for stackoverflow.com, timeout 1000:
[ 'ns1.serverfault.com',
  'ns2.serverfault.com',
  'ns3.serverfault.com' ]

Node.js dns.resolve* use c-ares library underneath, which supports timeouts and various other options natively. Unfortunately Node.js doesn't expose those tunables, but some of them can be set via RES_OPTIONS environment variable.

Example: RES_OPTIONS='ndots:3 retrans:1000 retry:3 rotate' node server.js

  • ndots : same as ARES_OPT_NDOTS
  • retrans : same as ARES_OPT_TIMEOUTMS
  • retry : same as ARES_OPT_TRIES
  • rotate : same as ARES_OPT_ROTATE

See man ares_init_options(3) for details what each option means, for instance here http://manpages.ubuntu.com/manpages/zesty/man3/ares_init_options.3.html

Also it good to know that lookup may block your application .

We developed a module that replaces/extends node's dns.lookup method. The main goal is to bypass issues with blocking of thread pool. So module caches responses, has multi-records resolving and TTL support. Also we have good unit and functional tests with 100% coverage . Module was tested in production and highload environments. Under MIT license.

Here it is: https://github.com/LCMApps/dns-lookup-cache

I believe it may help!

building upon @redbaron's answer, you can set the RES_OPTIONS variable during runtime to set the timeout for the c-ares library used by dns.resolve* :

// this will timeout for (1000 * 3 * 2) ms
process.env.RES_OPTIONS='ndots:3 retrans:1000 retry:3 rotate';

Wrapper for resolveNs with timeout:

const dns = require("dns");

const nsLookup = (domain, timeout) => {
  return new Promise((resolve, reject) => {
    let finished = false;

    const timer = setTimeout(()=>{
      finished = true; 
      reject();
    }, timeout);

    const callback = (_err, result) => {
      clearTimeout(timer);
      if (!finished) resolve(result);
    };
  
    dns.resolveNs(domain, callback);  
  });
};

Use the Resolver object, which takes a timeout params in it's constructor args...

eg

const { Resolver }= require('dns').promises;

const yourTimeout = 5000; // milliseconds

const resolver = new Resolver({timeout: yourTimeout});

const response = await resolver.resolve('www.google.com');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM