userver: utils::TokenBucket Class Reference
No Matches
utils::TokenBucket Class Referencefinal

#include <userver/utils/token_bucket.hpp>

Detailed Description

Thread safe ratelimiter

Definition at line 16 of file token_bucket.hpp.


struct  RefillPolicy
 Token bucket refill policy. More...

Public Types

using TimePoint = std::chrono::steady_clock::time_point
using Duration = std::chrono::steady_clock::duration

Public Member Functions

 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
TokenBucketoperator= (const TokenBucket &)=delete
TokenBucketoperator= (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 Public Member Functions

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)

Member Typedef Documentation

◆ Duration

using utils::TokenBucket::Duration = std::chrono::steady_clock::duration

Definition at line 19 of file token_bucket.hpp.

◆ TimePoint

using utils::TokenBucket::TimePoint = std::chrono::steady_clock::time_point

Definition at line 18 of file token_bucket.hpp.

Constructor & Destructor Documentation

◆ 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".

Member Function Documentation

◆ Obtain()

bool utils::TokenBucket::Obtain ( )
true if token was successfully obtained

◆ ObtainAll()

bool utils::TokenBucket::ObtainAll ( size_t count)
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: