Skip to content

Commit 367ce5a

Browse files
committed
update
1 parent a9bc7df commit 367ce5a

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

ext/src/http/client/curl/http_client_curl.cc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -352,29 +352,20 @@ void HttpClient::MaybeSpawnBackgroundThread()
352352
int still_running = 1;
353353
std::chrono::system_clock::time_point last_free_job_timepoint =
354354
std::chrono::system_clock::now();
355+
bool need_wait_more = false;
355356
while (true)
356357
{
357358
CURLMsg *msg;
358359
int queued;
359360
CURLMcode mc = curl_multi_perform(self->multi_handle_, &still_running);
360361

361-
std::chrono::milliseconds wait_for;
362-
#if LIBCURL_VERSION_NUM >= 0x074200
363-
// only avaliable with curl_multi_poll
364-
wait_for = self->background_thread_wait_for_;
365-
#endif
366-
if (self->is_shutdown.load(std::memory_order_acquire))
367-
{
368-
wait_for = std::chrono::milliseconds{0};
369-
}
370-
371362
// According to https://curl.se/libcurl/c/curl_multi_perform.html, when mc is not OK, we
372363
// can not curl_multi_perform it again
373364
if (mc != CURLM_OK)
374365
{
375366
self->resetMultiHandle();
376367
}
377-
else if (still_running || now - last_free_job_timepoint < wait_for)
368+
else if (still_running || need_wait_more)
378369
{
379370
// curl_multi_poll is added from libcurl 7.66.0, before 7.68.0, we can only wait util
380371
// timeout to do the rest jobs
@@ -433,13 +424,31 @@ void HttpClient::MaybeSpawnBackgroundThread()
433424
still_running = 1;
434425
}
435426

427+
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
436428
if (still_running > 0)
437429
{
438430
last_free_job_timepoint = now;
431+
need_wait_more = false;
432+
continue;
433+
}
434+
435+
std::chrono::milliseconds wait_for;
436+
#if LIBCURL_VERSION_NUM >= 0x074200
437+
// only avaliable with curl_multi_poll
438+
wait_for = self->background_thread_wait_for_;
439+
#endif
440+
if (self->is_shutdown.load(std::memory_order_acquire))
441+
{
442+
wait_for = std::chrono::milliseconds{0};
443+
}
444+
445+
if (now - last_free_job_timepoint < wait_for)
446+
{
447+
need_wait_more = true;
439448
continue;
440449
}
441450

442-
if (still_running == 0 && now - last_free_job_timepoint > wait_for)
451+
if (still_running == 0)
443452
{
444453
std::lock_guard<std::mutex> lock_guard{self->background_thread_m_};
445454
// Double check, make sure no more pending sessions after locking background thread

ext/test/http/curl_http_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,21 @@ TEST_F(BasicCurlHttpTests, FinishInAsyncCallback)
526526
}
527527
}
528528
}
529+
530+
TEST_F(BasicCurlHttpTests, ElegantQuitQuick)
531+
{
532+
auto http_client = http_client::HttpClientFactory::Create();
533+
std::dynamic_pointer_cast<curl::HttpClient>(http_client)->MaybeSpawnBackgroundThread();
534+
auto beg = std::chrono::system_clock::now();
535+
auto session = http_client->CreateSession("http://127.0.0.1:19000/get/");
536+
auto request = session->CreateRequest();
537+
request->SetUri("get/");
538+
auto handler = std::make_shared<GetEventHandler>();
539+
session->SendRequest(handler);
540+
http_client->FinishAllSessions();
541+
http_client.reset();
542+
// when use background_thread_wait_for_ should have no side effort on elegant quit
543+
ASSERT_TRUE(std::chrono::system_clock::now() - beg < std::chrono::milliseconds{5});
544+
ASSERT_TRUE(handler->is_called_);
545+
ASSERT_TRUE(handler->got_response_);
546+
}

0 commit comments

Comments
 (0)