@@ -151,7 +151,8 @@ class ChannelWrap : public AsyncWrap { |
151 | 151 |
|
152 | 152 | void Setup(); |
153 | 153 | void EnsureServers(); |
154 | | - void CleanupTimer(); |
| 154 | + void StartTimer(); |
| 155 | + void CloseTimer(); |
155 | 156 |
|
156 | 157 | void ModifyActivityQueryCount(int count); |
157 | 158 |
|
@@ -313,13 +314,7 @@ void ares_sockstate_cb(void* data, |
313 | 314 | if (read || write) { |
314 | 315 | if (!task) { |
315 | 316 | /* New socket */ |
316 | | - |
317 | | - /* If this is the first socket then start the timer. */ |
318 | | - uv_timer_t* timer_handle = channel->timer_handle(); |
319 | | - if (!uv_is_active(reinterpret_cast<uv_handle_t*>(timer_handle))) { |
320 | | - CHECK(channel->task_list()->empty()); |
321 | | - uv_timer_start(timer_handle, ChannelWrap::AresTimeout, 1000, 1000); |
322 | | - } |
| 317 | + channel->StartTimer(); |
323 | 318 |
|
324 | 319 | task = ares_task_create(channel, sock); |
325 | 320 | if (task == nullptr) { |
@@ -349,7 +344,7 @@ void ares_sockstate_cb(void* data, |
349 | 344 | channel->env()->CloseHandle(&task->poll_watcher, ares_poll_close_cb); |
350 | 345 |
|
351 | 346 | if (channel->task_list()->empty()) { |
352 | | - uv_timer_stop(channel->timer_handle()); |
| 347 | + channel->CloseTimer(); |
353 | 348 | } |
354 | 349 | } |
355 | 350 | } |
@@ -490,15 +485,26 @@ void ChannelWrap::Setup() { |
490 | 485 | } |
491 | 486 |
|
492 | 487 | library_inited_ = true; |
| 488 | +} |
493 | 489 |
|
494 | | - /* Initialize the timeout timer. The timer won't be started until the */ |
495 | | - /* first socket is opened. */ |
496 | | - CleanupTimer(); |
497 | | - timer_handle_ = new uv_timer_t(); |
498 | | - timer_handle_->data = static_cast<void*>(this); |
499 | | - uv_timer_init(env()->event_loop(), timer_handle_); |
| 490 | +void ChannelWrap::StartTimer() { |
| 491 | + if (timer_handle_ == nullptr) { |
| 492 | + timer_handle_ = new uv_timer_t(); |
| 493 | + timer_handle_->data = static_cast<void*>(this); |
| 494 | + uv_timer_init(env()->event_loop(), timer_handle_); |
| 495 | + } else if (uv_is_active(reinterpret_cast<uv_handle_t*>(timer_handle_))) { |
| 496 | + return; |
| 497 | + } |
| 498 | + uv_timer_start(timer_handle_, AresTimeout, 1000, 1000); |
500 | 499 | } |
501 | 500 |
|
| 501 | +void ChannelWrap::CloseTimer() { |
| 502 | + if (timer_handle_ == nullptr) |
| 503 | + return; |
| 504 | + |
| 505 | + env()->CloseHandle(timer_handle_, [](uv_timer_t* handle) { delete handle; }); |
| 506 | + timer_handle_ = nullptr; |
| 507 | +} |
502 | 508 |
|
503 | 509 | ChannelWrap::~ChannelWrap() { |
504 | 510 | if (library_inited_) { |
@@ -508,17 +514,10 @@ ChannelWrap::~ChannelWrap() { |
508 | 514 | } |
509 | 515 |
|
510 | 516 | ares_destroy(channel_); |
511 | | - CleanupTimer(); |
| 517 | + CloseTimer(); |
512 | 518 | } |
513 | 519 |
|
514 | 520 |
|
515 | | -void ChannelWrap::CleanupTimer() { |
516 | | - if (timer_handle_ == nullptr) return; |
517 | | - |
518 | | - env()->CloseHandle(timer_handle_, [](uv_timer_t* handle) { delete handle; }); |
519 | | - timer_handle_ = nullptr; |
520 | | -} |
521 | | - |
522 | 521 | void ChannelWrap::ModifyActivityQueryCount(int count) { |
523 | 522 | active_query_count_ += count; |
524 | 523 | if (active_query_count_ < 0) active_query_count_ = 0; |
@@ -566,6 +565,7 @@ void ChannelWrap::EnsureServers() { |
566 | 565 | /* destroy channel and reset channel */ |
567 | 566 | ares_destroy(channel_); |
568 | 567 |
|
| 568 | + CloseTimer(); |
569 | 569 | Setup(); |
570 | 570 | } |
571 | 571 |
|
|