upstash-ratelimit 0.1.0

Upstash ratelimit compatible rate limiting library.
Documentation
  • Coverage
  • 20.59%
    7 out of 34 items documented0 out of 12 items with examples
  • Size
  • Source code size: 10.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 333.8 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 34s Average build duration of successful builds.
  • all releases: 34s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • liamkinne

Redis Backed Rate Limit

This library is inspired by and designed to be compatible with the Upstash Rate Limit library. This means you can have multiple JavaScript and Rust services operate on the same Redis keys and use the same rate limiting algorithms.

Getting Started

Add the dependency

cargo add upstash-ratelimit

Use upstash_ratelimit to

use upstash_ratelimit::{Limiter, RateLimit, Response};

let redis = redis::Client::open("redis://127.0.0.1/")?;

let ratelimit = RateLimit::builder()
    .redis(REDIS.clone())
    .limiter(Limiter::FixedWindow {
        tokens: 30,
        window: Duration::from_millis(100),
    })
    .build()?;

let response = ratelimit.limit("unique-id");

match result {
    Response::Success { .. } => println!("Allow!")
    Response::Failure { .. } => println!("Rate limited!")
}

Running Unit Tests

Before running cargo test, spin up a Redis instance on localhost with docker-compose up -d. It's a good idea to restart Redis between test runs.

Feature Support

  • Single region Redis
  • Multi-region Redis
  • Fixed window limiting
  • Sliding window limiting
  • Token bucket limiting
  • Cached fixed window limiting
  • Arbitrary key prefix
  • Ephemeral (in-memory) cache
  • Block until ready
  • HTTP Redis connection
    • This will require substantial effort and should probably reside in a separate library.
  • Analytics
    • This will require substantial efford and should probably reside in a separate library.