Skip to content

Commit

Permalink
enable connection reuse in libcurl
Browse files Browse the repository at this point in the history
Originally, in the `HttpClient::perform()` method, a new `CurlHandle` was created
upon every invocation, leading to subsequent calls to `curl_easy_init()` and
`curl_easy_cleanup()`, as the variable went in and out of scope while it executed
on the `asio::thread_pool`. This meant that there was no opportunity to reuse
connections from the underlying connection pool, because it was always cleaned up.

By marking the `CurlHandle` with the `thread_local` storage duration, it ensures
that the same one remains in use for each thread, thus providing the means to
reuse the established connection pool.

Fixes Netflix-Skunkworks#51.
  • Loading branch information
copperlight committed Oct 19, 2023
1 parent 79a2713 commit 802819e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions spectator/http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ class CurlHandle {

auto handle() const noexcept -> CURL* { return handle_; }

auto perform() -> CURLcode { return curl_easy_perform(handle()); }
auto perform() -> CURLcode {
if (response_.size() > 0) {
response_.clear();
}
return curl_easy_perform(handle());
}

auto set_opt(CURLoption option, const void* param) -> CURLcode {
return curl_easy_setopt(handle(), option, param);
Expand Down Expand Up @@ -199,7 +204,7 @@ auto HttpClient::perform(const char* method, const std::string& url,
int attempt_number) const -> HttpResponse {
LogEntry entry{registry_, method, url};

CurlHandle curl;
thread_local CurlHandle curl;
auto total_timeout = config_.connect_timeout + config_.read_timeout;
curl.set_timeout(total_timeout);
curl.set_connect_timeout(config_.connect_timeout);
Expand Down

0 comments on commit 802819e

Please sign in to comment.