diff --git a/include/echoion/core/NetworkClient.hpp b/include/echoion/core/NetworkClient.hpp index a1ad249..2d38698 100644 --- a/include/echoion/core/NetworkClient.hpp +++ b/include/echoion/core/NetworkClient.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -125,6 +126,10 @@ namespace Echoion::Core Api::RetryConfig retryConfig_; mutable std::mutex mutex_; std::atomic 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 diff --git a/include/echoion/core/YouTubeClient.hpp b/include/echoion/core/YouTubeClient.hpp index a7b75cd..a4ca74a 100644 --- a/include/echoion/core/YouTubeClient.hpp +++ b/include/echoion/core/YouTubeClient.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -151,6 +152,10 @@ namespace Echoion::Core std::unique_ptr ytDlpManager_; std::unique_ptr urlShortener_; + // Worker pool backing the parallel API/resolution fan-outs. Bounds + // thread reuse instead of spawning an OS thread per std::async call. + Void::Concurrency::ThreadPool pool_; + std::atomic initialized_{false}; mutable std::mutex mutex_; diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 1118ba1..128cf9d 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -163,28 +163,23 @@ namespace Echoion::Core NetworkClient::getAsync(std::string_view url, const std::vector& headers) { - return std::async(std::launch::async, - [this, u = std::string(url), headers] { - return get(u, headers); - }); + return pool_.submit([this, u = std::string(url), headers] { + return get(u, headers); + }); } std::future> NetworkClient::postAsync(std::string_view url, std::string_view body, const std::vector& headers) { - return std::async(std::launch::async, - [this, u = std::string(url), - b = std::string(body), headers] { - return post(u, b, headers); - }); + return pool_.submit([this, u = std::string(url), b = std::string(body), + headers] { return post(u, b, headers); }); } std::future> NetworkClient::requestAsync(const Api::HttpRequest& req) { - return std::async(std::launch::async, - [this, req] { return request(req); }); + return pool_.submit([this, req] { return request(req); }); } std::vector> diff --git a/src/YouTubeClient.cpp b/src/YouTubeClient.cpp index b3ff6c0..f4d5338 100644 --- a/src/YouTubeClient.cpp +++ b/src/YouTubeClient.cpp @@ -324,10 +324,9 @@ namespace Echoion::Core std::future> YouTubeClient::getVideoInfoAsync(std::string_view videoId) { - return std::async(std::launch::async, - [this, videoIdStr = std::string(videoId)]() { - return getVideoInfo(videoIdStr); - }); + return pool_.submit([this, videoIdStr = std::string(videoId)]() { + return getVideoInfo(videoIdStr); + }); } std::vector> YouTubeClient::getVideoInfoBatch( @@ -840,7 +839,7 @@ namespace Echoion::Core // Launch concurrent resolutions for this batch for (size_t j = i; j < end; ++j) { futures.push_back( - std::async(std::launch::async, [this, &items, j]() { + pool_.submit([this, &items, j]() { return resolveTrackWithYtDlp(items[j]); })); } @@ -884,10 +883,9 @@ namespace Echoion::Core std::future> YouTubeClient::getPlaylistAsync(std::string_view playlistId) { - return std::async(std::launch::async, - [this, playlistIdStr = std::string(playlistId)]() { - return getPlaylist(playlistIdStr); - }); + return pool_.submit([this, playlistIdStr = std::string(playlistId)]() { + return getPlaylist(playlistIdStr); + }); } void YouTubeClient::getPlaylistStreaming( @@ -942,7 +940,7 @@ namespace Echoion::Core // Launch concurrent resolutions for this batch for (size_t j = i; j < end; ++j) { futures.push_back( - std::async(std::launch::async, [this, &items, j]() { + pool_.submit([this, &items, j]() { return resolveTrackWithYtDlp(items[j]); })); } @@ -1413,6 +1411,9 @@ namespace Echoion::Core continue; } + // Best-effort wide-parallel shortening under a tight timeout — kept + // on std::async so each track gets its own thread rather than + // queueing behind the bounded resolution pool. futures.push_back(std::async(std::launch::async, [this, &track]() { // Each URL shortening has its own 3-second timeout // Use shared_ptr for safe detached thread execution