leaky-bucket
A token-based rate limiter based on the leaky bucket algorithm.
If the tokens are already available, the acquisition will be instant through a fast path, and the acquired number of tokens will be added to the bucket.
If the bucket overflows (i.e. goes over max), the task that tried to acquire the tokens will be suspended until the required number of tokens has been added.
Usage
Add the following to your Cargo.toml
:
= "0.9.0"
Example
If the project is built with the static
feature (default), you can use
LeakyBucket directly as long as you are inside a Tokio runtime, like so:
use LeakyBucket;
use ;
async
Note that only one coordinator instance will be lazily created, and it's associated with the runtime from within it was constructed. To use multiple runtimes you must use an explicit coordinator as documented in the next section.
Example using explicit coordinator
Leaky buckets require coordination. By default, this will happen through a static coordinator spawned through tokio::spawn at first use. If you want to spawn the coordinator yourself, you can do the following with LeakyBuckets:
use LeakyBuckets;
use ;
async
Note that since this coordinator uses timing facilities from tokio it has to
be spawned within a Tokio runtime with the time
feature enabled.
License: MIT/Apache-2.0