Token bucket that refills continuously, the model Kalshi's rate limits use.
More...
#include <kalshi/rate_limit.hpp>
|
| | TokenBucket (Config config) |
| |
| bool | try_acquire (double cost=1.0) noexcept |
| | Takes cost tokens if the bucket holds them.
|
| |
| bool | acquire_for (double cost, std::chrono::nanoseconds max_wait) |
| | Waits up to max_wait for cost tokens.
|
| |
| std::chrono::nanoseconds | wait_time (double cost) const noexcept |
| | How long until cost tokens are available: zero if they are available now, nanoseconds::max() if they never will be (cost above capacity).
|
| |
| double | available () const noexcept |
| | Tokens available now. Zero while earlier waiters hold reservations.
|
| |
| void | reset () noexcept |
| |
| const Config & | config () const noexcept |
| |
Token bucket that refills continuously, the model Kalshi's rate limits use.
A request proceeds when the bucket covers its cost. Thread-safe, and waiting callers are served in arrival order: a waiter reserves its tokens up front, so a large request is not starved by a stream of small ones.
◆ TokenBucket()
| kalshi::TokenBucket::TokenBucket |
( |
Config |
config | ) |
|
|
explicit |
◆ try_acquire()
| bool kalshi::TokenBucket::try_acquire |
( |
double |
cost = 1.0 | ) |
|
|
noexcept |
Takes cost tokens if the bucket holds them.
◆ acquire_for()
| bool kalshi::TokenBucket::acquire_for |
( |
double |
cost, |
|
|
std::chrono::nanoseconds |
max_wait |
|
) |
| |
Waits up to max_wait for cost tokens.
Returns false without taking tokens if they would not arrive in time or cost exceeds the capacity.
◆ wait_time()
| std::chrono::nanoseconds kalshi::TokenBucket::wait_time |
( |
double |
cost | ) |
const |
|
noexcept |
How long until cost tokens are available: zero if they are available now, nanoseconds::max() if they never will be (cost above capacity).
◆ available()
| double kalshi::TokenBucket::available |
( |
| ) |
const |
|
noexcept |
Tokens available now. Zero while earlier waiters hold reservations.
◆ reset()
| void kalshi::TokenBucket::reset |
( |
| ) |
|
|
noexcept |
◆ config()
| const Config & kalshi::TokenBucket::config |
( |
| ) |
const |
|
inlinenoexcept |
The documentation for this class was generated from the following file: