NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
neograph::util::RequestQueue Class Reference

Lock-free task queue with a fixed worker thread pool and backpressure. More...

#include <request_queue.h>

Classes

struct  Stats
 Runtime statistics for monitoring queue health. More...
 

Public Member Functions

void close ()
 Stop accepting work, cancel queued tasks, and wait for workers.
 
bool is_closed () const noexcept
 True once close() has started rejecting new work.
 
 RequestQueue (size_t num_workers=128, size_t max_queue_size=10000)
 Construct a request queue with a worker thread pool.
 
Stats stats () const
 Get current queue statistics.
 
template<typename F >
std::pair< bool, std::future< void > > submit (F &&task)
 Submit a task to the queue.
 
 ~RequestQueue ()
 Destructor: applies the same cancel-on-close policy as close().
 

Detailed Description

Lock-free task queue with a fixed worker thread pool and backpressure.

Tasks are submitted via submit() and executed asynchronously by worker threads. When the queue is full, new tasks are rejected (backpressure).

RequestQueue queue(4, 1000); // 4 workers, max 1000 pending tasks
auto [accepted, future] = queue.submit([]{ do_work(); });
if (accepted) future.wait();
Lock-free task queue with a fixed worker thread pool and backpressure.

Definition at line 96 of file request_queue.h.

Constructor & Destructor Documentation

◆ RequestQueue()

neograph::util::RequestQueue::RequestQueue ( size_t  num_workers = 128,
size_t  max_queue_size = 10000 
)
inline

Construct a request queue with a worker thread pool.

Parameters
num_workersNumber of worker threads to spawn (default: 128).
max_queue_sizeMaximum number of pending tasks before backpressure (default: 10000).
Exceptions
std::invalid_argumentif num_workers is zero.

Definition at line 322 of file request_queue.h.

Member Function Documentation

◆ close()

void neograph::util::RequestQueue::close ( )
inline

Stop accepting work, cancel queued tasks, and wait for workers.

The first caller transitions the queue to closed. Work a worker claimed before that transition may finish; all unclaimed work completes its future with std::runtime_error("RequestQueue is closed"). Concurrent and repeated external callers wait for that same shutdown to complete. A task may call close() to initiate shutdown, but cannot wait for itself.

Definition at line 381 of file request_queue.h.

Referenced by ~RequestQueue().

◆ stats()

Stats neograph::util::RequestQueue::stats ( ) const
inline

Get current queue statistics.

Returns
Stats snapshot with pending, active, completed, and rejected counts.

Definition at line 395 of file request_queue.h.

◆ submit()

template<typename F >
std::pair< bool, std::future< void > > neograph::util::RequestQueue::submit ( F &&  task)
inline

Submit a task to the queue.

Admission atomically reserves one pending slot before enqueueing, so concurrent submitters cannot exceed max_queue_size. A full queue returns {false, invalid_future}. If the underlying queue rejects an already reserved task, the reservation is rolled back and the returned future is valid but completes with an exception.

Template Parameters
FCallable type (must be invocable with no arguments).
Parameters
taskThe task to execute.
Returns
A pair of {accepted, future}: accepted is true if the task was enqueued; future can be waited on for completion. Capacity rejection returns an invalid future; enqueue failure returns a valid future that propagates the failure. Closure rejection returns an invalid future.

Definition at line 368 of file request_queue.h.


The documentation for this class was generated from the following file: