Compare commits
3
Commits
51f1651c18
...
ef094c0d78
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef094c0d78 | ||
|
|
da708fa9bb | ||
|
|
5775c522eb |
No files matched your search
@@ -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
|
||||
|
||||
@@ -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_;
|
||||
|
||||
|
||||
@@ -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
@@ -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
@@ -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
|
||||
|
||||
+12
-52
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user