proxygen
|
#include <TokenBucket.h>
Public Member Functions | |
BasicDynamicTokenBucket (double zeroTime=0) noexcept | |
BasicDynamicTokenBucket (const BasicDynamicTokenBucket &other) noexcept | |
BasicDynamicTokenBucket & | operator= (const BasicDynamicTokenBucket &other) noexcept |
void | reset (double zeroTime=0) noexcept |
bool | consume (double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow()) |
double | consumeOrDrain (double toConsume, double rate, double burstSize, double nowInSeconds=defaultClockNow()) |
double | available (double rate, double burstSize, double nowInSeconds=defaultClockNow()) const noexcept |
Static Public Member Functions | |
static double | defaultClockNow () noexcept |
Private Member Functions | |
template<typename TCallback > | |
bool | consumeImpl (double rate, double burstSize, double nowInSeconds, const TCallback &callback) |
Private Attributes | |
std::atomic< double > | zeroTime_ |
Thread-safe (atomic) token bucket implementation.
A token bucket (http://en.wikipedia.org/wiki/Token_bucket) models a stream of events with an average rate and some amount of burstiness. The canonical example is a packet switched network: the network can accept some number of bytes per second and the bytes come in finite packets (bursts). A token bucket stores up to a fixed number of tokens (the burst size). Some number of tokens are removed when an event occurs. The tokens are replenished at a fixed rate.
This implementation records the last time it was updated. This allows the token bucket to add tokens "just in time" when tokens are requested.
The "dynamic" base variant allows the token generation rate and maximum burst size to change with every token consumption.
Clock | Clock type, must be steady i.e. monotonic. |
Definition at line 48 of file TokenBucket.h.
|
inlineexplicitnoexcept |
Constructor.
zeroTime | Initial time at which to consider the token bucket starting to fill. Defaults to 0, so by default token buckets are "full" after construction. |
Definition at line 59 of file TokenBucket.h.
|
inlinenoexcept |
Copy constructor.
Thread-safe. (Copy constructors of derived classes may not be thread-safe however.)
Definition at line 68 of file TokenBucket.h.
|
inlinenoexcept |
Returns the number of tokens currently available.
Thread-safe (but returned value may immediately be outdated).
Definition at line 182 of file TokenBucket.h.
References min, and folly::BasicDynamicTokenBucket< Clock >::zeroTime_.
Referenced by folly::BasicTokenBucket< Clock >::reset(), and TEST().
|
inline |
Attempts to consume some number of tokens. Tokens are first added to the bucket based on the time elapsed since the last attempt to consume tokens. Note: Attempts to consume more tokens than the burst size will always fail.
Thread-safe.
toConsume | The number of tokens to consume. |
rate | Number of tokens to generate per second. |
burstSize | Maximum burst size. Must be greater than 0. |
nowInSeconds | Current time in seconds. Should be monotonically increasing from the nowInSeconds specified in this token bucket's constructor. |
Definition at line 121 of file TokenBucket.h.
References folly::BasicDynamicTokenBucket< Clock >::consumeImpl(), and tokens.
Referenced by TEST().
|
inlineprivate |
Definition at line 194 of file TokenBucket.h.
References min, tokens, UNLIKELY, and folly::BasicDynamicTokenBucket< Clock >::zeroTime_.
Referenced by folly::BasicDynamicTokenBucket< Clock >::consume(), and folly::BasicDynamicTokenBucket< Clock >::consumeOrDrain().
|
inline |
Similar to consume, but always consumes some number of tokens. If the bucket contains enough tokens - consumes toConsume tokens. Otherwise the bucket is drained.
Thread-safe.
toConsume | The number of tokens to consume. |
rate | Number of tokens to generate per second. |
burstSize | Maximum burst size. Must be greater than 0. |
nowInSeconds | Current time in seconds. Should be monotonically increasing from the nowInSeconds specified in this token bucket's constructor. |
Definition at line 154 of file TokenBucket.h.
References folly::BasicDynamicTokenBucket< Clock >::consumeImpl(), and tokens.
Referenced by TEST().
|
inlinestaticnoexcept |
|
inlinenoexcept |
Copy-assignment operator.
Warning: not thread safe for the object being assigned to (including self-assignment). Thread-safe for the other object.
Definition at line 77 of file TokenBucket.h.
References folly::BasicDynamicTokenBucket< Clock >::zeroTime_.
Referenced by folly::BasicTokenBucket< Clock >::BasicTokenBucket().
|
inlinenoexcept |
Re-initialize token bucket.
Thread-safe.
zeroTime | Initial time at which to consider the token bucket starting to fill. Defaults to 0, so by default token bucket is reset to "full". |
Definition at line 92 of file TokenBucket.h.
References folly::BasicDynamicTokenBucket< Clock >::zeroTime_.
|
private |