-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathrate_limit.rs
More file actions
57 lines (49 loc) · 1.72 KB
/
Copy pathrate_limit.rs
File metadata and controls
57 lines (49 loc) · 1.72 KB
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
//! A `governor`-based rate limiter builder.
//!
//! **Not currently wired into the service.** [`build_rate_limiter`] is
//! defined but never called from `main.rs` or the Axum router - see the
//! "Known issues" section of `contract/README.md`. `RATE_LIMIT_PER_SECOND`
//! / `RATE_LIMIT_BURST` are parsed by [`crate::config::AppConfig`] but not
//! currently enforced anywhere.
use governor::{Quota, RateLimiter};
use std::num::NonZeroU32;
pub type DefaultRateLimiter = RateLimiter<
governor::state::NotKeyed,
governor::state::InMemoryState,
governor::clock::DefaultClock,
>;
/// Build an in-memory, non-keyed rate limiter allowing `per_second`
/// sustained requests with a burst capacity of `burst`.
///
/// Panics if either argument is zero (`NonZeroU32::new(...).unwrap()`).
pub fn build_rate_limiter(per_second: u32, burst: u32) -> DefaultRateLimiter {
let quota = Quota::per_second(NonZeroU32::new(per_second).unwrap())
.allow_burst(NonZeroU32::new(burst).unwrap());
RateLimiter::direct(quota)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_rate_limiter_does_not_panic() {
let _limiter = build_rate_limiter(10, 10);
}
#[test]
fn test_rate_limiter_allows_first_request() {
let limiter = build_rate_limiter(10, 10);
assert!(limiter.check().is_ok());
}
#[test]
fn test_rate_limiter_allows_burst() {
let limiter = build_rate_limiter(10, 5);
for _ in 0..5 {
assert!(limiter.check().is_ok());
}
}
#[test]
fn test_build_rate_limiter_various_burst_values() {
let _l1 = build_rate_limiter(1, 1);
let _l2 = build_rate_limiter(100, 200);
let _l3 = build_rate_limiter(50, 50);
}
}