Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
token_bucket.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#pragma once
12
13#include <atomic>
14#include <chrono>
15#include <cstddef>
16
17namespace kcenon::thread
18{
69 {
70 public:
78 token_bucket(std::size_t tokens_per_second, std::size_t burst_size);
79
83 ~token_bucket() = default;
84
85 // Non-copyable, non-movable for thread safety
86 token_bucket(const token_bucket&) = delete;
90
101 [[nodiscard]] auto try_acquire(std::size_t tokens = 1) -> bool;
102
118 [[nodiscard]] auto try_acquire_for(
119 std::size_t tokens,
120 std::chrono::milliseconds timeout) -> bool;
121
131 [[nodiscard]] auto available_tokens() const -> std::size_t;
132
143 [[nodiscard]] auto time_until_available(std::size_t tokens) const
144 -> std::chrono::nanoseconds;
145
154 auto set_rate(std::size_t tokens_per_second) -> void;
155
164 auto set_burst_size(std::size_t burst_size) -> void;
165
170 [[nodiscard]] auto get_rate() const -> std::size_t;
171
176 [[nodiscard]] auto get_burst_size() const -> std::size_t;
177
185 auto reset() -> void;
186
187 private:
194 auto refill() -> void;
195
202 std::atomic<std::int64_t> tokens_;
203
207 std::atomic<std::int64_t> max_tokens_;
208
214 std::atomic<double> refill_rate_;
215
219 std::atomic<std::chrono::steady_clock::time_point::rep> last_refill_;
220
226 static constexpr std::int64_t PRECISION_FACTOR = 1000;
227 };
228
229} // namespace kcenon::thread
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.
Definition thread_impl.h:17
STL namespace.