Thread‐safe (atomic) token bucket implementation.

Synopsis

Declared in <folly/TokenBucket.h>

template<typename Policy = TokenBucketPolicyDefault>
class BasicDynamicTokenBucket;

Description

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. Failure to allocate tokens implies resource is unavailable and caller needs to implement its own retry mechanism. For simple cases where caller is okay with a FIFO starvation‐free scheduling behavior, there are also APIs to 'borrow' from the future effectively assigning a start time to the caller when it should proceed with using the resource. It is also possible to 'return' previously allocated tokens to make them available to other users. Returns in excess of burstSize are considered expired and will not be available to later callers.

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.

Member Functions

Name

Description

BasicDynamicTokenBucket [constructor]

Constructors

operator=

Copy assignment operator.

available

Returns the tokens available at specified time (zero if in debt).

balance

Returns the token balance at specified time (negative if bucket in debt).

consume

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.

consumeOrDrain

Similar to consume, but always consumes some number of tokens. If the bucket contains enough tokens ‐ consumes toConsume tokens. Otherwise the bucket is drained.

consumeWithBorrowAndWait

Convenience wrapper around non‐blocking borrow to sleep inline until reservation is valid.

consumeWithBorrowNonBlocking

Like consumeOrDrain but the call will always satisfy the asked for count. It does so by borrowing tokens from the future if the currently available count isn't sufficient.

reset

Re‐initialize token bucket.

returnTokens

Return extra tokens back to the bucket.

Static Member Functions

Name

Description

defaultClockNow

Returns the current time in seconds since Epoch.

Template Parameters

Name

Description

Policy

A policy.

Created with MrDocs