![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
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. | |
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.
| Clock | The clock whose instants are passed in. |
Definition at line 164 of file rate_limit.hpp.
| using algotradelib::network::BasicRateLimitBucket< Clock >::ClockType = Clock |
The clock type.
Definition at line 167 of file rate_limit.hpp.
| using algotradelib::network::BasicRateLimitBucket< Clock >::Duration = std::chrono::nanoseconds |
The duration type of all results.
Definition at line 173 of file rate_limit.hpp.
| using algotradelib::network::BasicRateLimitBucket< Clock >::TimePoint = typename Clock::time_point |
The clock's instant type.
Definition at line 170 of file rate_limit.hpp.
|
inline |
Constructs a bucket.
| burst | How many requests may go back to back after an idle period. |
| per_second | The sustained rate. |
| std::invalid_argument | if either is below 1. |
Definition at line 181 of file rate_limit.hpp.
Referenced by BasicRateLimitBucket().
|
inlineexplicit |
Constructs a bucket from a budget's burst and rate.
| budget | The budget. |
| std::invalid_argument | if either is below 1. |
Definition at line 188 of file rate_limit.hpp.
|
inline |
Returns how many requests could go back to back now.
| now | The current instant. |
| allowance | How much of the burst the caller may use. |
Definition at line 295 of file rate_limit.hpp.
|
inlinenoexcept |
Returns the burst.
Definition at line 312 of file rate_limit.hpp.
Referenced by BasicRateLimitBucket(), observe_limit(), and resize().
|
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.
| now | The current instant. |
| duration | The hold; a negative one counts as zero. |
Definition at line 265 of file rate_limit.hpp.
Referenced by observe_limit().
|
inline |
Returns how long a request must wait.
| now | The current instant. |
| allowance | How much of the burst the caller may use (clamped to 1..burst()). |
Definition at line 215 of file rate_limit.hpp.
Referenced by ready().
|
inlinenoexcept |
Returns the spacing of the sustained rate.
Definition at line 324 of file rate_limit.hpp.
|
inline |
Adopts a burst and rate the broker reported, then holds requests until it refills.
| now | The current instant. |
| burst | The broker's burst. |
| per_second | The broker's rate. |
| refill | How long to hold; zero or less holds for one interval. |
| std::invalid_argument | if burst or per_second is below 1. |
Definition at line 284 of file rate_limit.hpp.
|
inlinenoexcept |
Returns the sustained rate.
Definition at line 318 of file rate_limit.hpp.
Referenced by BasicRateLimitBucket(), observe_limit(), and resize().
|
inline |
Tests whether a request may go now.
| now | The current instant. |
| allowance | How much of the burst the caller may use. |
Definition at line 232 of file rate_limit.hpp.
Referenced by try_acquire().
|
inline |
Changes the burst and rate, keeping the current schedule.
| burst | The burst. |
| per_second | The sustained rate. |
| std::invalid_argument | if either is below 1. |
Definition at line 197 of file rate_limit.hpp.
Referenced by BasicRateLimitBucket(), and observe_limit().
|
inline |
Admits one request if it may go now.
| now | The current instant. |
| allowance | How much of the burst the caller may use. |
Definition at line 243 of file rate_limit.hpp.