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
This commit is contained in:
ChuheZhang 2024-10-30 22:04:50 +08:00 committed by GitHub
parent 69b83817cd
commit 018646d6bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<std::string>()(key.host) ^ (std::hash<std::string>()(key.service) << 1);
}
};
};
explicit AsioHttpClientPool(const std::shared_ptr<IOCtx>& 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<AsioHttpClient>();
}
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_ = State::kPreInit;
// client管理
std::unordered_map<std::string, std::shared_ptr<AsioHttpClient>> client_map_;
std::unordered_map<ClientKey, std::shared_ptr<AsioHttpClient>, ClientKey::Hash> client_map_;
};
} // namespace aimrt::common::net