22#include "util/Mutex.hpp"
23#include "util/config/ConfigDefinition.hpp"
24#include "util/log/Logger.hpp"
25#include "util/prometheus/Counter.hpp"
26#include "util/prometheus/Gauge.hpp"
28#include <boost/asio/spawn.hpp>
29#include <boost/asio/thread_pool.hpp>
30#include <boost/json/object.hpp>
55 [[nodiscard]]
virtual boost::json::object
63 using TaskType = std::function<void(boost::asio::yield_context)>;
65 struct TaskWithTimestamp {
67 std::chrono::system_clock::time_point queuedAt;
70 using QueueType = std::queue<TaskWithTimestamp>;
86 size_t highPriorityCounter = 0;
89 push(
Priority priority, TaskType&& task)
91 auto& queue = [
this, priority] -> QueueType& {
92 if (priority == Priority::High)
98 .task = std::move(task), .queuedAt = std::chrono::system_clock::now()
106 return high.empty() and normal.empty();
109 [[nodiscard]] std::optional<TaskWithTimestamp>
112 if (not high.empty() and (highPriorityCounter < kTAKE_HIGH_PRIO or normal.empty())) {
113 auto taskWithTimestamp = std::move(high.front());
115 ++highPriorityCounter;
116 return taskWithTimestamp;
119 if (not normal.empty()) {
120 auto taskWithTimestamp = std::move(normal.front());
122 highPriorityCounter = 0;
123 return taskWithTimestamp;
131 static constexpr auto kTAKE_HIGH_PRIO = 4uz;
134 std::reference_wrapper<util::prometheus::CounterInt> queued_;
135 std::reference_wrapper<util::prometheus::CounterInt> durationUs_;
137 std::reference_wrapper<util::prometheus::GaugeInt> curSize_;
138 uint32_t maxSize_ = std::numeric_limits<uint32_t>::max();
140 util::Logger log_{
"RPC"};
141 boost::asio::thread_pool ioc_;
143 std::atomic_bool stopping_;
144 std::atomic_bool processingStarted_{
false};
146 class OneTimeCallable {
147 std::function<void()> func_;
152 setCallable(std::function<
void()> func);
158 operator bool()
const;
160 util::Mutex<OneTimeCallable> onQueueEmpty_;
161 util::Mutex<QueueState> queueState_;
175 WorkQueue(std::uint32_t numWorkers, uint32_t maxSize = 0);
185 WorkQueue(DontStartProcessingTag, std::uint32_t numWorkers, uint32_t maxSize = 0);
202 requestStop(std::function<
void()> onQueueEmpty = [] {});
231 postCoro(TaskType func,
bool isWhiteListed,
Priority priority = Priority::Default);
238 [[nodiscard]] boost::json::object
257 executeTask(boost::asio::yield_context yield);
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:62
void stop()
Put the work queue into a stopping state and await workers to finish.
Definition WorkQueue.cpp:146
WorkQueue(std::uint32_t numWorkers, uint32_t maxSize=0)
Create an instance of the work queue.
Definition WorkQueue.cpp:83
boost::json::object report() const override
Generate a report of the work queue state.
Definition WorkQueue.cpp:176
void startProcessing()
Start processing of the enqueued tasks.
Definition WorkQueue.cpp:95
bool postCoro(TaskType func, bool isWhiteListed, Priority priority=Priority::Default)
Submit a job to the work queue.
Definition WorkQueue.cpp:108
Priority
Represents a task scheduling priority.
Definition WorkQueue.hpp:76
void join()
Wait until all the jobs in the queue are finished.
size_t size() const
Get the size of the queue.
Definition WorkQueue.cpp:189
static WorkQueue makeWorkQueue(util::config::ClioConfigDefinition const &config)
A factory function that creates the work queue based on a config.
Definition WorkQueue.cpp:163
void requestStop(std::function< void()> onQueueEmpty=[] {})
Put the work queue into a stopping state. This will prevent new jobs from being queued.
Definition WorkQueue.cpp:137
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
An interface for any class providing a report as json object.
Definition WorkQueue.hpp:47
virtual boost::json::object report() const =0
Generate a report of the work queue state.
Definition WorkQueue.hpp:164