145 lines
3.9 KiB
C++
145 lines
3.9 KiB
C++
#include <Void/Log/Log.hpp>
|
|
#include "echoion/core/NetworkClient.hpp"
|
|
#include <Void/Functional/Retry.hpp>
|
|
#include <Void/Net/Http.hpp>
|
|
#include <algorithm>
|
|
#include <format>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
NetworkClient::NetworkClient() = default;
|
|
NetworkClient::~NetworkClient() { shutdown(); }
|
|
|
|
Api::Result<void> NetworkClient::initialize(
|
|
const std::optional<Void::Net::ProxyConfig>& proxy)
|
|
{
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
proxy_ = proxy;
|
|
}
|
|
initialized_ = true;
|
|
Void::Log::info("NetworkClient", "{}", std::format("NetworkClient initialized (proxy: {})",
|
|
proxy.has_value() ? "enabled" : "disabled"));
|
|
return {};
|
|
}
|
|
|
|
void NetworkClient::shutdown() { initialized_ = false; }
|
|
|
|
Api::Result<Void::Net::Response>
|
|
NetworkClient::executeRequest(const Void::Net::Request& req)
|
|
{
|
|
Void::Net::HttpClient client;
|
|
// Snapshot the proxy under the lock. setProxy()/initialize() can run on
|
|
// other threads (config reload, init), and a torn read of the
|
|
// optional<ProxyConfig> (it holds std::string host/user/pass) could
|
|
// yield an empty host -> enabled()==false -> a silent direct connection.
|
|
std::optional<Void::Net::ProxyConfig> active;
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
active = proxy_;
|
|
}
|
|
if (active && active->enabled()) {
|
|
client.set_proxy(*active);
|
|
}
|
|
|
|
auto result = client.send(req);
|
|
if (!result) {
|
|
return Api::fail(Api::ErrorCode::NetworkError,
|
|
result.error().message());
|
|
}
|
|
return std::move(*result);
|
|
}
|
|
|
|
Api::Result<Void::Net::Response>
|
|
NetworkClient::executeWithRetry(const Void::Net::Request& req)
|
|
{
|
|
return Void::Retry::retry_while(
|
|
retryPolicy_, [&] { return executeRequest(req); },
|
|
[this](const Api::Result<Void::Net::Response>& result) {
|
|
if (!result) {
|
|
Void::Log::warn("NetworkClient", "{}", std::format(
|
|
"Request error: {}, retrying", result.error().message));
|
|
return true;
|
|
}
|
|
// Retry only on the configured retryable HTTP statuses.
|
|
const auto& codes = retryableStatusCodes_;
|
|
if (std::find(codes.begin(), codes.end(), result->status) !=
|
|
codes.end()) {
|
|
Void::Log::warn("NetworkClient", "{}",
|
|
std::format("Request failed with status {}, retrying",
|
|
result->status));
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
Api::Result<Void::Net::Response>
|
|
NetworkClient::request(const Void::Net::Request& req)
|
|
{
|
|
return executeWithRetry(req);
|
|
}
|
|
|
|
Api::Result<Void::Net::Response>
|
|
NetworkClient::getDirect(std::string_view url,
|
|
const std::vector<Void::Net::Header>& headers)
|
|
{
|
|
Void::Net::Request req;
|
|
req.url = std::string(url);
|
|
req.method = Void::Net::Method::Get;
|
|
req.headers = headers;
|
|
|
|
// Deliberately no proxy and no retry (see header).
|
|
Void::Net::HttpClient client;
|
|
auto result = client.send(req);
|
|
if (!result) {
|
|
return Api::fail(Api::ErrorCode::NetworkError,
|
|
result.error().message());
|
|
}
|
|
return std::move(*result);
|
|
}
|
|
|
|
Api::Result<Void::Net::Response>
|
|
NetworkClient::get(std::string_view url,
|
|
const std::vector<Void::Net::Header>& headers)
|
|
{
|
|
Void::Net::Request req;
|
|
req.url = std::string(url);
|
|
req.method = Void::Net::Method::Get;
|
|
req.headers = headers;
|
|
return request(req);
|
|
}
|
|
|
|
Api::Result<Void::Net::Response>
|
|
NetworkClient::post(std::string_view url, std::string_view body,
|
|
const std::vector<Void::Net::Header>& headers)
|
|
{
|
|
Void::Net::Request req;
|
|
req.url = std::string(url);
|
|
req.method = Void::Net::Method::Post;
|
|
req.headers = headers;
|
|
req.body = std::string(body);
|
|
return request(req);
|
|
}
|
|
|
|
void NetworkClient::setProxy(
|
|
const std::optional<Void::Net::ProxyConfig>& proxy)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
proxy_ = proxy;
|
|
}
|
|
|
|
std::optional<Void::Net::ProxyConfig> NetworkClient::getProxy() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return proxy_;
|
|
}
|
|
|
|
void NetworkClient::clearProxy()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
proxy_ = std::nullopt;
|
|
}
|
|
|
|
} // namespace Echoion::Core
|