From a0898649370be3e841f986d6576923816a3756f4 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 19 May 2020 22:39:58 +0200 Subject: [PATCH 1/4] dns: make dns.Resolver timeout configurable --- doc/api/dns.md | 16 +++++++ lib/internal/dns/promises.js | 6 ++- lib/internal/dns/utils.js | 16 ++++++- src/cares_wrap.cc | 26 +++++++---- test/parallel/test-dns-channel-timeout.js | 53 +++++++++++++++++++++++ 5 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 test/parallel/test-dns-channel-timeout.js diff --git a/doc/api/dns.md b/doc/api/dns.md index 030bb7674b0cca..7ec5d487f3f221 100644 --- a/doc/api/dns.md +++ b/doc/api/dns.md @@ -92,6 +92,22 @@ The following methods from the `dns` module are available: * [`resolver.reverse()`][`dns.reverse()`] * [`resolver.setServers()`][`dns.setServers()`] +### `Resolver([options])` + + +Create a new resolver. + +* `options` {Object} + * `timeout` {integer} Query timeout in milliseconds, or `-1` to use the + default timeout. + ### `resolver.cancel()` From eae2cfc7e1260b75a39c51f9984a7200de96f282 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 19 May 2020 22:57:21 +0200 Subject: [PATCH 3/4] fixup! clamp timeout at 1000 ms otherwise it's a possible observable change in behavior --- src/cares_wrap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 467c3e20b203f0..4a332db71279a4 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -502,7 +502,7 @@ void ChannelWrap::StartTimer() { } int timeout = timeout_; if (timeout == 0) timeout = 1; - if (timeout < 0) timeout = 1000; + if (timeout < 0 || timeout > 1000) timeout = 1000; uv_timer_start(timer_handle_, AresTimeout, timeout, timeout); } From 7941e8456faf755e3f0d52b56aaef3eb1a390135 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 28 May 2020 23:06:30 +0200 Subject: [PATCH 4/4] Update lib/internal/dns/utils.js Co-authored-by: James M Snell --- lib/internal/dns/utils.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index b6d4381114b34c..6642a4f090bf67 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -25,11 +25,8 @@ const { } = errors.codes; function validateTimeout(options) { - let timeout = -1; // Use c-ares's default timeout. - if (options != null && typeof options === 'object' && 'timeout' in options) { - timeout = options.timeout; - validateInt32(timeout, 'options.timeout', -1, 2 ** 31 - 1); - } + const { timeout = -1 } = { ...options }; + validateInt32(timeout, 'options.timeout', -1, 2 ** 31 - 1); return timeout; }