Unofficial Upstash rate limit SDK for Rust
A rate-limiting SDK built for the Rust ecosystem that uses in-memory data storage.
Inspiration
This rate limit SDK is inspired by the official TypeScript rate limit SDK created by Upstash team.
Setup
- To setup the ratelimiter, first create a client instance of Redis that can store the request counts for a given set window:
let connection_str = var.unwrap_or_else;
let Ok = open else ;
-
Create a client instance of the
RatelimitConfiguration
using the Redis client: -
Use the client configuration to create a new instance of any one of the three rate-limiting algorithms:
For example: Using the fixed window algorithm to limit 10 requests in 30 seconds of the window.
let client = new;
let ratelimit = new;
In the above client configuration, using the Ephemeral cache to avoid making Redis calls if the request is already blocked and adding a custom prefix string will override the default prefix string,
Use the ratelimit
instance to call the limit function in any request calls to rate limit your requests:
let limit_response = state.ratelimit.limit.await;
Custom rate
By default every algorithm consumes one token per request, but if you want rate-limit the requests based on the payload size or any other factor, you can do so by providing the rate value to the limit function call:
let limit_response = state.ratelimit.limit.await;
This will consume 10 tokens in one request.
Examples
Check the examples directory
Roadmap
-
Single Region (may have latency issues)
- Fixed window algorithm ✅
- Sliding window algorithm ✅
- Token bucket algorithm ✅
- Cached fixed window algorithm 🛠️
- Analytics 🛠️
- Forced timeout 🛠️
- Hard reset 🛠️
-
Multiple Region (no latency issues)
- Fixed window algorithm 🛠️
- Sliding window algorithm 🛠️