|
Click
|
Token bucket rate limiter. More...
#include <click/tokenbucket.hh>
Public Types | |
| enum | { max_tokens = rate_type::max_tokens } |
| typedef P | parameter_type |
| The template parameter type. | |
| typedef TokenRateX< P > | rate_type |
| The token rate type. | |
| typedef TokenCounterX< rate_type > | counter_type |
| The token counter type. | |
| typedef rate_type::token_type | token_type |
| Unsigned type of token counts. | |
| typedef rate_type::time_point_type | time_point_type |
| Type of time ticks. | |
| typedef rate_type::ticks_type | ticks_type |
| Unsigned type of differences between time ticks. | |
Public Member Functions | |
| TokenBucketX () | |
| Construct an idle token bucket. | |
| TokenBucketX (bool unlimited) | |
| Construct an idle or unlimited token bucket. | |
| TokenBucketX (token_type rate, token_type capacity) | |
| Construct a token bucket representing rate. | |
| void | assign (bool unlimited=false) |
| Set the token bucket rate to idle or unlimited. | |
| void | assign (token_type rate, token_type capacity) |
| Set the token bucket rate and capacity. | |
| void | assign_adjust (token_type rate, token_type capacity) |
| Set the token bucket rate and capacity, preserving the token count. | |
| bool | unlimited () const |
| Return true iff the token rate is unlimited. | |
| bool | idle () const |
| Return true iff the token rate is idle. | |
| token_type | rate () const |
| Return the rate in tokens per period. | |
| token_type | capacity () const |
| Return the capacity in tokens. | |
| token_type | size () const |
| Return the number of tokens in the bucket. | |
| token_type | fraction () const |
| Return the bucket's fullness fraction. | |
| bool | empty () const |
| Test if the token bucket is completely empty. | |
| bool | full () const |
| Test if the token bucket is at full capacity. | |
| bool | contains (token_type t) const |
| Test if the token bucket has at least t tokens. | |
| bool | contains_fraction (token_type f) const |
| Test if the token bucket is above a fraction of its capacity. | |
| void | clear () |
| Clear the token bucket. | |
| void | set_full () |
| Fill the token bucket to capacity. | |
| void | set (token_type t) |
| Set the token bucket to contain t tokens. | |
| void | set_fraction (token_type f) |
| Set the token bucket to a fraction of its capacity. | |
| void | refill () |
| Refill the token bucket to time P::now(). | |
| void | refill (time_point_type time) |
| Refill the token bucket for time. | |
| template<typename U > | |
| void | refill (U time) |
| Refill the token bucket for time P::time_point(time). | |
| void | set_time_point (time_point_type time) |
| Set the token bucket's internal time point to time. | |
| time_point_type | time_point () |
| void | remove (token_type t) |
| Remove t tokens from the bucket. | |
| bool | remove_if (token_type t) |
| Remove t tokens from the bucket if it contains t tokens. | |
| void | remove_fraction (token_type f) |
| Remove a fullness fraction from the bucket. | |
| bool | remove_fraction_if (token_type f) |
| Remove a fullness fraction from the bucket if it is full enough. | |
| ticks_type | time_until_contains (token_type t) const |
| Return the number of ticks until contains(t). | |
| ticks_type | time_until_contains_fraction (ticks_type f) const |
| Return the number of ticks until contains_fraction(f). | |
Token bucket rate limiter.
The TokenBucketX class implements a token bucket rate limiter. It is implemented as a pair of TokenRateX and TokenCounterX.
Most users will be satisfied with the TokenBucket type, which is equal to TokenBucketX<TokenBucketUsecParameters<unsigned long long> >.
| typedef P TokenBucketX< P >::parameter_type |
The template parameter type.
| typedef TokenRateX<P> TokenBucketX< P >::rate_type |
The token rate type.
| typedef TokenCounterX<rate_type> TokenBucketX< P >::counter_type |
The token counter type.
| typedef rate_type::token_type TokenBucketX< P >::token_type |
Unsigned type of token counts.
| typedef rate_type::time_point_type TokenBucketX< P >::time_point_type |
Type of time ticks.
| typedef rate_type::ticks_type TokenBucketX< P >::ticks_type |
Unsigned type of differences between time ticks.
|
inline |
Construct an idle token bucket.
The initial time point is 0.
|
inlineexplicit |
Construct an idle or unlimited token bucket.
| unlimited | idle if false, unlimited if true |
The initial time point is 0.
|
inline |
Construct a token bucket representing rate.
| rate | refill rate in tokens per period |
| capacity | maximum token accumulation |
The initial time point is 0 and the token bucket is initially full (the initial token count equals capacity). The rate is idle if either rate or capacity is 0.
|
inline |
Set the token bucket rate to idle or unlimited.
| unlimited | idle if false, unlimited if true |
|
inline |
Set the token bucket rate and capacity.
| rate | refill rate in tokens per period |
| capacity | maximum token accumulation |
Sets the token bucket's rate to rate and capacity to capacity. If either rate or capacity is 0, the token bucket becomes idle. The time point is unchanged.
The ratio of tokens/burst is unchanged by the assignment, so the actual number of tokens could go up or down, depending on how the rate is changed.
|
inline |
|
inline |
Return true iff the token rate is unlimited.
|
inline |
Return true iff the token rate is idle.
|
inline |
Return the rate in tokens per period.
Returns max_tokens for unlimited rates. Imprecise computer arithmetic may cause the result to differ from the configured rate.
|
inline |
Return the capacity in tokens.
Returns max_tokens for unlimited rates. Imprecise computer arithmetic may cause the result to differ from the configured capacity.
|
inline |
Return the number of tokens in the bucket.
The return value is a lower bound on the number of tokens, since TokenBucketX keeps track of fractional tokens.
|
inline |
Return the bucket's fullness fraction.
The return value is a number between 0 and max_tokens, where max_tokens represents full capacity.
|
inline |
Test if the token bucket is completely empty.
|
inline |
Test if the token bucket is at full capacity.
|
inline |
Test if the token bucket has at least t tokens.
Returns true whenever t is zero or rate is unlimited. Returns false whenever t is greater than rate.capacity().
|
inline |
Test if the token bucket is above a fraction of its capacity.
| f | fullness fraction, where max_tokens is full capacity |
|
inline |
Clear the token bucket.
|
inline |
|
inline |
Set the token bucket to contain t tokens.
| t | number of tokens |
The result will never have more tokens than the associated capacity.
|
inline |
Set the token bucket to a fraction of its capacity.
| f | fullness fraction, where max_tokens is full capacity |
|
inline |
Refill the token bucket to time P::now().
There are three refill() methods, useful for different methods of measuring ticks. This method call parameter_type::now(), which returns the current time. Other methods use an explicit time point and a parameter_type::time(U) method.
|
inline |
Refill the token bucket for time.
Refill the token bucket for time P::time_point(time).
|
inline |
|
inline |
Remove t tokens from the bucket.
| t | number of tokens |
If the token bucket contains less than t tokens, the new token count is 0.
|
inline |
Remove t tokens from the bucket if it contains t tokens.
| t | number of tokens |
If the token bucket contains t or more tokens, calls remove(t) and returns true. If it contains less than t tokens, returns false without removing any tokens.
|
inline |
Remove a fullness fraction from the bucket.
| f | fullness fraction, where max_tokens is full capacity |
If the token counter is less than f full, the new token count is 0.
|
inline |
Remove a fullness fraction from the bucket if it is full enough.
| f | fullness fraction, where max_tokens is full capacity |
If fraction() is at least f, calls remove_fraction(f) and returns true. Otherwise, returns false without removing any tokens.
|
inline |
Return the number of ticks until contains(t).
Returns (ticks_type) -1 if passing time will never make contains(t) true.
|
inline |
Return the number of ticks until contains_fraction(f).
Returns (ticks_type) -1 if passing time will never make contains_fraction(f) true.