|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Lock-free token bucket rate limiter for controlling throughput. More...
#include <token_bucket.h>

Public Member Functions | |
| token_bucket (std::size_t tokens_per_second, std::size_t burst_size) | |
| Constructs a token bucket with the specified rate and burst size. | |
| ~token_bucket ()=default | |
| Default destructor. | |
| token_bucket (const token_bucket &)=delete | |
| token_bucket & | operator= (const token_bucket &)=delete |
| token_bucket (token_bucket &&)=delete | |
| token_bucket & | operator= (token_bucket &&)=delete |
| auto | try_acquire (std::size_t tokens=1) -> bool |
| Attempts to acquire tokens without waiting. | |
| auto | try_acquire_for (std::size_t tokens, std::chrono::milliseconds timeout) -> bool |
| Attempts to acquire tokens with a timeout. | |
| auto | available_tokens () const -> std::size_t |
| Returns the current number of available tokens. | |
| auto | time_until_available (std::size_t tokens) const -> std::chrono::nanoseconds |
| Calculates time until the specified tokens become available. | |
| auto | set_rate (std::size_t tokens_per_second) -> void |
| Updates the token refill rate. | |
| auto | set_burst_size (std::size_t burst_size) -> void |
| Updates the maximum bucket capacity. | |
| auto | get_rate () const -> std::size_t |
| Returns the current refill rate. | |
| auto | get_burst_size () const -> std::size_t |
| Returns the maximum bucket capacity. | |
| auto | reset () -> void |
| Resets the bucket to full capacity. | |
Private Member Functions | |
| auto | refill () -> void |
| Refills tokens based on elapsed time since last refill. | |
Private Attributes | |
| std::atomic< std::int64_t > | tokens_ |
| Current token count (scaled by 1000 for sub-token precision). | |
| std::atomic< std::int64_t > | max_tokens_ |
| Maximum tokens (burst size) scaled by precision factor. | |
| std::atomic< double > | refill_rate_ |
| Token refill rate in nano-tokens per nanosecond. | |
| std::atomic< std::chrono::steady_clock::time_point::rep > | last_refill_ |
| Timestamp of last token refill. | |
Static Private Attributes | |
| static constexpr std::int64_t | PRECISION_FACTOR = 1000 |
| Precision factor for fixed-point token calculations. | |
Lock-free token bucket rate limiter for controlling throughput.
The token bucket algorithm is a metering mechanism that controls the rate at which operations can proceed. Tokens are added to a bucket at a fixed rate, and operations consume tokens. If no tokens are available, the operation either waits or is rejected.
All methods are thread-safe and lock-free. Multiple threads can concurrently acquire tokens without blocking each other.
Definition at line 68 of file token_bucket.h.
| kcenon::thread::token_bucket::token_bucket | ( | std::size_t | tokens_per_second, |
| std::size_t | burst_size ) |
Constructs a token bucket with the specified rate and burst size.
Constructs a token bucket with the specified rate and capacity.
| tokens_per_second | Number of tokens added per second. |
| burst_size | Maximum tokens that can accumulate (bucket capacity). |
The bucket starts full (burst_size tokens available).
Implementation details:
| tokens_per_second | Number of tokens to add per second |
| burst_size | Maximum tokens that can accumulate |
Definition at line 33 of file token_bucket.cpp.
|
default |
Default destructor.
|
delete |
|
delete |
|
nodiscard |
Returns the current number of available tokens.
Returns current available tokens.
This is a snapshot that may become stale immediately in a multi-threaded environment.
Thread Safety: Lock-free read.
Implementation details:
Definition at line 201 of file token_bucket.cpp.
References PRECISION_FACTOR, refill(), and tokens_.

|
nodiscard |
Returns the maximum bucket capacity.
Definition at line 316 of file token_bucket.cpp.
References max_tokens_, and PRECISION_FACTOR.
|
nodiscard |
Returns the current refill rate.
Definition at line 305 of file token_bucket.cpp.
References PRECISION_FACTOR, and refill_rate_.
|
delete |
|
delete |
|
private |
Refills tokens based on elapsed time since last refill.
Uses CAS loop to atomically update both tokens and timestamp. Called internally before each token acquisition attempt.
Implementation details:
Algorithm:
Definition at line 57 of file token_bucket.cpp.
Referenced by available_tokens().

| auto kcenon::thread::token_bucket::reset | ( | ) | -> void |
Resets the bucket to full capacity.
Restores tokens to burst_size and resets the last refill time.
Thread Safety: Lock-free write.
Implementation details:
Definition at line 330 of file token_bucket.cpp.
| auto kcenon::thread::token_bucket::set_burst_size | ( | std::size_t | burst_size | ) | -> void |
Updates the maximum bucket capacity.
| burst_size | New maximum tokens. |
If current tokens exceed new capacity, excess tokens are discarded.
Thread Safety: Lock-free write.
Implementation details:
| burst_size | New maximum capacity |
Definition at line 282 of file token_bucket.cpp.
| auto kcenon::thread::token_bucket::set_rate | ( | std::size_t | tokens_per_second | ) | -> void |
Updates the token refill rate.
| tokens_per_second | New tokens per second rate. |
Takes effect immediately. Does not affect currently accumulated tokens.
Thread Safety: Lock-free write.
Implementation details:
| tokens_per_second | New refill rate |
Definition at line 263 of file token_bucket.cpp.
|
nodiscard |
Calculates time until the specified tokens become available.
Calculates time until tokens become available.
| tokens | Number of tokens needed. |
Useful for implementing waiting strategies or displaying estimated wait times to users.
Thread Safety: Lock-free calculation.
Implementation details:
| tokens | Number of tokens needed |
Definition at line 225 of file token_bucket.cpp.
|
nodiscard |
Attempts to acquire tokens without waiting.
| tokens | Number of tokens to acquire (default: 1). |
This method is non-blocking and returns immediately. If the bucket doesn't have enough tokens, the operation fails without waiting.
Thread Safety: Lock-free, safe for concurrent calls.
Implementation details:
Thread Safety:
| tokens | Number of tokens to acquire |
Definition at line 117 of file token_bucket.cpp.
|
nodiscard |
Attempts to acquire tokens with a timeout.
Attempts to acquire tokens with timeout and backoff.
| tokens | Number of tokens to acquire. |
| timeout | Maximum time to wait for tokens. |
This method will spin-wait (with backoff) until either:
Implementation uses exponential backoff to reduce CPU usage while waiting for token refill.
Thread Safety: Lock-free with cooperative spin-waiting.
Implementation details:
Backoff Strategy:
| tokens | Number of tokens to acquire |
| timeout | Maximum time to wait |
Definition at line 161 of file token_bucket.cpp.
|
private |
Timestamp of last token refill.
Definition at line 219 of file token_bucket.h.
|
private |
Maximum tokens (burst size) scaled by precision factor.
Definition at line 207 of file token_bucket.h.
Referenced by get_burst_size().
|
staticconstexprprivate |
Precision factor for fixed-point token calculations.
Using 1000 allows for milli-token precision without floating point.
Definition at line 226 of file token_bucket.h.
Referenced by available_tokens(), get_burst_size(), and get_rate().
|
private |
Token refill rate in nano-tokens per nanosecond.
Calculated as: (tokens_per_second * PRECISION_FACTOR) / 1e9
Definition at line 214 of file token_bucket.h.
Referenced by get_rate().
|
private |
Current token count (scaled by 1000 for sub-token precision).
We use fixed-point arithmetic to avoid floating-point atomics. Actual tokens = tokens_ / PRECISION_FACTOR
Definition at line 202 of file token_bucket.h.
Referenced by available_tokens().