algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::network::BasicRateLimitBucket< Clock > Class Template Reference

A rate limiter (the generic cell rate algorithm) that computes when a request may go; it neither waits nor reads the clock itself. More...

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

Public Types

using ClockType = Clock
 The clock type.
using TimePoint = typename Clock::time_point
 The clock's instant type.
using Duration = std::chrono::nanoseconds
 The duration type of all results.

Public Member Functions

 BasicRateLimitBucket (int burst, int per_second)
 Constructs a bucket.
 BasicRateLimitBucket (const RateLimitBudget &budget)
 Constructs a bucket from a budget's burst and rate.
void resize (int burst, int per_second)
 Changes the burst and rate, keeping the current schedule.
Duration delay_until (TimePoint now, int allowance) const
 Returns how long a request must wait.
bool ready (TimePoint now, int allowance) const
 Tests whether a request may go now.
bool try_acquire (TimePoint now, int allowance)
 Admits one request if it may go now.
void cool_down (TimePoint now, Duration duration)
 Holds every request for at least a duration (for a broker refusal that is not this client's pacing fault). A caller with a smaller allowance waits correspondingly longer.
void observe_limit (TimePoint now, int burst, int per_second, Duration refill)
 Adopts a burst and rate the broker reported, then holds requests until it refills.
int available (TimePoint now, int allowance) const
 Returns how many requests could go back to back now.
int burst () const noexcept
 Returns the burst.
int per_second () const noexcept
 Returns the sustained rate.
Duration interval () const noexcept
 Returns the spacing of the sustained rate.

Detailed Description

template<typename Clock = std::chrono::steady_clock>
class algotradelib::network::BasicRateLimitBucket< Clock >

A rate limiter (the generic cell rate algorithm) that computes when a request may go; it neither waits nor reads the clock itself.

Each call passes the current instant and an allowance: how much of the burst the caller may use (see usable_burst). A caller with a smaller allowance reaches its limit earlier, so one schedule can keep a reserve for trading.

Template Parameters
ClockThe clock whose instants are passed in.

Definition at line 164 of file rate_limit.hpp.

Member Typedef Documentation

◆ ClockType

template<typename Clock = std::chrono::steady_clock>
using algotradelib::network::BasicRateLimitBucket< Clock >::ClockType = Clock

The clock type.

Definition at line 167 of file rate_limit.hpp.

◆ Duration

template<typename Clock = std::chrono::steady_clock>
using algotradelib::network::BasicRateLimitBucket< Clock >::Duration = std::chrono::nanoseconds

The duration type of all results.

Definition at line 173 of file rate_limit.hpp.

◆ TimePoint

template<typename Clock = std::chrono::steady_clock>
using algotradelib::network::BasicRateLimitBucket< Clock >::TimePoint = typename Clock::time_point

The clock's instant type.

Definition at line 170 of file rate_limit.hpp.

Constructor & Destructor Documentation

◆ BasicRateLimitBucket() [1/2]

template<typename Clock = std::chrono::steady_clock>
algotradelib::network::BasicRateLimitBucket< Clock >::BasicRateLimitBucket ( int burst,
int per_second )
inline

Constructs a bucket.

Parameters
burstHow many requests may go back to back after an idle period.
per_secondThe sustained rate.
Exceptions
std::invalid_argumentif either is below 1.

Definition at line 181 of file rate_limit.hpp.

Referenced by BasicRateLimitBucket().

◆ BasicRateLimitBucket() [2/2]

template<typename Clock = std::chrono::steady_clock>
algotradelib::network::BasicRateLimitBucket< Clock >::BasicRateLimitBucket ( const RateLimitBudget & budget)
inlineexplicit

Constructs a bucket from a budget's burst and rate.

Parameters
budgetThe budget.
Exceptions
std::invalid_argumentif either is below 1.

Definition at line 188 of file rate_limit.hpp.

Member Function Documentation

◆ available()

template<typename Clock = std::chrono::steady_clock>
int algotradelib::network::BasicRateLimitBucket< Clock >::available ( TimePoint now,
int allowance ) const
inline

Returns how many requests could go back to back now.

Parameters
nowThe current instant.
allowanceHow much of the burst the caller may use.
Returns
The count, at most allowance.

Definition at line 295 of file rate_limit.hpp.

◆ burst()

template<typename Clock = std::chrono::steady_clock>
int algotradelib::network::BasicRateLimitBucket< Clock >::burst ( ) const
inlinenoexcept

Returns the burst.

Returns
The burst.

Definition at line 312 of file rate_limit.hpp.

Referenced by BasicRateLimitBucket(), observe_limit(), and resize().

◆ cool_down()

template<typename Clock = std::chrono::steady_clock>
void algotradelib::network::BasicRateLimitBucket< Clock >::cool_down ( TimePoint now,
Duration duration )
inline

Holds every request for at least a duration (for a broker refusal that is not this client's pacing fault). A caller with a smaller allowance waits correspondingly longer.

A hold never shortens an existing one.

Parameters
nowThe current instant.
durationThe hold; a negative one counts as zero.

Definition at line 265 of file rate_limit.hpp.

Referenced by observe_limit().

◆ delay_until()

template<typename Clock = std::chrono::steady_clock>
Duration algotradelib::network::BasicRateLimitBucket< Clock >::delay_until ( TimePoint now,
int allowance ) const
inline

Returns how long a request must wait.

Parameters
nowThe current instant.
allowanceHow much of the burst the caller may use (clamped to 1..burst()).
Returns
The wait, zero if it may go now.

Definition at line 215 of file rate_limit.hpp.

Referenced by ready().

◆ interval()

template<typename Clock = std::chrono::steady_clock>
Duration algotradelib::network::BasicRateLimitBucket< Clock >::interval ( ) const
inlinenoexcept

Returns the spacing of the sustained rate.

Returns
One second divided by the rate.

Definition at line 324 of file rate_limit.hpp.

◆ observe_limit()

template<typename Clock = std::chrono::steady_clock>
void algotradelib::network::BasicRateLimitBucket< Clock >::observe_limit ( TimePoint now,
int burst,
int per_second,
Duration refill )
inline

Adopts a burst and rate the broker reported, then holds requests until it refills.

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

Definition at line 284 of file rate_limit.hpp.

◆ per_second()

template<typename Clock = std::chrono::steady_clock>
int algotradelib::network::BasicRateLimitBucket< Clock >::per_second ( ) const
inlinenoexcept

Returns the sustained rate.

Returns
Requests per second.

Definition at line 318 of file rate_limit.hpp.

Referenced by BasicRateLimitBucket(), observe_limit(), and resize().

◆ ready()

template<typename Clock = std::chrono::steady_clock>
bool algotradelib::network::BasicRateLimitBucket< Clock >::ready ( TimePoint now,
int allowance ) const
inline

Tests whether a request may go now.

Parameters
nowThe current instant.
allowanceHow much of the burst the caller may use.
Returns
true if it need not wait.

Definition at line 232 of file rate_limit.hpp.

Referenced by try_acquire().

◆ resize()

template<typename Clock = std::chrono::steady_clock>
void algotradelib::network::BasicRateLimitBucket< Clock >::resize ( int burst,
int per_second )
inline

Changes the burst and rate, keeping the current schedule.

Parameters
burstThe burst.
per_secondThe sustained rate.
Exceptions
std::invalid_argumentif either is below 1.

Definition at line 197 of file rate_limit.hpp.

Referenced by BasicRateLimitBucket(), and observe_limit().

◆ try_acquire()

template<typename Clock = std::chrono::steady_clock>
bool algotradelib::network::BasicRateLimitBucket< Clock >::try_acquire ( TimePoint now,
int allowance )
inline

Admits one request if it may go now.

Parameters
nowThe current instant.
allowanceHow much of the burst the caller may use.
Returns
true if admitted (the schedule advances one interval); false if it must wait, with nothing changed.

Definition at line 243 of file rate_limit.hpp.


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