Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
WorkQueue.hpp
1#pragma once
2
3#include "util/Mutex.hpp"
4#include "util/config/ConfigDefinition.hpp"
5#include "util/log/Logger.hpp"
6#include "util/prometheus/Counter.hpp"
7#include "util/prometheus/Gauge.hpp"
8
9#include <boost/asio/spawn.hpp>
10#include <boost/asio/thread_pool.hpp>
11#include <boost/json/object.hpp>
12
13#include <atomic>
14#include <chrono>
15#include <cstddef>
16#include <cstdint>
17#include <functional>
18#include <limits>
19#include <optional>
20#include <queue>
21#include <utility>
22
23namespace rpc {
24
28struct Reportable {
29 virtual ~Reportable() = default;
30
36 [[nodiscard]] virtual boost::json::object
37 report() const = 0;
38};
39
43class WorkQueue : public Reportable {
44 using TaskType = std::function<void(boost::asio::yield_context)>;
45
46 struct TaskWithTimestamp {
47 TaskType task;
48 std::chrono::system_clock::time_point queuedAt;
49 };
50
51 using QueueType = std::queue<TaskWithTimestamp>;
52
53public:
57 enum class Priority : uint8_t {
58 High,
59 Default,
60 };
61
62private:
63 struct QueueState {
64 QueueType high;
65 QueueType normal;
66
67 size_t highPriorityCounter = 0;
68
69 void
70 push(Priority priority, TaskType&& task)
71 {
72 auto& queue = [this, priority] -> QueueType& {
73 if (priority == Priority::High)
74 return high;
75 return normal;
76 }();
77 queue.push(
78 TaskWithTimestamp{
79 .task = std::move(task),
80 .queuedAt = std::chrono::system_clock::now(),
81 }
82 );
83 }
84
85 [[nodiscard]] bool
86 empty() const
87 {
88 return high.empty() and normal.empty();
89 }
90
91 [[nodiscard]] std::optional<TaskWithTimestamp>
92 popNext()
93 {
94 if (not high.empty() and (highPriorityCounter < kTakeHighPrio or normal.empty())) {
95 auto taskWithTimestamp = std::move(high.front());
96 high.pop();
97 ++highPriorityCounter;
98 return taskWithTimestamp;
99 }
100
101 if (not normal.empty()) {
102 auto taskWithTimestamp = std::move(normal.front());
103 normal.pop();
104 highPriorityCounter = 0;
105 return taskWithTimestamp;
106 }
107
108 return std::nullopt;
109 }
110 };
111
112private:
113 static constexpr auto kTakeHighPrio = 4uz;
114
115 // these are cumulative for the lifetime of the process
116 std::reference_wrapper<util::prometheus::CounterInt> queued_;
117 std::reference_wrapper<util::prometheus::CounterInt> durationUs_;
118
119 std::reference_wrapper<util::prometheus::GaugeInt> curSize_;
120 uint32_t maxSize_ = std::numeric_limits<uint32_t>::max();
121
122 util::Logger log_{"RPC"};
123 boost::asio::thread_pool ioc_;
124
125 std::atomic_bool stopping_;
126 std::atomic_bool processingStarted_{false};
127
128 class OneTimeCallable {
129 std::function<void()> func_;
130 bool called_{false};
131
132 public:
133 void
134 setCallable(std::function<void()> func);
135
136 void
137 operator()();
138
139 explicit
140 operator bool() const;
141 };
142 util::Mutex<OneTimeCallable> onQueueEmpty_;
143 util::Mutex<QueueState> queueState_;
144
145public:
147 static constexpr DontStartProcessingTag kDontStartProcessingTag = {};
148
157 WorkQueue(std::uint32_t numWorkers, uint32_t maxSize = 0);
158
167 WorkQueue(DontStartProcessingTag, std::uint32_t numWorkers, uint32_t maxSize = 0);
168
169 ~WorkQueue() override;
170
174 void
176
183 void
184 requestStop(std::function<void()> onQueueEmpty = [] {});
185
189 void
190 stop();
191
198 [[nodiscard]] static WorkQueue
200
212 bool
213 postCoro(TaskType func, bool isWhiteListed, Priority priority = Priority::Default);
214
220 [[nodiscard]] boost::json::object
221 report() const override;
222
226 void
228
234 [[nodiscard]] size_t
235 size() const;
236
237private:
238 void
239 executeTask(boost::asio::yield_context yield);
240};
241
242} // namespace rpc
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:43
void stop()
Put the work queue into a stopping state and await workers to finish.
Definition WorkQueue.cpp:127
WorkQueue(std::uint32_t numWorkers, uint32_t maxSize=0)
Create an instance of the work queue.
Definition WorkQueue.cpp:64
boost::json::object report() const override
Generate a report of the work queue state.
Definition WorkQueue.cpp:157
void startProcessing()
Start processing of the enqueued tasks.
Definition WorkQueue.cpp:76
bool postCoro(TaskType func, bool isWhiteListed, Priority priority=Priority::Default)
Submit a job to the work queue.
Definition WorkQueue.cpp:89
Priority
Represents a task scheduling priority.
Definition WorkQueue.hpp:57
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:170
static WorkQueue makeWorkQueue(util::config::ClioConfigDefinition const &config)
A factory function that creates the work queue based on a config.
Definition WorkQueue.cpp:144
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:118
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
An interface for any class providing a report as json object.
Definition WorkQueue.hpp:28
virtual boost::json::object report() const =0
Generate a report of the work queue state.
Definition WorkQueue.hpp:146