worker-ratelimit

This is a general purpose rate limiting library for Cloudflare Workers. It builds on top of Cloudflare Workers KV for storage.
This library is meant to compile to WebAssembly and execute on Cloudflare's serverless plattform. Please note, this is not an official Cloudflare project and implements rate-limiting non-atomically, on best-effort basis, meaning it may fail to count some actions happening in quick succession. Users may be able to go slightly above the configured limits, but once they are reached the limits are effective. Ratelimiting is implemented with a BTree-based sliding-window algorithm.
This library is meant to work with features available on free-tier. For more serious use definitely also consider the "Security > WAF > Rate limiting rules" settings.
Usage
Configure a RateLimiter struct containing your rules.
use RateLimiter;
Interact with your KV-backed ratelimits like this:
let ratelimits = setup_ratelimiter;
// Get the request ip address
let ip_addr = req.headers.get?.unwrap_or_else;
// Check the rate-limit thresholds (also providing the current time)
let Allow = ratelimits.check_kv.await? else ;
/* Perform the action */
do_something.await?;
// Increase the ratelimit counter (if needed)
if let Some = ticket
License
MIT OR Apache-2.0