Crates.io | upstash-ratelimit |
lib.rs | upstash-ratelimit |
version | 0.1.0 |
source | src |
created_at | 2023-07-07 11:04:32.047249 |
updated_at | 2023-07-07 11:04:32.047249 |
description | Upstash ratelimit compatible rate limiting library. |
homepage | |
repository | |
max_upload_size | |
id | 910665 |
size | 10,473 |
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.
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!")
}
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.