@@ -151,7 +151,8 @@ class ChannelWrap : public AsyncWrap{
151151
152152void Setup ();
153153void EnsureServers ();
154- void CleanupTimer ();
154+ void StartTimer ();
155+ void CloseTimer ();
155156
156157void ModifyActivityQueryCount (int count);
157158
@@ -313,13 +314,7 @@ void ares_sockstate_cb(void* data,
313314if (read || write){
314315if (!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 ();
323318
324319 task = ares_task_create (channel, sock);
325320if (task == nullptr ){
@@ -349,7 +344,7 @@ void ares_sockstate_cb(void* data,
349344 channel->env ()->CloseHandle (&task->poll_watcher , ares_poll_close_cb);
350345
351346if (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 }
491486
492487 library_inited_ = true ;
488+ }
493489
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 );
500499}
501500
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+ }
502508
503509ChannelWrap::~ChannelWrap (){
504510if (library_inited_){
@@ -508,17 +514,10 @@ ChannelWrap::~ChannelWrap(){
508514 }
509515
510516ares_destroy (channel_);
511- CleanupTimer ();
517+ CloseTimer ();
512518}
513519
514520
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-
522521void ChannelWrap::ModifyActivityQueryCount (int count){
523522 active_query_count_ += count;
524523if (active_query_count_ < 0 ) active_query_count_ = 0 ;
@@ -566,6 +565,7 @@ void ChannelWrap::EnsureServers(){
566565/* destroy channel and reset channel */
567566ares_destroy (channel_);
568567
568+ CloseTimer ();
569569Setup ();
570570}
571571
0 commit comments