Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
rpc::WorkQueue Class Reference

An asynchronous, thread-safe queue for RPC requests. More...

#include <WorkQueue.hpp>

Inheritance diagram for rpc::WorkQueue:
Collaboration diagram for rpc::WorkQueue:

Classes

struct  DontStartProcessingTag

Public Types

enum class  Priority : uint8_t { High , Default }
 Represents a task scheduling priority.

Public Member Functions

 WorkQueue (std::uint32_t numWorkers, uint32_t maxSize=0)
 Create an instance of the work queue.
 WorkQueue (DontStartProcessingTag, std::uint32_t numWorkers, uint32_t maxSize=0)
 Create an instance of the work queue without starting the processing of events.
void startProcessing ()
 Start processing of the enqueued tasks.
void requestStop (std::function< void()> onQueueEmpty=[] {})
 Put the work queue into a stopping state. This will prevent new jobs from being queued.
void stop ()
 Put the work queue into a stopping state and await workers to finish.
bool postCoro (TaskType func, bool isWhiteListed, Priority priority=Priority::Default)
 Submit a job to the work queue.
boost::json::object report () const override
 Generate a report of the work queue state.
void join ()
 Wait until all the jobs in the queue are finished.
size_t size () const
 Get the size of the queue.

Static Public Member Functions

static WorkQueue makeWorkQueue (util::config::ClioConfigDefinition const &config)
 A factory function that creates the work queue based on a config.

Static Public Attributes

static constexpr DontStartProcessingTag kDONT_START_PROCESSING_TAG = {}

Detailed Description

An asynchronous, thread-safe queue for RPC requests.

Constructor & Destructor Documentation

◆ WorkQueue() [1/2]

rpc::WorkQueue::WorkQueue ( std::uint32_t numWorkers,
uint32_t maxSize = 0 )

Create an instance of the work queue.

The work queue immediately starts to process tasks as they come.

Parameters
numWorkersThe amount of threads to spawn in the pool
maxSizeThe maximum capacity of the queue; 0 means unlimited

◆ WorkQueue() [2/2]

rpc::WorkQueue::WorkQueue ( DontStartProcessingTag ,
std::uint32_t numWorkers,
uint32_t maxSize = 0 )

Create an instance of the work queue without starting the processing of events.

Clients are expected to call startProcessing manually once ready to start processing tasks.

Parameters
numWorkersThe amount of threads to spawn in the pool
maxSizeThe maximum capacity of the queue; 0 means unlimited

Member Function Documentation

◆ makeWorkQueue()

WorkQueue rpc::WorkQueue::makeWorkQueue ( util::config::ClioConfigDefinition const & config)
staticnodiscard

A factory function that creates the work queue based on a config.

Parameters
configThe Clio config to use
Returns
The work queue

◆ postCoro()

bool rpc::WorkQueue::postCoro ( TaskType func,
bool isWhiteListed,
Priority priority = Priority::Default )

Submit a job to the work queue.

The job will be rejected if isWhiteListed is set to false and the current size of the queue reached capacity.

Parameters
funcThe function object to queue as a job
isWhiteListedWhether the queue capacity applies to this job
priorityThe priority of the task
Returns
true if the job was successfully queued; false otherwise

◆ report()

boost::json::object rpc::WorkQueue::report ( ) const
nodiscardoverridevirtual

Generate a report of the work queue state.

Returns
The report as a JSON object.

Implements rpc::Reportable.

◆ requestStop()

void rpc::WorkQueue::requestStop ( std::function< void()> onQueueEmpty = [] {})

Put the work queue into a stopping state. This will prevent new jobs from being queued.

Parameters
onQueueEmptyA callback to run when the last task in the queue is completed

◆ size()

size_t rpc::WorkQueue::size ( ) const
nodiscard

Get the size of the queue.

Returns
The number of jobs in the queue.

The documentation for this class was generated from the following files:
  • /__w/clio/clio/src/rpc/WorkQueue.hpp
  • /__w/clio/clio/src/rpc/WorkQueue.cpp