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:
parent
69b83817cd
commit
018646d6bf
@ -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)
|
explicit AsioHttpClientPool(const std::shared_ptr<IOCtx>& io_ptr)
|
||||||
: io_ptr_(io_ptr),
|
: io_ptr_(io_ptr),
|
||||||
mgr_strand_(boost::asio::make_strand(*io_ptr_)),
|
mgr_strand_(boost::asio::make_strand(*io_ptr_)),
|
||||||
@ -550,7 +567,7 @@ class AsioHttpClientPool
|
|||||||
co_return std::shared_ptr<AsioHttpClient>();
|
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);
|
auto itr = client_map_.find(client_key);
|
||||||
if (itr != client_map_.end()) {
|
if (itr != client_map_.end()) {
|
||||||
@ -605,7 +622,7 @@ class AsioHttpClientPool
|
|||||||
std::atomic<State> state_ = State::kPreInit;
|
std::atomic<State> state_ = State::kPreInit;
|
||||||
|
|
||||||
// client管理
|
// 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
|
} // namespace aimrt::common::net
|
||||||
|
Loading…
x
Reference in New Issue
Block a user