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>
This commit is contained in:
1 parent
51f1651c18
commit
5775c522eb
4 files changed
+27
-21
No files matched your search
@@ -3,6 +3,7 @@
|
|||||||
#include <NetworkInterface.hpp>
|
#include <NetworkInterface.hpp>
|
||||||
#include <NetworkTypes.hpp>
|
#include <NetworkTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
|
#include <Void/Async/ThreadPool.hpp>
|
||||||
#include <Void/Data/Json.hpp>
|
#include <Void/Data/Json.hpp>
|
||||||
#include <Void/Data/Reflect.hpp>
|
#include <Void/Data/Reflect.hpp>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
@@ -125,6 +126,10 @@ namespace Echoion::Core
|
|||||||
Api::RetryConfig retryConfig_;
|
Api::RetryConfig retryConfig_;
|
||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
std::atomic<bool> initialized_{false};
|
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
|
} // namespace Echoion::Core
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <NetworkTypes.hpp>
|
#include <NetworkTypes.hpp>
|
||||||
#include <TrackTypes.hpp>
|
#include <TrackTypes.hpp>
|
||||||
#include <Types.hpp>
|
#include <Types.hpp>
|
||||||
|
#include <Void/Async/ThreadPool.hpp>
|
||||||
#include <YouTubeTypes.hpp>
|
#include <YouTubeTypes.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@@ -151,6 +152,10 @@ namespace Echoion::Core
|
|||||||
std::unique_ptr<YtDlpManager> ytDlpManager_;
|
std::unique_ptr<YtDlpManager> ytDlpManager_;
|
||||||
std::unique_ptr<UrlShortener> urlShortener_;
|
std::unique_ptr<UrlShortener> 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<bool> initialized_{false};
|
std::atomic<bool> initialized_{false};
|
||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
|
|
||||||
|
|||||||
+6
-11
@@ -163,28 +163,23 @@ namespace Echoion::Core
|
|||||||
NetworkClient::getAsync(std::string_view url,
|
NetworkClient::getAsync(std::string_view url,
|
||||||
const std::vector<Api::HttpHeader>& headers)
|
const std::vector<Api::HttpHeader>& headers)
|
||||||
{
|
{
|
||||||
return std::async(std::launch::async,
|
return pool_.submit([this, u = std::string(url), headers] {
|
||||||
[this, u = std::string(url), headers] {
|
return get(u, headers);
|
||||||
return get(u, headers);
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::future<Api::Result<Api::HttpResponse>>
|
std::future<Api::Result<Api::HttpResponse>>
|
||||||
NetworkClient::postAsync(std::string_view url, std::string_view body,
|
NetworkClient::postAsync(std::string_view url, std::string_view body,
|
||||||
const std::vector<Api::HttpHeader>& headers)
|
const std::vector<Api::HttpHeader>& headers)
|
||||||
{
|
{
|
||||||
return std::async(std::launch::async,
|
return pool_.submit([this, u = std::string(url), b = std::string(body),
|
||||||
[this, u = std::string(url),
|
headers] { return post(u, b, headers); });
|
||||||
b = std::string(body), headers] {
|
|
||||||
return post(u, b, headers);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::future<Api::Result<Api::HttpResponse>>
|
std::future<Api::Result<Api::HttpResponse>>
|
||||||
NetworkClient::requestAsync(const Api::HttpRequest& req)
|
NetworkClient::requestAsync(const Api::HttpRequest& req)
|
||||||
{
|
{
|
||||||
return std::async(std::launch::async,
|
return pool_.submit([this, req] { return request(req); });
|
||||||
[this, req] { return request(req); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Api::Result<Api::HttpResponse>>
|
std::vector<Api::Result<Api::HttpResponse>>
|
||||||
|
|||||||
+11
-10
@@ -324,10 +324,9 @@ namespace Echoion::Core
|
|||||||
std::future<Api::Result<Api::TrackInfo>>
|
std::future<Api::Result<Api::TrackInfo>>
|
||||||
YouTubeClient::getVideoInfoAsync(std::string_view videoId)
|
YouTubeClient::getVideoInfoAsync(std::string_view videoId)
|
||||||
{
|
{
|
||||||
return std::async(std::launch::async,
|
return pool_.submit([this, videoIdStr = std::string(videoId)]() {
|
||||||
[this, videoIdStr = std::string(videoId)]() {
|
return getVideoInfo(videoIdStr);
|
||||||
return getVideoInfo(videoIdStr);
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Api::Result<Api::TrackInfo>> YouTubeClient::getVideoInfoBatch(
|
std::vector<Api::Result<Api::TrackInfo>> YouTubeClient::getVideoInfoBatch(
|
||||||
@@ -840,7 +839,7 @@ namespace Echoion::Core
|
|||||||
// Launch concurrent resolutions for this batch
|
// Launch concurrent resolutions for this batch
|
||||||
for (size_t j = i; j < end; ++j) {
|
for (size_t j = i; j < end; ++j) {
|
||||||
futures.push_back(
|
futures.push_back(
|
||||||
std::async(std::launch::async, [this, &items, j]() {
|
pool_.submit([this, &items, j]() {
|
||||||
return resolveTrackWithYtDlp(items[j]);
|
return resolveTrackWithYtDlp(items[j]);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -884,10 +883,9 @@ namespace Echoion::Core
|
|||||||
std::future<Api::Result<Api::PlaylistInfo>>
|
std::future<Api::Result<Api::PlaylistInfo>>
|
||||||
YouTubeClient::getPlaylistAsync(std::string_view playlistId)
|
YouTubeClient::getPlaylistAsync(std::string_view playlistId)
|
||||||
{
|
{
|
||||||
return std::async(std::launch::async,
|
return pool_.submit([this, playlistIdStr = std::string(playlistId)]() {
|
||||||
[this, playlistIdStr = std::string(playlistId)]() {
|
return getPlaylist(playlistIdStr);
|
||||||
return getPlaylist(playlistIdStr);
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void YouTubeClient::getPlaylistStreaming(
|
void YouTubeClient::getPlaylistStreaming(
|
||||||
@@ -942,7 +940,7 @@ namespace Echoion::Core
|
|||||||
// Launch concurrent resolutions for this batch
|
// Launch concurrent resolutions for this batch
|
||||||
for (size_t j = i; j < end; ++j) {
|
for (size_t j = i; j < end; ++j) {
|
||||||
futures.push_back(
|
futures.push_back(
|
||||||
std::async(std::launch::async, [this, &items, j]() {
|
pool_.submit([this, &items, j]() {
|
||||||
return resolveTrackWithYtDlp(items[j]);
|
return resolveTrackWithYtDlp(items[j]);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -1413,6 +1411,9 @@ namespace Echoion::Core
|
|||||||
continue;
|
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]() {
|
futures.push_back(std::async(std::launch::async, [this, &track]() {
|
||||||
// Each URL shortening has its own 3-second timeout
|
// Each URL shortening has its own 3-second timeout
|
||||||
// Use shared_ptr for safe detached thread execution
|
// Use shared_ptr for safe detached thread execution
|
||||||
|
|||||||
Reference in new issue
Block a user