zhjwpku commented on code in PR #1090:
URL: https://github.com/apache/iceberg-cpp/pull/1090#discussion_r4488250094
##########
src/iceberg/test/rest_catalog_integration_test.cc:
##########
@@ -425,6 +426,31 @@ TEST_F(RestCatalogIntegrationTest,
FetchServerConfigDirect) {
}
}
+// RestCatalog shares its HttpClient with token refresh and metrics reporting.
Review Comment:
I get it, thanks
##########
src/iceberg/catalog/rest/http_client.cc:
##########
@@ -190,6 +199,50 @@ Status HandleFailureResponse(const cpr::Response& response,
} // namespace
+Result<HttpResponse> HttpResponse::Impl::Make(cpr::Response response,
+ const ErrorHandler&
error_handler) {
+ ICEBERG_RETURN_UNEXPECTED(HandleFailureResponse(response, error_handler));
+ HttpResponse http_response;
+ http_response.impl_ = std::make_unique<Impl>(std::move(response));
+ return http_response;
+}
+
+// libcurl does not support using a connection cache from several threads at
once
+// (CURLSHOPT_SHARE(3)).
+class HttpClient::ConnectionPools {
+ public:
+ template <typename Send>
+ Result<HttpResponse> Run(const Send& send, const ErrorHandler&
error_handler) {
+ std::unique_ptr<cpr::ConnectionPool> pool = Acquire();
+ // Make() consumes the CPR response, freeing its curl handle before pool
reuse.
+ Result<HttpResponse> response = HttpResponse::Impl::Make(send(*pool),
error_handler);
+ Release(std::move(pool));
+ return response;
+ }
+
+ private:
+ std::unique_ptr<cpr::ConnectionPool> Acquire() {
+ {
+ std::lock_guard lock(mutex_);
+ if (!idle_.empty()) {
+ auto pool = std::move(idle_.back());
+ idle_.pop_back();
+ return pool;
+ }
+ }
+ return std::make_unique<cpr::ConnectionPool>();
+ }
+
+ void Release(std::unique_ptr<cpr::ConnectionPool> pool) {
+ std::lock_guard lock(mutex_);
+ idle_.push_back(std::move(pool));
+ }
+
+ std::mutex mutex_;
+ // LIFO: the most recently released pool is the likeliest to have a live
connection.
+ std::vector<std::unique_ptr<cpr::ConnectionPool>> idle_;
Review Comment:
`idle_` never shrinks, it ends up as large as the peak number of concurrent
requests, but I think it's fine for now :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]