Files
EchoionCore/include/echoion/core/NetworkClient.hpp
T
faumarayandClaude Opus 4.8 5775c522eb Phase C: back parallel fan-outs with Void ThreadPool
Replace the per-call std::async OS-thread spawns in the blocking
fan-out/collect paths with a bounded Void::Concurrency::ThreadPool member,
giving thread reuse instead of an unbounded thread per request:

- YouTubeClient: getVideoInfoAsync, getPlaylistAsync, and the two playlist
  resolution batches now submit to pool_. The std::future<T>/.get() join
  semantics are unchanged, so control flow is identical.
- NetworkClient: getAsync/postAsync/requestAsync (and thus requestBatch)
  submit to pool_.

populateShortenedUrls is deliberately left on std::async — it relies on
wide parallelism under a tight 5s budget, which a bounded pool would
throttle. Build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 03:06:44 +03:00

136 lines
4.2 KiB
C++

#pragma once
#include <NetworkInterface.hpp>
#include <NetworkTypes.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <Void/Data/Json.hpp>
#include <Void/Data/Reflect.hpp>
#include <atomic>
#include <format>
#include <future>
#include <mutex>
#include <optional>
#include <string_view>
#include <vector>
namespace Echoion::Core
{
// HTTP client implementing Api::INetworkClient. A thin adapter over
// Void::Net::HttpClient (HTTP/HTTPS, TLS, SOCKS5 + HTTP-CONNECT proxies);
// retry/backoff is layered here via Void::Retry.
class NetworkClient : public Api::INetworkClient
{
public:
NetworkClient();
~NetworkClient() override;
Api::Result<void> initialize(
const std::optional<Api::ProxyConfig>& proxy = std::nullopt);
void shutdown();
// Synchronous requests
[[nodiscard]] Api::Result<Api::HttpResponse>
get(std::string_view url,
const std::vector<Api::HttpHeader>& headers = {});
[[nodiscard]] Api::Result<Api::HttpResponse>
post(std::string_view url, std::string_view body,
const std::vector<Api::HttpHeader>& headers = {});
[[nodiscard]] Api::Result<Api::HttpResponse>
request(const Api::HttpRequest& req);
// Asynchronous requests
[[nodiscard]] std::future<Api::Result<Api::HttpResponse>>
getAsync(std::string_view url,
const std::vector<Api::HttpHeader>& headers = {});
[[nodiscard]] std::future<Api::Result<Api::HttpResponse>>
postAsync(std::string_view url, std::string_view body,
const std::vector<Api::HttpHeader>& headers = {});
[[nodiscard]] std::future<Api::Result<Api::HttpResponse>>
requestAsync(const Api::HttpRequest& req);
// Concurrent batch requests
[[nodiscard]] std::vector<Api::Result<Api::HttpResponse>>
requestBatch(const std::vector<Api::HttpRequest>& requests);
// JSON helpers using Void::Json reflection
template <typename T>
[[nodiscard]] Api::Result<T>
getJson(std::string_view url,
const std::vector<Api::HttpHeader>& headers = {})
{
auto response = get(url, headers);
if (!response) {
return std::unexpected(response.error());
}
return parseJson<T>(response->bodyAsString());
}
template <typename T, typename Body>
[[nodiscard]] Api::Result<T>
postJson(std::string_view url, const Body& body,
const std::vector<Api::HttpHeader>& headers = {})
{
std::string jsonBody =
Void::Json::serialize(Void::Json::to_json(body));
auto response = post(url, jsonBody, headers);
if (!response) {
return std::unexpected(response.error());
}
return parseJson<T>(response->bodyAsString());
}
// Proxy configuration
void setProxy(const std::optional<Api::ProxyConfig>& proxy);
[[nodiscard]] std::optional<Api::ProxyConfig> getProxy() const;
void clearProxy();
// Retry configuration
void setRetryConfig(const Api::RetryConfig& config);
[[nodiscard]] Api::RetryConfig getRetryConfig() const;
private:
template <typename T>
[[nodiscard]] static Api::Result<T>
parseJson(const std::string& body)
{
auto parsed = Void::Json::parse(body);
if (!parsed) {
return Api::fail(
Api::ErrorCode::ParseError,
std::format("JSON parse error ({}:{}): {}",
parsed.error().line, parsed.error().col,
parsed.error().message));
}
auto value = Void::Json::from_json<T>(*parsed);
if (!value) {
return Api::fail(
Api::ErrorCode::ParseError,
std::format("JSON decode error: field '{}': {}",
value.error().field, value.error().message));
}
return std::move(*value);
}
// Execute with retry/backoff (Void::Retry); single attempt via
// Void::Net::HttpClient.
[[nodiscard]] Api::Result<Api::HttpResponse>
executeWithRetry(const Api::HttpRequest& req);
[[nodiscard]] Api::Result<Api::HttpResponse>
executeRequest(const Api::HttpRequest& req);
std::optional<Api::ProxyConfig> proxy_;
Api::RetryConfig retryConfig_;
mutable std::mutex mutex_;
std::atomic<bool> initialized_{false};
// Worker pool backing the *Async / requestBatch fan-outs, replacing
// a per-call std::async OS-thread spawn with bounded thread reuse.
Void::Concurrency::ThreadPool pool_;
};
} // namespace Echoion::Core