D++ (DPP)
C++ Discord API Bot Library
dpp::request_queue Class Reference

The request_queue class manages rate limits and marshalls HTTP requests that have been built as http_request objects. More...

#include <queues.h>

+ Collaboration diagram for dpp::request_queue:

Classes

struct  completed_request
 A completed request. Contains both the request and the response. More...
 

Public Member Functions

 request_queue (class cluster *owner, uint32_t request_concurrency=8)
 constructor More...
 
uint32_t get_request_queue_count () const
 Get the request queue concurrency count. More...
 
 ~request_queue ()
 Destroy the request queue object. Side effects: Ends and deletes concurrency timers. More...
 
request_queuepost_request (std::unique_ptr< http_request > req)
 Put a http_request into the request queue. More...
 
bool is_globally_ratelimited () const
 Returns true if the bot is currently globally rate limited. More...
 
size_t get_active_request_count () const
 Returns the number of active requests on this queue. More...
 

Public Attributes

class clustercreator
 The cluster that owns this request_queue. More...
 
std::vector< std::unique_ptr< request_concurrency_queue > > requests_in
 A vector of timers forming a pool. More...
 
std::atomic< bool > terminating
 Set to true if the timers should terminate. When this is set to true no further requests are accepted to the queues. More...
 
bool globally_ratelimited
 True if globally rate limited. More...
 
time_t globally_limited_until
 When we are globally rate limited until (unix epoch) More...
 
uint32_t in_queue_pool_size
 Number of request queues in the pool. This is the direct size of the requests_in vector. More...
 

Friends

class request_concurrency_queue
 Required so request_concurrency_queue can access these member variables. More...
 

Detailed Description

The request_queue class manages rate limits and marshalls HTTP requests that have been built as http_request objects.

It ensures asynchronous delivery of events and queueing of requests.

It will spawn multiple timers to make outbound HTTP requests and then call the callbacks of those requests on completion within the dpp::thread_pool for the cluster. If the user decides to take a long time processing a reply in their callback it won't affect when other requests are sent, and if a HTTP request takes a long time due to latency, it won't hold up user processing.

There are usually two request_queue objects in each dpp::cluster, one of which is used internally for the various REST methods to Discord such as sending messages, and the other used to support user REST calls via dpp::cluster::request(). They are separated so that the one for user requests can be specifically configured to never ever send the Discord token unless it is explicitly placed into the request, for security reasons.

Constructor & Destructor Documentation

◆ request_queue()

dpp::request_queue::request_queue ( class cluster owner,
uint32_t  request_concurrency = 8 
)

constructor

Parameters
ownerThe creating cluster.
request_concurrencyThe number of http request queues to allocate. Each request queue is a dpp::timer which ticks every second looking for new requests to run. The timer will hold back requests if we are waiting as to comply with rate limits. Adding a request to this class will cause the queue it is placed in to run immediately but this cannot override rate limits. By default eight concurrency queues are allocated. Side effects: Creates timers for the queue

◆ ~request_queue()

dpp::request_queue::~request_queue ( )

Destroy the request queue object. Side effects: Ends and deletes concurrency timers.

Member Function Documentation

◆ get_active_request_count()

size_t dpp::request_queue::get_active_request_count ( ) const

Returns the number of active requests on this queue.

Returns
Total number of active requests

◆ get_request_queue_count()

uint32_t dpp::request_queue::get_request_queue_count ( ) const

Get the request queue concurrency count.

Returns
uint32_t number of request queues that are active

◆ is_globally_ratelimited()

bool dpp::request_queue::is_globally_ratelimited ( ) const

Returns true if the bot is currently globally rate limited.

Returns
true if globally rate limited

◆ post_request()

request_queue & dpp::request_queue::post_request ( std::unique_ptr< http_request req)

Put a http_request into the request queue.

Note
Will use a simple hash function to determine which of the 'in queues' to place this request onto.
Parameters
reqrequest to add
Returns
reference to self

Friends And Related Function Documentation

◆ request_concurrency_queue

friend class request_concurrency_queue
friend

Required so request_concurrency_queue can access these member variables.

Member Data Documentation

◆ creator

class cluster* dpp::request_queue::creator

The cluster that owns this request_queue.

◆ globally_limited_until

time_t dpp::request_queue::globally_limited_until

When we are globally rate limited until (unix epoch)

Note
Only valid if globally_rate limited is true. If we are globally rate limited, queues in this class will not process requests until the current unix epoch time is greater than this time.

◆ globally_ratelimited

bool dpp::request_queue::globally_ratelimited

True if globally rate limited.

When globally rate limited the concurrency queues associated with this request queue will not process any requests in their timers until the global rate limit expires.

◆ in_queue_pool_size

uint32_t dpp::request_queue::in_queue_pool_size

Number of request queues in the pool. This is the direct size of the requests_in vector.

◆ requests_in

std::vector<std::unique_ptr<request_concurrency_queue> > dpp::request_queue::requests_in

A vector of timers forming a pool.

There are a set number of these defined by a constant in cluster.cpp. A request is always placed on the same element in this vector, based upon its url, so that two conditions are satisfied:

1) Any requests for the same ratelimit bucket are handled by the same concurrency queue in the pool so that they do not create unnecessary 429 errors, 2) Requests for different endpoints go into different buckets, so that they may be requested in parallel A global ratelimit event pauses all timers in the pool. These are few and far between.

◆ terminating

std::atomic<bool> dpp::request_queue::terminating

Set to true if the timers should terminate. When this is set to true no further requests are accepted to the queues.

D++ Library version 10.0.35D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0