From 018646d6bfb4b25a23d1fc863c137b1c4905019a Mon Sep 17 00:00:00 2001 From: ChuheZhang <32006697+ChuheZhang@users.noreply.github.com> Date: Wed, 30 Oct 2024 22:04:50 +0800 Subject: [PATCH] Refactor coroutine request handling and ensure unique client keys (#61) * Refactor coroutine request handling and ensure unique client keys * Refactor client_key to use ClientKey struct with custom hash to ensure uniqueness and support IPv6 --- src/common/net/asio_http_cli.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/common/net/asio_http_cli.h b/src/common/net/asio_http_cli.h index 033becb59..439e216e4 100644 --- a/src/common/net/asio_http_cli.h +++ b/src/common/net/asio_http_cli.h @@ -490,6 +490,23 @@ class AsioHttpClientPool } }; + // Define ClientKey struct inside the AsioHttpClientPool class + struct ClientKey { + std::string host; + std::string service; + + bool operator==(const ClientKey& rhs) const { + return host == rhs.host && service == rhs.service; + } + + // Custom hash function for ClientKey + struct Hash { + std::size_t operator()(const ClientKey& key) const { + return std::hash()(key.host) ^ (std::hash()(key.service) << 1); + } + }; + }; + explicit AsioHttpClientPool(const std::shared_ptr& io_ptr) : io_ptr_(io_ptr), mgr_strand_(boost::asio::make_strand(*io_ptr_)), @@ -550,7 +567,7 @@ class AsioHttpClientPool co_return std::shared_ptr(); } - auto client_key = client_options.host + client_options.service; + ClientKey client_key{client_options.host, client_options.service}; auto itr = client_map_.find(client_key); if (itr != client_map_.end()) { @@ -605,7 +622,7 @@ class AsioHttpClientPool std::atomic state_ = State::kPreInit; // client管理 - std::unordered_map> client_map_; + std::unordered_map, ClientKey::Hash> client_map_; }; } // namespace aimrt::common::net