algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::RateLimitController Class Reference

The rate limiter of one connection (one app key): a RateLimitBucket on the steady clock, plus counters. More...

#include <algotradelib/src/network/rate_limit.hpp>

Public Types

using Duration = RateLimitBucket::Duration
 The duration type.

Public Member Functions

 RateLimitController (RateLimitBudget budget={})
 Constructs a controller.
Duration delay_until (RateLimitGroup group) const
 Returns how long a request of a class must wait.
bool acquire (RateLimitGroup group)
 Admits one request of a class if it may go now, and counts it.
void record_delay (RateLimitGroup group, Duration waited)
 Records that a request waited before admission.
void record_shed (RateLimitGroup group)
 Records that a request was refused unsent.
void hold (Duration duration)
 Counts a broker refusal and holds every request for a duration, keeping the budget.
void shrink (int burst, int per_second, Duration retry)
 Counts a broker refusal, adopts the burst and rate the broker reported, and holds.
const RateLimitBudget & budget () const noexcept
 Returns the budget in force.
const RateLimitStats & stats () const noexcept
 Returns the counters.

Detailed Description

The rate limiter of one connection (one app key): a RateLimitBucket on the steady clock, plus counters.

Not synchronized: use it only from the owning client's strand (see AsyncHttpClient::rate_limits).

Definition at line 397 of file rate_limit.hpp.

Member Typedef Documentation

◆ Duration

Constructor & Destructor Documentation

◆ RateLimitController()

algotradelib::network::RateLimitController::RateLimitController ( RateLimitBudget budget = {})
inlineexplicit

Constructs a controller.

Parameters
budgetThe budget.
Exceptions
std::invalid_argumentif its burst or rate is below 1.

Definition at line 407 of file rate_limit.hpp.

Member Function Documentation

◆ acquire()

bool algotradelib::network::RateLimitController::acquire ( RateLimitGroup group)
inline

Admits one request of a class if it may go now, and counts it.

Parameters
groupThe class.
Returns
true if admitted; false if it must wait, with nothing changed.

Definition at line 427 of file rate_limit.hpp.

◆ budget()

const RateLimitBudget & algotradelib::network::RateLimitController::budget ( ) const
inlinenoexcept

Returns the budget in force.

Returns
The budget.

Definition at line 488 of file rate_limit.hpp.

Referenced by RateLimitController().

◆ delay_until()

Duration algotradelib::network::RateLimitController::delay_until ( RateLimitGroup group) const
inline

Returns how long a request of a class must wait.

Parameters
groupThe class.
Returns
The wait, zero if it may go now.

Definition at line 418 of file rate_limit.hpp.

◆ hold()

void algotradelib::network::RateLimitController::hold ( Duration duration)
inline

Counts a broker refusal and holds every request for a duration, keeping the budget.

For a refusal this client's pacing could not have prevented (capacity shared with other customers). The refusal is counted in RateLimitStats::broker_rejections_, not per class.

Parameters
durationThe hold.

Definition at line 462 of file rate_limit.hpp.

◆ record_delay()

void algotradelib::network::RateLimitController::record_delay ( RateLimitGroup group,
Duration waited )
inline

Records that a request waited before admission.

Parameters
groupThe class.
waitedHow long.

Definition at line 442 of file rate_limit.hpp.

◆ record_shed()

void algotradelib::network::RateLimitController::record_shed ( RateLimitGroup group)
inline

Records that a request was refused unsent.

Parameters
groupThe class.

Definition at line 452 of file rate_limit.hpp.

◆ shrink()

void algotradelib::network::RateLimitController::shrink ( int burst,
int per_second,
Duration retry )
inline

Counts a broker refusal, adopts the burst and rate the broker reported, and holds.

The trading reserve is reduced if it would not be smaller than the new burst.

Parameters
burstThe broker's burst.
per_secondThe broker's rate.
retryHow long to hold; zero or less holds for one interval.
Exceptions
std::invalid_argumentif burst or per_second is below 1.

Definition at line 477 of file rate_limit.hpp.

◆ stats()

const RateLimitStats & algotradelib::network::RateLimitController::stats ( ) const
inlinenoexcept

Returns the counters.

Returns
The statistics.

Definition at line 494 of file rate_limit.hpp.


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