#include <userver/utils/token_bucket.hpp>
Thread safe ratelimiter
Definition at line 16 of file token_bucket.hpp.
|
| using | TimePoint = std::chrono::steady_clock::time_point |
| |
| using | Duration = std::chrono::steady_clock::duration |
| |
|
|
| TokenBucket () noexcept |
| | Create an initially always empty token bucket.
|
| |
|
| TokenBucket (size_t max_size, RefillPolicy policy) |
| | Create a token bucket with max_size tokens and a specified refill policy.
|
| |
| | TokenBucket (size_t max_size, Duration single_token_update_interval) |
| |
|
| TokenBucket (const TokenBucket &)=delete |
| |
|
| TokenBucket (TokenBucket &&) noexcept |
| |
|
TokenBucket & | operator= (const TokenBucket &)=delete |
| |
|
TokenBucket & | operator= (TokenBucket &&) noexcept |
| |
|
bool | IsUnbounded () const |
| |
|
size_t | GetMaxSizeApprox () const |
| | Get current token limit (might be inaccurate as the result is stale)
|
| |
|
size_t | GetRefillAmountApprox () const |
| | Get current refill amount (might be inaccurate as the result is stale)
|
| |
|
Duration | GetRefillIntervalApprox () const |
| | Get current refill interval (might be inaccurate as the result is stale)
|
| |
|
double | GetRatePs () const |
| | Get rate (tokens per second)
|
| |
|
size_t | GetTokensApprox () |
| | Get current token count (might be inaccurate as the result is stale)
|
| |
|
void | SetMaxSize (size_t max_size) |
| | Set max token count.
|
| |
| void | SetUpdateInterval (Duration single_token_update_interval) |
| |
|
void | SetRefillPolicy (RefillPolicy policy) |
| | Set refill policy for the bucket.
|
| |
| void | SetInstantRefillPolicy () |
| |
| bool | Obtain () |
| |
| bool | ObtainAll (size_t count) |
| |
|
|
static TokenBucket | MakeUnbounded () noexcept |
| | Create an initially unbounded token bucket (largest size, instant refill)
|
| |
|
static double | GetRatePs (Duration interval) |
| | Get rate for specified update interval (updates per second)
|
| |
◆ Duration
| using utils::TokenBucket::Duration = std::chrono::steady_clock::duration |
◆ TimePoint
| using utils::TokenBucket::TimePoint = std::chrono::steady_clock::time_point |
◆ TokenBucket()
| utils::TokenBucket::TokenBucket |
( |
size_t | max_size, |
|
|
Duration | single_token_update_interval ) |
Start with max_size tokens and add 1 token each single_token_update_interval up to max_size. Zero duration means "no limit".
◆ Obtain()
| bool utils::TokenBucket::Obtain |
( |
| ) |
|
|
nodiscard |
- Returns
- true if token was successfully obtained
◆ ObtainAll()
| bool utils::TokenBucket::ObtainAll |
( |
size_t | count | ) |
|
|
nodiscard |
- Returns
- true if the requested number of tokens was successfully obtained
◆ SetInstantRefillPolicy()
| void utils::TokenBucket::SetInstantRefillPolicy |
( |
| ) |
|
Set refill policy to "instant refill".
Obtain does not deplete the bucket in this mode. Equivalent to amount=1, interval=zero() policy.
◆ SetUpdateInterval()
| void utils::TokenBucket::SetUpdateInterval |
( |
Duration | single_token_update_interval | ) |
|
Add 1 token each token_update_interval. Zero duration means "no limit".
The documentation for this class was generated from the following file: