Class TokenBucketLimiter

Inheritance Relationships

Base Type

Class Documentation

class TokenBucketLimiter : public cras::RateLimiter

Token bucket rate limiting algorithm.

Note

Generally, it should be quite good at achieving the desired rate.

Note

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.

Note

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.

Public Functions

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

Create the rate-limiter limiting to the desired rate.

Parameters:
  • rate[in] Desired rate.

  • bucketCapacity[in] Capacity of the bucket (in tokens).

  • initialTokensAvailable[in] Number 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.

explicit TokenBucketLimiter(const ::rclcpp::Clock::SharedPtr &clock, const ::rclcpp::Duration &period, size_t bucketCapacity = 2, double initialTokensAvailable = 1.0)

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

Parameters:
  • clock[in] The clock to use.

  • period[in] Average delay between two desired output messages.

  • bucketCapacity[in] Capacity of the bucket (in tokens).

  • initialTokensAvailable[in] Number 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.

virtual bool shouldPublish(const ::rclcpp::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.

Parameters:

stamp[in] Time when the message should be sent (usually not header.stamp!).

Returns:

Whether to continue publishing the message.

virtual void reset() override

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

Protected Attributes

::rclcpp::Time lastCheckTime = {0, 0}

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

size_t bucketCapacity

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

::rclcpp::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.

double initialTokensAvailable

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