Module rate_limiter

Module rate_limiter 

Source
Expand description

Rate limiting implementation for WiseGate.

Provides per-IP rate limiting using a sliding window algorithm with automatic cleanup of expired entries to prevent memory exhaustion.

§Algorithm

Uses a simple sliding window approach:

  • Each IP has a counter and a timestamp of the last request
  • If the window has expired, the counter resets
  • If under the limit, the counter increments and the request is allowed
  • If over the limit, the request is denied

§Memory Management

To prevent memory exhaustion from tracking many unique IPs, the rate limiter performs automatic cleanup when:

  • Entry count exceeds the configured threshold
  • Minimum interval since last cleanup has passed

§Thread Safety

Uses tokio::sync::Mutex for async-friendly locking that won’t block the Tokio thread pool.

§Example

use wisegate_core::{rate_limiter, RateLimiter};

let limiter = RateLimiter::new();

if rate_limiter::check_rate_limit(&limiter, "192.168.1.1", &config).await {
    // Request allowed
} else {
    // Rate limit exceeded
}

Functions§

check_rate_limit
Checks if a request from the given IP should be allowed based on rate limits.