Skip to content

Commit c7e918b

Browse files
oluandanielleadams
authored andcommitted
dns: add "tries" option to Resolve options
PR-URL: #39610 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 760cafa commit c7e918b

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

doc/api/dns.md

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ Create a new resolver.
108108
* `options` {Object}
109109
* `timeout` {integer} Query timeout in milliseconds, or `-1` to use the
110110
default timeout.
111+
* `tries` {integer} The number of tries the resolver will try contacting
112+
each name server before giving up, `4` by default.
111113

112114
### `resolver.cancel()`
113115
<!-- YAML

lib/internal/dns/promises.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
Resolver: CallbackResolver,
1313
validateHints,
1414
validateTimeout,
15+
validateTries,
1516
emitInvalidHostnameWarning,
1617
getDefaultVerbatim,
1718
} = require('internal/dns/utils');
@@ -216,7 +217,8 @@ const resolveMap = ObjectCreate(null);
216217
class Resolver {
217218
constructor(options = undefined) {
218219
const timeout = validateTimeout(options);
219-
this._handle = new ChannelWrap(timeout);
220+
const tries = validateTries(options);
221+
this._handle = new ChannelWrap(timeout, tries);
220222
}
221223
}
222224

lib/internal/dns/utils.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,18 @@ function validateTimeout(options) {
4343
return timeout;
4444
}
4545

46+
function validateTries(options) {
47+
const { tries = 4 } = { ...options };
48+
validateInt32(tries, 'options.tries', 1, 2 ** 31 - 1);
49+
return tries;
50+
}
51+
4652
// Resolver instances correspond 1:1 to c-ares channels.
4753
class Resolver {
4854
constructor(options = undefined) {
4955
const timeout = validateTimeout(options);
50-
this._handle = new ChannelWrap(timeout);
56+
const tries = validateTries(options);
57+
this._handle = new ChannelWrap(timeout, tries);
5158
}
5259

5360
cancel() {
@@ -209,6 +216,7 @@ module.exports = {
209216
setDefaultResolver,
210217
validateHints,
211218
validateTimeout,
219+
validateTries,
212220
Resolver,
213221
emitInvalidHostnameWarning,
214222
getDefaultVerbatim,

src/cares_wrap.cc

+14-5
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,14 @@ int ParseSoaReply(
631631
}
632632
} // anonymous namespace
633633

634-
ChannelWrap::ChannelWrap(Environment* env, Local<Object> object, int timeout)
634+
ChannelWrap::ChannelWrap(
635+
Environment* env,
636+
Local<Object> object,
637+
int timeout,
638+
int tries)
635639
: AsyncWrap(env, object, PROVIDER_DNSCHANNEL),
636-
timeout_(timeout) {
640+
timeout_(timeout),
641+
tries_(tries) {
637642
MakeWeak();
638643

639644
Setup();
@@ -647,11 +652,13 @@ void ChannelWrap::MemoryInfo(MemoryTracker* tracker) const {
647652

648653
void ChannelWrap::New(const FunctionCallbackInfo<Value>& args) {
649654
CHECK(args.IsConstructCall());
650-
CHECK_EQ(args.Length(), 1);
655+
CHECK_EQ(args.Length(), 2);
651656
CHECK(args[0]->IsInt32());
657+
CHECK(args[1]->IsInt32());
652658
const int timeout = args[0].As<Int32>()->Value();
659+
const int tries = args[1].As<Int32>()->Value();
653660
Environment* env = Environment::GetCurrent(args);
654-
new ChannelWrap(env, args.This(), timeout);
661+
new ChannelWrap(env, args.This(), timeout, tries);
655662
}
656663

657664
GetAddrInfoReqWrap::GetAddrInfoReqWrap(
@@ -704,6 +711,7 @@ void ChannelWrap::Setup() {
704711
options.sock_state_cb = ares_sockstate_cb;
705712
options.sock_state_cb_data = this;
706713
options.timeout = timeout_;
714+
options.tries = tries_;
707715

708716
int r;
709717
if (!library_inited_) {
@@ -717,7 +725,8 @@ void ChannelWrap::Setup() {
717725

718726
/* We do the call to ares_init_option for caller. */
719727
const int optmask =
720-
ARES_OPT_FLAGS | ARES_OPT_TIMEOUTMS | ARES_OPT_SOCK_STATE_CB;
728+
ARES_OPT_FLAGS | ARES_OPT_TIMEOUTMS |
729+
ARES_OPT_SOCK_STATE_CB | ARES_OPT_TRIES;
721730
r = ares_init_options(&channel_, &options, optmask);
722731

723732
if (r != ARES_SUCCESS) {

src/cares_wrap.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ struct NodeAresTask final : public MemoryRetainer {
147147

148148
class ChannelWrap final : public AsyncWrap {
149149
public:
150-
ChannelWrap(Environment* env, v8::Local<v8::Object> object, int timeout);
150+
ChannelWrap(
151+
Environment* env,
152+
v8::Local<v8::Object> object,
153+
int timeout,
154+
int tries);
151155
~ChannelWrap() override;
152156

153157
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -181,6 +185,7 @@ class ChannelWrap final : public AsyncWrap {
181185
bool is_servers_default_ = true;
182186
bool library_inited_ = false;
183187
int timeout_;
188+
int tries_;
184189
int active_query_count_ = 0;
185190
NodeAresTask::List task_list_;
186191
};

0 commit comments

Comments
 (0)