@@ -152,7 +152,7 @@ using node_ares_task_list =
152
152
153
153
class ChannelWrap : public AsyncWrap {
154
154
public:
155
- ChannelWrap (Environment* env, Local<Object> object);
155
+ ChannelWrap (Environment* env, Local<Object> object, int timeout );
156
156
~ChannelWrap () override ;
157
157
158
158
static void New (const FunctionCallbackInfo<Value>& args);
@@ -190,18 +190,21 @@ class ChannelWrap : public AsyncWrap {
190
190
bool query_last_ok_;
191
191
bool is_servers_default_;
192
192
bool library_inited_;
193
+ int timeout_;
193
194
int active_query_count_;
194
195
node_ares_task_list task_list_;
195
196
};
196
197
197
198
ChannelWrap::ChannelWrap (Environment* env,
198
- Local<Object> object)
199
+ Local<Object> object,
200
+ int timeout)
199
201
: AsyncWrap(env, object, PROVIDER_DNSCHANNEL),
200
202
timer_handle_ (nullptr ),
201
203
channel_(nullptr ),
202
204
query_last_ok_(true ),
203
205
is_servers_default_(true ),
204
206
library_inited_(false ),
207
+ timeout_(timeout),
205
208
active_query_count_(0 ) {
206
209
MakeWeak ();
207
210
@@ -210,10 +213,11 @@ ChannelWrap::ChannelWrap(Environment* env,
210
213
211
214
void ChannelWrap::New (const FunctionCallbackInfo<Value>& args) {
212
215
CHECK (args.IsConstructCall ());
213
- CHECK_EQ (args.Length (), 0 );
214
-
216
+ CHECK_EQ (args.Length (), 1 );
217
+ CHECK (args[0 ]->IsInt32 ());
218
+ const int timeout = args[0 ].As <Int32>()->Value ();
215
219
Environment* env = Environment::GetCurrent (args);
216
- new ChannelWrap (env, args.This ());
220
+ new ChannelWrap (env, args.This (), timeout );
217
221
}
218
222
219
223
class GetAddrInfoReqWrap : public ReqWrap <uv_getaddrinfo_t > {
@@ -462,6 +466,7 @@ void ChannelWrap::Setup() {
462
466
options.flags = ARES_FLAG_NOCHECKRESP;
463
467
options.sock_state_cb = ares_sockstate_cb;
464
468
options.sock_state_cb_data = this ;
469
+ options.timeout = timeout_;
465
470
466
471
int r;
467
472
if (!library_inited_) {
@@ -474,9 +479,9 @@ void ChannelWrap::Setup() {
474
479
}
475
480
476
481
/* We do the call to ares_init_option for caller. */
477
- r = ares_init_options (&channel_,
478
- &options,
479
- ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB );
482
+ const int optmask =
483
+ ARES_OPT_FLAGS | ARES_OPT_TIMEOUTMS | ARES_OPT_SOCK_STATE_CB;
484
+ r = ares_init_options (&channel_, &options, optmask );
480
485
481
486
if (r != ARES_SUCCESS) {
482
487
Mutex::ScopedLock lock (ares_library_mutex);
@@ -495,7 +500,10 @@ void ChannelWrap::StartTimer() {
495
500
} else if (uv_is_active (reinterpret_cast <uv_handle_t *>(timer_handle_))) {
496
501
return ;
497
502
}
498
- uv_timer_start (timer_handle_, AresTimeout, 1000 , 1000 );
503
+ int timeout = timeout_;
504
+ if (timeout == 0 ) timeout = 1 ;
505
+ if (timeout < 0 || timeout > 1000 ) timeout = 1000 ;
506
+ uv_timer_start (timer_handle_, AresTimeout, timeout, timeout);
499
507
}
500
508
501
509
void ChannelWrap::CloseTimer () {
0 commit comments