78 token_bucket(std::size_t tokens_per_second, std::size_t burst_size);
101 [[nodiscard]]
auto try_acquire(std::size_t tokens = 1) -> bool;
120 std::chrono::milliseconds timeout) -> bool;
144 ->
std::chrono::nanoseconds;
154 auto
set_rate(
std::
size_t tokens_per_second) ->
void;
170 [[nodiscard]] auto
get_rate() const ->
std::
size_t;
185 auto
reset() ->
void;
Lock-free token bucket rate limiter for controlling throughput.
token_bucket & operator=(const token_bucket &)=delete
token_bucket(token_bucket &&)=delete
auto get_rate() const -> std::size_t
Returns the current refill rate.
std::atomic< std::int64_t > max_tokens_
Maximum tokens (burst size) scaled by precision factor.
auto try_acquire_for(std::size_t tokens, std::chrono::milliseconds timeout) -> bool
Attempts to acquire tokens with a timeout.
std::atomic< std::chrono::steady_clock::time_point::rep > last_refill_
Timestamp of last token refill.
token_bucket(std::size_t tokens_per_second, std::size_t burst_size)
Constructs a token bucket with the specified rate and burst size.
auto time_until_available(std::size_t tokens) const -> std::chrono::nanoseconds
Calculates time until the specified tokens become available.
~token_bucket()=default
Default destructor.
auto get_burst_size() const -> std::size_t
Returns the maximum bucket capacity.
token_bucket & operator=(token_bucket &&)=delete
std::atomic< double > refill_rate_
Token refill rate in nano-tokens per nanosecond.
std::atomic< std::int64_t > tokens_
Current token count (scaled by 1000 for sub-token precision).
auto available_tokens() const -> std::size_t
Returns the current number of available tokens.
auto set_burst_size(std::size_t burst_size) -> void
Updates the maximum bucket capacity.
auto reset() -> void
Resets the bucket to full capacity.
token_bucket(const token_bucket &)=delete
auto refill() -> void
Refills tokens based on elapsed time since last refill.
auto try_acquire(std::size_t tokens=1) -> bool
Attempts to acquire tokens without waiting.
static constexpr std::int64_t PRECISION_FACTOR
Precision factor for fixed-point token calculations.
auto set_rate(std::size_t tokens_per_second) -> void
Updates the token refill rate.
Core threading foundation of the thread system library.