Compare commits

...
3 Commits
Author SHA1 Message Date
faumarayandClaude Opus 4.8 ef094c0d78 Phase C: replace CommandManager's hand-rolled pool with Void ThreadPool
CommandManager dispatched async commands through a bespoke pool — a jthread
vector, a CommandTask queue, a mutex/condition_variable, and a manual
drain-on-shutdown loop. Replace it with a Void::Concurrency::ThreadPool:

- startThreadPool() builds the pool at THREAD_POOL_SIZE; shutdown() flips
  the accept flag and resets the pool (~ThreadPool drains queued commands
  then joins — the same shutdown semantics the manual loop had).
- dispatch() submits the CommandTask lambda straight to the pool instead of
  enqueuing + notifying; executeCommandAsync already swallows exceptions, so
  the discarded future is safe.
- processCommandQueue() and the queue/cv/worker members are gone.

Build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 03:12:10 +03:00
faumarayandClaude Opus 4.8 da708fa9bb Phase C: replace YtDlpManager's hand-rolled pool with Void ThreadPool
YtDlpManager carried a bespoke worker pool — a jthread vector, a
packaged_task queue, and a mutex/condition_variable/stop-flag loop. Swap
all of it for a single Void::Concurrency::ThreadPool (held by unique_ptr so
setThreadPoolSize can recreate it):

- startThreadPool() constructs the pool at the (auto-adjusted) size;
  stopThreadPool() resets it (the destructor drains and joins).
- executeAsync() submits to the pool under mutex_ (mutually exclusive with
  start/stop), with a synchronous fallback if called before start.
- workerThread() and the taskQueue_/taskCv_/taskMutex_/stopThreads_ members
  are gone.

Behaviorally equivalent (bounded yt-dlp concurrency, same std::future
results) with far less concurrency code to own. Build green.

The init-time "update yt-dlp in background" detached thread is left as-is:
it's a one-shot fired before the pool exists.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 03:09:15 +03:00
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
8 changed files with 61 additions and 134 deletions

No files matched your search

+5 -5
View File
@@ -4,7 +4,9 @@
#include <ConfigTypes.hpp>
#include <PluginTypes.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <atomic>
#include <memory>
#include <condition_variable>
#include <functional>
#include <future>
@@ -174,13 +176,11 @@ namespace Echoion::Core
};
void startThreadPool();
void processCommandQueue();
void executeCommandAsync(CommandTask task);
std::vector<std::jthread> workerThreads_;
std::queue<CommandTask> commandQueue_;
std::mutex queueMutex_;
std::condition_variable queueCv_;
// Void worker pool backing async command execution (drains
// in-flight commands on shutdown via ~ThreadPool).
std::unique_ptr<Void::Concurrency::ThreadPool> pool_;
std::atomic<bool> shutdown_{false};
static constexpr size_t THREAD_POOL_SIZE =
4; // Handle up to 4 concurrent commands
+5
View File
@@ -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
+5
View File
@@ -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_;
+5 -9
View File
@@ -3,13 +3,13 @@
#include "NetworkClient.hpp"
#include <TrackTypes.hpp>
#include <Types.hpp>
#include <Void/Async/ThreadPool.hpp>
#include <YouTubeTypes.hpp>
#include <atomic>
#include <condition_variable>
#include <filesystem>
#include <future>
#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <vector>
@@ -105,7 +105,6 @@ namespace Echoion::Core
// Thread pool for async operations
void startThreadPool();
void stopThreadPool();
void workerThread();
std::shared_ptr<NetworkClient> networkClient_;
std::filesystem::path executablePath_;
@@ -120,13 +119,10 @@ namespace Echoion::Core
std::optional<Api::ProxyConfig> proxyConfig_;
mutable std::mutex proxyMutex_;
// Thread pool for async operations
// Thread pool for async operations (Void worker pool; recreated
// on resize). Guarded by mutex_ for start/stop/submit.
size_t threadPoolSize_ = 4;
std::vector<std::jthread> threadPool_;
std::queue<std::packaged_task<YtDlpResult()>> taskQueue_;
std::mutex taskMutex_;
std::condition_variable taskCv_;
std::atomic<bool> stopThreads_{false};
std::unique_ptr<Void::Concurrency::ThreadPool> pool_;
// yt-dlp download URLs
static constexpr std::string_view YTDLP_VERSION_URL =
+12 -47
View File
@@ -119,53 +119,20 @@ namespace Echoion::Core
void CommandManager::shutdown()
{
{
std::lock_guard<std::mutex> lock(queueMutex_);
shutdown_ = true;
}
queueCv_.notify_all();
for (auto& thread : workerThreads_) {
if (thread.joinable()) {
thread.join();
}
}
workerThreads_.clear();
shutdown_ = true;
// ~ThreadPool drains queued commands and joins its workers.
pool_.reset();
}
void CommandManager::startThreadPool()
{
for (size_t i = 0; i < THREAD_POOL_SIZE; ++i) {
workerThreads_.emplace_back([this]() { processCommandQueue(); });
}
pool_ =
std::make_unique<Void::Concurrency::ThreadPool>(THREAD_POOL_SIZE);
ECHOION_LOG_INFO(
std::format("CommandManager thread pool started with {} workers",
THREAD_POOL_SIZE));
}
void CommandManager::processCommandQueue()
{
while (true) {
CommandTask task;
{
std::unique_lock<std::mutex> lock(queueMutex_);
queueCv_.wait(lock, [this] {
return shutdown_ || !commandQueue_.empty();
});
if (shutdown_ && commandQueue_.empty()) {
return;
}
task = std::move(commandQueue_.front());
commandQueue_.pop();
}
// Execute the command
executeCommandAsync(std::move(task));
}
}
void CommandManager::executeCommandAsync(CommandTask task)
{
try {
@@ -288,21 +255,19 @@ namespace Echoion::Core
return true; // Command was found but not allowed
}
// Queue the command for async execution
// Submit the command for async execution on the worker pool.
if (shutdown_ || !pool_) {
return false;
}
CommandTask task{.clientId = clientId,
.message = std::string(message),
.musicManager = musicManager,
.replyFunc = std::move(replyFunc),
.invokerName = std::string(invokerName)};
{
std::lock_guard<std::mutex> lock(queueMutex_);
if (shutdown_) {
return false;
}
commandQueue_.push(std::move(task));
}
queueCv_.notify_one();
pool_->submit([this, task = std::move(task)]() mutable {
executeCommandAsync(std::move(task));
});
return true;
}
+6 -11
View File
@@ -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
View File
@@ -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
+12 -52
View File
@@ -171,7 +171,7 @@ namespace Echoion::Core
void YtDlpManager::startThreadPool()
{
stopThreadPool(); // Clear any existing threads first
stopThreadPool(); // Drop any existing pool first
// Auto-adjust thread pool size based on hardware
unsigned int hardwareThreads = std::thread::hardware_concurrency();
@@ -185,50 +185,13 @@ namespace Echoion::Core
ECHOION_LOG_INFO(std::format(
"Starting yt-dlp thread pool with {} threads", threadPoolSize_));
for (size_t i = 0; i < threadPoolSize_; ++i) {
threadPool_.emplace_back([this] { workerThread(); });
}
pool_ = std::make_unique<Void::Concurrency::ThreadPool>(threadPoolSize_);
}
void YtDlpManager::stopThreadPool()
{
stopThreads_ = true;
taskCv_.notify_all();
for (auto& thread : threadPool_) {
if (thread.joinable()) {
thread.join();
}
}
threadPool_.clear();
stopThreads_ = false;
}
void YtDlpManager::workerThread()
{
while (!stopThreads_.load()) {
std::packaged_task<YtDlpResult()> task;
{
std::unique_lock<std::mutex> lock(taskMutex_);
taskCv_.wait(lock, [this] {
return !taskQueue_.empty() || stopThreads_.load();
});
if (stopThreads_.load()) {
return;
}
if (!taskQueue_.empty()) {
task = std::move(taskQueue_.front());
taskQueue_.pop();
}
}
if (task.valid()) {
task();
}
}
// ~ThreadPool drains in-flight work and joins its workers.
pool_.reset();
}
void YtDlpManager::setThreadPoolSize(size_t size)
@@ -631,18 +594,15 @@ namespace Echoion::Core
std::future<YtDlpResult>
YtDlpManager::executeAsync(const std::vector<std::string>& args)
{
std::packaged_task<YtDlpResult()> task(
[this, args]() { return executeInternal(args); });
auto future = task.get_future();
{
std::lock_guard<std::mutex> lock(taskMutex_);
taskQueue_.push(std::move(task));
std::lock_guard<std::mutex> lock(mutex_);
if (!pool_) {
// Not started (shouldn't happen post-init): run synchronously so
// the caller still gets a valid, ready future.
std::promise<YtDlpResult> prom;
prom.set_value(executeInternal(args));
return prom.get_future();
}
taskCv_.notify_one();
return future;
return pool_->submit([this, args]() { return executeInternal(args); });
}
std::vector<YtDlpResult> YtDlpManager::executeBatch(