Base module

RateLimiter

Token bucket rate limiter for throttling message send frequency.

RateLimiter

#include <icy/ratelimiter.h>

Token bucket rate limiter for throttling message send frequency.

Public Attributes

ReturnNameDescription
doublerateHow many messages.
doublesecondsOver how many seconds.
doubleallowanceRemaining send allowance.

rate

double rate

How many messages.


seconds

double seconds

Over how many seconds.


allowance

double allowance

Remaining send allowance.

Public Methods

ReturnNameDescription
RateLimiter inlineConstructs a token bucket limiter.
boolcanSend inlineReturns true if a message may be sent without exceeding the rate limit. Replenishes the token bucket based on elapsed time since the last check, then consumes one token. Returns false if the bucket is empty.

RateLimiter

inline

inline RateLimiter(double rate, double seconds)

Constructs a token bucket limiter.

Parameters

  • rate Maximum number of messages permitted in the window.

  • seconds Duration of the replenishment window in seconds.


canSend

inline

inline bool canSend()

Returns true if a message may be sent without exceeding the rate limit. Replenishes the token bucket based on elapsed time since the last check, then consumes one token. Returns false if the bucket is empty.

Returns

true if sending is allowed, false if the rate limit is exceeded.

Private Attributes

ReturnNameDescription
std::chrono::steady_clock::time_point_lastCheck
bool_started

_lastCheck

std::chrono::steady_clock::time_point _lastCheck

_started

bool _started