Public Member Functions | Protected Attributes | List of all members
cras::TokenBucketLimiter Class Reference

Token bucket rate limiting algorithm. More...

#include <rate_limiter.h>

Inheritance diagram for cras::TokenBucketLimiter:
Inheritance graph
[legend]

Public Member Functions

void reset () override
 Reset the rate-limiter as if it were newly created with the same parameters. More...
 
bool shouldPublish (const ::ros::Time &stamp) override
 Call this function whenever a message is received. It tells whether the message has passed the rate-limiting and should be published, or whether it should be skipped. More...
 
 TokenBucketLimiter (const ::ros::Rate &rate, size_t bucketCapacity=2, double initialTokensAvailable=1.0)
 Create the rate-limiter limiting to the desired rate. More...
 
 TokenBucketLimiter (const ::ros::Duration &period, size_t bucketCapacity=2, double initialTokensAvailable=1.0)
 Create rate-limiter with rate corresponding to the given period. More...
 
- Public Member Functions inherited from cras::RateLimiter
 RateLimiter (const ::ros::Rate &rate)
 Create limiter with the given rate. More...
 
 RateLimiter (const ::ros::Duration &period)
 Create limiter with rate corresponding to the given period. More...
 
void setJumpBackTolerance (const ::ros::Duration &tolerance)
 Set the limit for telling between small and large backwards time jumps. Small jumps result in ignoring the messages, while a large jump results in a reset of the rate-limter. More...
 

Protected Attributes

size_t bucketCapacity
 Number of tokens that can fit into the bucket. This influences the maximum burst size. More...
 
double initialTokensAvailable
 The number of tokens that are initially in the buffer (and after reset). More...
 
::ros::Time lastCheckTime {0, 0}
 Stamp of the last incoming message. Zero at the beginning. More...
 
ros::Duration tokensAvailable
 The number of currently available tokens. This units of this number are actually not seconds, but Duration is used here to achieve higher decimal point accuracy. More...
 
- Protected Attributes inherited from cras::RateLimiter
::ros::Duration jumpBackTolerance {3, 0}
 Threshold for jump back detection. More...
 
::ros::Duration period
 The desired period between message (1/rate). More...
 
::ros::Rate rate
 The desired rate (1/period). More...
 

Additional Inherited Members

- Protected Member Functions inherited from cras::RateLimiter
bool jumpedBack (const ::ros::Time &stamp, const ::ros::Time &previousStamp) const
 Decide whether the newly coming message should be treated as a backwards jump in time. More...
 

Detailed Description

Token bucket rate limiting algorithm.

Note
Generally, it should be quite good at achieving the desired rate.
The limiter has a bucket of a given capacity. It is refilled over time by a constant number of tokens per second. To publish a message, there has to be at least one token in the bucket. If more tokens should be put in the bucket than is its capacity, they are ignored.
The bucket capacity basically specifies the size of the burst that can happen after some period of inactivity when tokens are just collected and not consumed.

Definition at line 103 of file rate_limiter.h.

Constructor & Destructor Documentation

◆ TokenBucketLimiter() [1/2]

cras::TokenBucketLimiter::TokenBucketLimiter ( const ::ros::Rate rate,
size_t  bucketCapacity = 2,
double  initialTokensAvailable = 1.0 
)
explicit

Create the rate-limiter limiting to the desired rate.

Parameters
[in]rateDesired rate.
[in]bucketCapacityCapacity of the bucket (in tokens).
[in]initialTokensAvailableNumber of tokens available in the bucket at the beginning. Set to 1 to always let the first packet through. This number should not be higher than bucketCapacity.

◆ TokenBucketLimiter() [2/2]

cras::TokenBucketLimiter::TokenBucketLimiter ( const ::ros::Duration period,
size_t  bucketCapacity = 2,
double  initialTokensAvailable = 1.0 
)
explicit

Create rate-limiter with rate corresponding to the given period.

Parameters
[in]periodAverage delay between two desired output messages.
[in]bucketCapacityCapacity of the bucket (in tokens).
[in]initialTokensAvailableNumber of tokens available in the bucket at the beginning. Set to 1 to always let the first packet through. This number should not be higher than bucketCapacity.

Member Function Documentation

◆ reset()

void cras::TokenBucketLimiter::reset ( )
overridevirtual

Reset the rate-limiter as if it were newly created with the same parameters.

Implements cras::RateLimiter.

◆ shouldPublish()

bool cras::TokenBucketLimiter::shouldPublish ( const ::ros::Time stamp)
overridevirtual

Call this function whenever a message is received. It tells whether the message has passed the rate-limiting and should be published, or whether it should be skipped.

Parameters
[in]stampTime when the message should be sent (usually not header.stamp!).
Returns
Whether to continue publishing the message.

Implements cras::RateLimiter.

Member Data Documentation

◆ bucketCapacity

size_t cras::TokenBucketLimiter::bucketCapacity
protected

Number of tokens that can fit into the bucket. This influences the maximum burst size.

Definition at line 135 of file rate_limiter.h.

◆ initialTokensAvailable

double cras::TokenBucketLimiter::initialTokensAvailable
protected

The number of tokens that are initially in the buffer (and after reset).

Definition at line 142 of file rate_limiter.h.

◆ lastCheckTime

::ros::Time cras::TokenBucketLimiter::lastCheckTime {0, 0}
protected

Stamp of the last incoming message. Zero at the beginning.

Definition at line 132 of file rate_limiter.h.

◆ tokensAvailable

ros::Duration cras::TokenBucketLimiter::tokensAvailable
protected

The number of currently available tokens. This units of this number are actually not seconds, but Duration is used here to achieve higher decimal point accuracy.

Definition at line 139 of file rate_limiter.h.


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


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:32:53