1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! # touch_ratelimit
//!
//! A composable, extensible rate limiting crate for Rust.
//!
//! This crate provides building blocks for implementing rate limiting
//! in Rust applications using a clean separation of concerns:
//!
//! - **Algorithms** define *how* rate limiting works (e.g. token bucket).
//! - **Stores** define *where* rate limiting state is kept.
//! - **Middleware** defines *how* rate limiting is applied to requests.
//! - **Adapters** integrate rate limiting with web frameworks (e.g. Axum).
//!
//! The design is framework-agnostic and allows different algorithms,
//! storage backends, and adapters to be combined without modifying
//! core logic.
//!
//! ---
//!
//! ## Core concepts
//!
//! ### RateLimiter
//! A [`bucket::RateLimiter`] represents the rate limiting logic for
//! **a single identity** (for example, one user or one IP address).
//!
//! ### RateLimitStore
//! A [`storage::RateLimitStore`] maps request keys to individual
//! `RateLimiter` instances and handles concurrency.
//!
//! ### Middleware
//! The [`middleware`] module provides Tower middleware that enforces
//! rate limits before forwarding requests.
//!
//! ---
//!
//! ## Storage behavior
//!
//! The default in-memory store keeps all rate limiting state inside
//! the application's memory:
//!
//! - State is **lost on restart**
//! - State is **not shared across processes**
//!
//! This is suitable for single-instance deployments and development.
//! Distributed stores (e.g. Redis) can be added without changing
//! middleware or algorithms.
//!
//! ---
//!
//! ## Example: Axum
//!
//! ```rust,ignore
//! use axum::{routing::get, Router};
//! use std::sync::Arc;
//! use touch_ratelimit::{
//! adapters::axum::axum_rate_limit_layer,
//! storage::in_memory::InMemoryStore,
//! bucket::token_bucket::TokenBucket,
//! };
//!
//! let store = Arc::new(InMemoryStore::new(|| TokenBucket::new(10.0, 1.0)));
//!
//! let _ = Router::new()
//! .route("/", get(|| async { "hello" }))
//! .layer(axum_rate_limit_layer(store));
//! ```
//!
//! ---
//!
//! ## Roadmap
//!
//! Planned future additions include:
//!
//! - Additional rate limiting algorithms
//! - Distributed storage backends (e.g. Redis)
//! - Administrative inspection interfaces
//! - Optional CLI tooling
//!
//! ---
//!
//! ## Non-goals
//!
//! This crate does **not**:
//!
//! - Host infrastructure (e.g. Redis)
//! - Provide a global service
//! - Automatically persist rate limiting state
//!
//! Infrastructure is always owned and configured by the user.