Module rate_limiter

Module rate_limiter 

Source
Expand description

Copyright © 2025-2026 Wenze Wei. All Rights Reserved.

This file is part of DMSC. The DMSC project belongs to the Dunimd Team.

Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

§Rate Limiter Module

This module provides rate limiting functionality for the DMSC gateway, allowing for controlling the rate of requests from clients to prevent abuse and ensure fair usage.

§Key Components

  • DMSCRateLimitConfig: Configuration for rate limiting behavior
  • DMSCRateLimiter: Token bucket based rate limiter implementation
  • DMSCSlidingWindowRateLimiter: Sliding window based rate limiter for fine-grained control
  • DMSCRateLimitStats: Metrics for monitoring rate limiter performance

§Design Principles

  1. Token Bucket Algorithm: Implements the token bucket algorithm for smooth rate limiting
  2. Sliding Window: Provides a sliding window implementation for more precise control
  3. Thread Safe: Uses Arc and RwLock for safe operation in multi-threaded environments
  4. Configurable: Allows fine-tuning of requests per second, burst size, and window duration
  5. Metrics Collection: Tracks and reports rate limiter statistics
  6. Async Compatibility: Built with async/await patterns for modern Rust applications
  7. Burst Support: Allows for temporary bursts of requests beyond the steady rate
  8. Key-Based Limiting: Supports rate limiting by client IP or custom keys

§Usage

use dmsc::prelude::*;
 
async fn example() {
    // Create a rate limiter with default configuration
    let mut limiter = DMSCRateLimiter::new(DMSCRateLimitConfig::default());
     
    // Check if a request should be allowed
    let client_ip = "192.168.1.1";
    if limiter.check_rate_limit(client_ip, 1).await {
        println!("Request allowed");
    } else {
        println!("Request rate limited");
    }
     
    // Get rate limit stats for a client
    if let Some(stats) = limiter.get_stats(client_ip).await {
        println!("Current tokens: {}, Total requests: {}", 
            stats.current_tokens, stats.total_requests);
    }
     
    // Create a sliding window rate limiter
    let sliding_limiter = DMSCSlidingWindowRateLimiter::new(100, 60);
    if sliding_limiter.allow_request().await {
        println!("Sliding window request allowed");
    }
}

Structs§

DMSCRateLimitConfig
Configuration for rate limiting behavior.
DMSCRateLimitStats
Statistics for rate limiting monitoring.
DMSCRateLimiter
Token bucket based rate limiter implementation.
DMSCSlidingWindowRateLimiter
Sliding window rate limiter for fine-grained control.