Build Status License Documentation crates.io

GCRA: A basic implementation

Library which implements the core GCRA functionality in rust.

Features

Usage

```rust use gcra::{GcraState, RateLimit};

fn checkratelimit() { const LIMIT: u32 = 1; // Create a rate limit that allows 1/1s let ratelimit = RateLimit::persec(LIMIT);

let mut userstate = GcraState::default(); assert!(userstate.checkandmodify(&ratelimit, 1).isok()); assert!( userstate.checkandmodify(&ratelimit, 1).is_err(), "We should be over the limit now" ); } ```

With rate-limiter

```rust use gcra::{GcraError, RateLimit, RateLimiter, RateLimiterError};

[tokio::main]

async fn main() -> Result<(), RateLimiterError> { let ratelimit = RateLimit::persec(2); let mut rl = RateLimiter::new(4, 4);

rl.check("key", rate_limit.clone(), 1).await?;
rl.check("key", rate_limit.clone(), 1).await?;

match rl.check("key", rate_limit.clone(), 1).await {
    Err(RateLimiterError::GcraError(GcraError::DeniedUntil { next_allowed_at })) => {
        print!("Denied: Request next at {:?}", next_allowed_at);
        Ok(())
    }
    unexpected => panic!("Opps something went wrong! {:?}", unexpected),
}

} ```