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
//! Library which implements the core
//! [GCRA](https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm) functionality in rust.
//!
//! # Features
//! - `rate-limiter` a LRU + expiring rate limiter. Implements `Send + Sync` so
//! can be used asynchronously.
//!
//! # Usage
//!
//! ```rust
//! use gcra::{GcraState, RateLimit};
//!
//! fn check_rate_limit() {
//! const LIMIT: u32 = 1;
//! // Create a rate limit that allows `1/1s`
//! let rate_limit = RateLimit::per_sec(LIMIT);
//!
//! let mut user_state = GcraState::default();
//! assert!(user_state.check_and_modify(&rate_limit, 1).is_ok());
//! assert!(
//! user_state.check_and_modify(&rate_limit, 1).is_err(),
//! "We should be over the limit now"
//! );
//! }
//! ```
//!
//! ## With `rate-limiter`
//!
//! ```rust
//! use std::sync::Arc;
//! use gcra::{GcraError, RateLimit, RateLimiter};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), GcraError> {
//! let rate_limit = RateLimit::per_sec(2);
//! let rate_limiter = Arc::new(RateLimiter::new(4));
//!
//! rate_limiter.check("key", &rate_limit, 1).await?;
//! rate_limiter.check("key", &rate_limit, 1).await?;
//!
//! match rate_limiter.check("key", &rate_limit, 1).await {
//! Err(GcraError::DeniedUntil { next_allowed_at }) => {
//! print!("Denied: Request next at {:?}", next_allowed_at);
//! Ok(())
//! }
//! unexpected => panic!("Opps something went wrong! {:?}", unexpected),
//! }
//! }
//! ```
pub use crate;
pub use crateRateLimit;
pub use crateRateLimitGuard;
pub use crate;