◐ Shell
clean mode source ↗

src: do not persist timer handle in cares_wrap · nodejs/node@685b9b2

@@ -151,7 +151,8 @@ class ChannelWrap : public AsyncWrap {

151151152152

void Setup();

153153

void EnsureServers();

154-

void CleanupTimer();

154+

void StartTimer();

155+

void CloseTimer();

155156156157

void ModifyActivityQueryCount(int count);

157158

@@ -313,13 +314,7 @@ void ares_sockstate_cb(void* data,

313314

if (read || write) {

314315

if (!task) {

315316

/* 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();

323318324319

task = ares_task_create(channel, sock);

325320

if (task == nullptr) {

@@ -349,7 +344,7 @@ void ares_sockstate_cb(void* data,

349344

channel->env()->CloseHandle(&task->poll_watcher, ares_poll_close_cb);

350345351346

if (channel->task_list()->empty()) {

352-

uv_timer_stop(channel->timer_handle());

347+

channel->CloseTimer();

353348

}

354349

}

355350

}

@@ -490,15 +485,26 @@ void ChannelWrap::Setup() {

490485

}

491486492487

library_inited_ = true;

488+

}

493489494-

/* 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);

500499

}

501500501+

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+

}

502508503509

ChannelWrap::~ChannelWrap() {

504510

if (library_inited_) {

@@ -508,17 +514,10 @@ ChannelWrap::~ChannelWrap() {

508514

}

509515510516

ares_destroy(channel_);

511-

CleanupTimer();

517+

CloseTimer();

512518

}

513519514520515-

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-522521

void ChannelWrap::ModifyActivityQueryCount(int count) {

523522

active_query_count_ += count;

524523

if (active_query_count_ < 0) active_query_count_ = 0;

@@ -566,6 +565,7 @@ void ChannelWrap::EnsureServers() {

566565

/* destroy channel and reset channel */

567566

ares_destroy(channel_);

568567568+

CloseTimer();

569569

Setup();

570570

}

571571