kalshi-cpp 0.6.2
C++23 client for Kalshi's Predictions API
Loading...
Searching...
No Matches
kalshi::TokenBucket Class Reference

Token bucket that refills continuously, the model Kalshi's rate limits use. More...

#include <kalshi/rate_limit.hpp>

Classes

struct  Config
 

Public Member Functions

 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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ TokenBucket()

kalshi::TokenBucket::TokenBucket ( Config  config)
explicit

Member Function Documentation

◆ 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: