CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | List of all members
CppCommon::TokenBucket Class Reference

Token bucket rate limit algorithm. More...

#include <token_bucket.h>

Public Member Functions

 TokenBucket (uint64_t rate, uint64_t burst)
 Initialize the token bucket. More...
 
 TokenBucket (const TokenBucket &tb)
 
 TokenBucket (TokenBucket &&)=delete
 
 ~TokenBucket ()=default
 
TokenBucketoperator= (const TokenBucket &tb)
 
TokenBucketoperator= (TokenBucket &&)=delete
 
bool Consume (uint64_t tokens=1)
 Try to consume the given count of tokens. More...
 

Detailed Description

Token bucket rate limit algorithm.

Lock-free implementation of the token bucket rate limit algorithm.

Token bucket

The token bucket is an algorithm used in packet switched computer networks and telecommunications networks. It can be used to check that data transmissions, in the form of packets, conform to defined limits on bandwidth and burstiness (a measure of the unevenness or variations in the traffic flow).

Thread-safe.

https://en.wikipedia.org/wiki/Token_bucket

Examples
algorithms_token_bucket.cpp.

Definition at line 33 of file token_bucket.h.

Constructor & Destructor Documentation

◆ TokenBucket() [1/3]

CppCommon::TokenBucket::TokenBucket ( uint64_t  rate,
uint64_t  burst 
)
inline

Initialize the token bucket.

Initializes the token bucket to accumulate the given count of tokens per second, with a maximum of burst tokens.

Parameters
rate- Rate of tokens per second to accumulate in the token bucket
burst- Maximum of burst tokens in the token bucket

Definition at line 11 of file token_bucket.inl.

◆ TokenBucket() [2/3]

CppCommon::TokenBucket::TokenBucket ( const TokenBucket tb)
inline

Definition at line 19 of file token_bucket.inl.

◆ TokenBucket() [3/3]

CppCommon::TokenBucket::TokenBucket ( TokenBucket &&  )
delete

◆ ~TokenBucket()

CppCommon::TokenBucket::~TokenBucket ( )
default

Member Function Documentation

◆ Consume()

bool CppCommon::TokenBucket::Consume ( uint64_t  tokens = 1)

Try to consume the given count of tokens.

Parameters
tokens- Tokens to consume (default is 1)
Returns
'true' if all tokens were successfully consumed, 'false' if the token bucket is lack of required count of tokens
Examples
algorithms_token_bucket.cpp.

Definition at line 15 of file token_bucket.cpp.

◆ operator=() [1/2]

TokenBucket & CppCommon::TokenBucket::operator= ( const TokenBucket tb)
inline

Definition at line 26 of file token_bucket.inl.

◆ operator=() [2/2]

TokenBucket& CppCommon::TokenBucket::operator= ( TokenBucket &&  )
delete

The documentation for this class was generated from the following files: