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 <NetworkTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <atomic>
|
||||
@@ -125,6 +126,10 @@ namespace Echoion::Core
|
||||
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
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <NetworkTypes.hpp>
|
||||
#include <TrackTypes.hpp>
|
||||
#include <Types.hpp>
|
||||
#include <Void/Async/ThreadPool.hpp>
|
||||
#include <YouTubeTypes.hpp>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
@@ -151,6 +152,10 @@ namespace Echoion::Core
|
||||
std::unique_ptr<YtDlpManager> ytDlpManager_;
|
||||
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};
|
||||
mutable std::mutex mutex_;
|
||||
|
||||
|
||||
+6
-11
@@ -163,28 +163,23 @@ namespace Echoion::Core
|
||||
NetworkClient::getAsync(std::string_view url,
|
||||
const std::vector<Api::HttpHeader>& 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<Api::Result<Api::HttpResponse>>
|
||||
NetworkClient::postAsync(std::string_view url, std::string_view body,
|
||||
const std::vector<Api::HttpHeader>& 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<Api::Result<Api::HttpResponse>>
|
||||
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<Api::Result<Api::HttpResponse>>
|
||||
|
||||
+11
-10
@@ -324,10 +324,9 @@ namespace Echoion::Core
|
||||
std::future<Api::Result<Api::TrackInfo>>
|
||||
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<Api::Result<Api::TrackInfo>> 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<Api::Result<Api::PlaylistInfo>>
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user