Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::token_bucket_limiter Class Reference

Token bucket rate limiter implementation. More...

#include <resource_manager.h>

Inheritance diagram for kcenon::monitoring::token_bucket_limiter:
Inheritance graph
Collaboration diagram for kcenon::monitoring::token_bucket_limiter:
Collaboration graph

Public Types

using clock = std::chrono::steady_clock
 

Public Member Functions

 token_bucket_limiter (const std::string &name, double rate, size_t capacity, throttling_strategy=throttling_strategy::reject)
 
bool try_acquire (size_t count=1) override
 Try to acquire tokens.
 
const std::string & get_name () const override
 Get the name of this rate limiter.
 
- Public Member Functions inherited from kcenon::monitoring::rate_limiter
virtual ~rate_limiter ()=default
 
template<typename Func >
auto execute (Func &&func) -> decltype(func())
 Execute a function with rate limiting.
 

Private Member Functions

void refill ()
 

Private Attributes

std::string name_
 
double rate_
 
size_t capacity_
 
double tokens_
 
clock::time_point last_refill_
 
std::mutex mutex_
 

Detailed Description

Token bucket rate limiter implementation.

Uses the token bucket algorithm for rate limiting. Tokens are added at a fixed rate and consumed by requests.

Definition at line 215 of file resource_manager.h.

Member Typedef Documentation

◆ clock

using kcenon::monitoring::token_bucket_limiter::clock = std::chrono::steady_clock

Definition at line 217 of file resource_manager.h.

Constructor & Destructor Documentation

◆ token_bucket_limiter()

kcenon::monitoring::token_bucket_limiter::token_bucket_limiter ( const std::string & name,
double rate,
size_t capacity,
throttling_strategy = throttling_strategy::reject )
inline

Definition at line 219 of file resource_manager.h.

Member Function Documentation

◆ get_name()

const std::string & kcenon::monitoring::token_bucket_limiter::get_name ( ) const
inlineoverridevirtual

Get the name of this rate limiter.

Implements kcenon::monitoring::rate_limiter.

Definition at line 238 of file resource_manager.h.

238 {
239 return name_;
240 }

References name_.

◆ refill()

void kcenon::monitoring::token_bucket_limiter::refill ( )
inlineprivate

Definition at line 243 of file resource_manager.h.

243 {
244 auto now = clock::now();
245 auto elapsed = std::chrono::duration<double>(now - last_refill_).count();
246 tokens_ = std::min(static_cast<double>(capacity_), tokens_ + elapsed * rate_);
247 last_refill_ = now;
248 }

References capacity_, last_refill_, rate_, and tokens_.

Referenced by try_acquire().

Here is the caller graph for this function:

◆ try_acquire()

bool kcenon::monitoring::token_bucket_limiter::try_acquire ( size_t count = 1)
inlineoverridevirtual

Try to acquire tokens.

Parameters
countNumber of tokens to acquire
Returns
true if tokens were acquired

Implements kcenon::monitoring::rate_limiter.

Definition at line 227 of file resource_manager.h.

227 {
228 std::lock_guard<std::mutex> lock(mutex_);
229 refill();
230
231 if (tokens_ >= static_cast<double>(count)) {
232 tokens_ -= static_cast<double>(count);
233 return true;
234 }
235 return false;
236 }

References mutex_, refill(), and tokens_.

Here is the call graph for this function:

Member Data Documentation

◆ capacity_

size_t kcenon::monitoring::token_bucket_limiter::capacity_
private

Definition at line 252 of file resource_manager.h.

Referenced by refill().

◆ last_refill_

clock::time_point kcenon::monitoring::token_bucket_limiter::last_refill_
private

Definition at line 254 of file resource_manager.h.

Referenced by refill().

◆ mutex_

std::mutex kcenon::monitoring::token_bucket_limiter::mutex_
mutableprivate

Definition at line 255 of file resource_manager.h.

Referenced by try_acquire().

◆ name_

std::string kcenon::monitoring::token_bucket_limiter::name_
private

Definition at line 250 of file resource_manager.h.

Referenced by get_name().

◆ rate_

double kcenon::monitoring::token_bucket_limiter::rate_
private

Definition at line 251 of file resource_manager.h.

Referenced by refill().

◆ tokens_

double kcenon::monitoring::token_bucket_limiter::tokens_
private

Definition at line 253 of file resource_manager.h.

Referenced by refill(), and try_acquire().


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