Skip to content

Commit a9da025

Browse files
Reuse curl handle to attempt to reuse sessions (#7321)
Co-authored-by: Amaury Chamayou <[email protected]>
1 parent 10d1b67 commit a9da025

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/http/curl.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ namespace ccf::curl
6363
}
6464
}
6565

66+
// No implicit copying: unique ownership of the CURL handle
67+
UniqueCURL(const UniqueCURL&) = delete;
68+
UniqueCURL& operator=(const UniqueCURL&) = delete;
69+
70+
// Move semantics
71+
UniqueCURL(UniqueCURL&& other) noexcept : p(std::move(other.p)) {}
72+
UniqueCURL& operator=(UniqueCURL&& other) noexcept
73+
{
74+
p = std::move(other.p);
75+
return *this;
76+
}
77+
78+
~UniqueCURL() = default;
79+
6680
operator CURL*() const
6781
{
6882
return p.get();
@@ -486,6 +500,11 @@ namespace ccf::curl
486500
return curl_handle;
487501
}
488502

503+
[[nodiscard]] UniqueCURL& get_easy_handle_ptr()
504+
{
505+
return curl_handle;
506+
}
507+
489508
[[nodiscard]] RESTVerb get_method() const
490509
{
491510
return method;

src/snapshots/fetch.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ namespace snapshots
4949
// Make initial request, which returns a redirect response to specific
5050
// snapshot
5151
std::string snapshot_url;
52+
ccf::curl::UniqueCURL curl_easy;
5253
{
53-
ccf::curl::UniqueCURL curl_easy;
5454
curl_easy.set_opt(CURLOPT_CAINFO, path_to_peer_cert.c_str());
5555

5656
auto initial_url = fmt::format(
@@ -106,12 +106,12 @@ namespace snapshots
106106

107107
snapshot_url =
108108
fmt::format("https://{}{}", peer_address, location_it->second);
109+
curl_easy = std::move(request.get_easy_handle_ptr());
109110
}
110111

111112
// Make follow-up request to redirected URL, to fetch total content size
112113
size_t content_size = 0;
113114
{
114-
ccf::curl::UniqueCURL curl_easy;
115115
curl_easy.set_opt(CURLOPT_CAINFO, path_to_peer_cert.c_str());
116116

117117
ccf::curl::UniqueSlist headers;
@@ -174,6 +174,7 @@ namespace snapshots
174174
snapshot_size_request.get_url(),
175175
ec));
176176
}
177+
curl_easy = std::move(snapshot_size_request.get_easy_handle_ptr());
177178
}
178179

179180
// Fetch 4MB chunks at a time
@@ -192,7 +193,6 @@ namespace snapshots
192193

193194
while (true)
194195
{
195-
ccf::curl::UniqueCURL curl_easy;
196196
curl_easy.set_opt(CURLOPT_CAINFO, path_to_peer_cert.c_str());
197197

198198
ccf::curl::UniqueSlist headers;
@@ -236,6 +236,7 @@ namespace snapshots
236236

237237
snapshot_response =
238238
std::move(snapshot_range_request.get_response_ptr());
239+
curl_easy = std::move(snapshot_range_request.get_easy_handle_ptr());
239240

240241
if (range_end == content_size)
241242
{

0 commit comments

Comments
 (0)