#token-bucket #limiter #bucket #redis #token #rate

bucket-limiter

Token bucket like limiter library based on redis storage

3 unstable releases

Uses old Rust 2015

0.2.0 Nov 4, 2017
0.1.1 Nov 3, 2017
0.1.0 Nov 3, 2017

#13 in #token-bucket

MIT license

14KB
329 lines

bucket-limiter-rs

Token bucket like limiter library based on redis storage Python version

Example (use case)

Assume you have a API service, and want to limit user request rate like:

  • 10,000 requests/day
  • 600 requests/1-hour
  • 10 requests/10-seconds

You can do this:

let limiter = RedisLimiter::default();
let key = format!("{}:{}", request.endpoint, request.method);
// Once one request has come
if (limiter.consume_one(key, 10, 10) &&
    limiter.consume_one(key, 3600, 600) &&
    limiter.consume_one(key, 24*3600, 10000)) {
    // Process the request
} else {
    // Reject the request
}

Dependencies

~3.5MB
~83K SLoC