token_bucket_algorithm 0.1.2 copy "token_bucket_algorithm: ^0.1.2" to clipboard
token_bucket_algorithm: ^0.1.2 copied to clipboard

Implementation of the token bucket algorithm for rate limiting e.g. network requests or user actions.

token_bucket_algorithm #

pub.dev likes popularity pub points license codecov

This Dart package provides rate limiting by using an implementation of the token bucket algorithm.

Simple usage #

final bucket = TokenBucket(
  size: 15,
  refillInterval: const Duration(seconds: 1),
  refillAmount: 10,
  storage: MemoryTokenBucketStorage(), // optionally change the way the state of the bucket is stored
);

if(bucket.consume()) {
  // Consumed 1 token successfully
}

if(bucket.consume(2)) {
  // Consumed 2 tokens successfully
}

If you want to store the tokens asynchronously in a custom storage, you can also use the AsyncTokenBucket.

final bucket = AsyncTokenBucket(
  size: 15,
  refillInterval: const Duration(seconds: 1),
  refillAmount: 10,
  storage: MyCustomAsyncTokenBucketStorage(),
);

if(await bucket.consume()) {
  // Consumed 1 token successfully
}
6
likes
160
points
297
downloads

Documentation

API reference

Publisher

verified publishersplashbyte.dev

Weekly Downloads

Implementation of the token bucket algorithm for rate limiting e.g. network requests or user actions.

Repository (GitHub)
View/report issues

Topics

#rate-limit #token-bucket #algorithm #security

License

BSD-3-Clause (license)

Dependencies

clock

More

Packages that depend on token_bucket_algorithm