Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

18 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›ก๏ธ SurgeShield (surgeshield)

A high-performance, asynchronous, lock-free rate limiter library, Redis distributed store, Prometheus exporter, and Tower/Axum middleware built in Rust.

Rust 2021 License: MIT Build Status Platform: Cross--Platform


๐Ÿ“Œ Overview

SurgeShield is an invisible, production-ready rate limiting engine, distributed cluster store, and observability stack designed for high-concurrency web services, microservices, and API gateways in Rust. It protects web applications against denial-of-service (DoS) attacks, brute-force exploits, and sudden traffic spikes without introducing garbage collection delays or global locks.

By combining DashMap lock-free in-memory storage, Redis atomic Lua distributed storage, Prometheus OpenMetrics telemetry, an embedded live visual Web Dashboard, and Tokio's async runtime, SurgeShield delivers sub-millisecond rate limit decisions and real-time cluster observability.


โœจ Key Features

  • ๐Ÿš€ High Throughput & Lock-Free State: Built on DashMap for concurrent, non-blocking evaluation across all CPU cores.
  • ๐ŸŒ Distributed Redis Storage (RedisStore): Share atomic rate limit quotas across multiple cloud server nodes or Kubernetes pods using Redis and atomic Lua scripting.
  • ๐Ÿงฎ Dual Rate Limiting Engines:
    • Token Bucket Algorithm: Perfect for bursty traffic with smooth floating-point token replenishment.
    • Sliding Window Counter: Eliminates window-boundary burst exploits using weighted window estimation.
  • ๐Ÿ–ฅ๏ธ Embedded Live Telemetry Dashboard (GET /dashboard): Zero-dependency, single-page dark-mode Web UI featuring real-time Chart.js graphs showing live RPS, allowed/blocked traffic breakdown, and evaluation latency.
  • ๐Ÿ“Š Prometheus Metrics Exporter (GET /metrics): Exposes standard OpenMetrics format:
    • surgeshield_requests_allowed_total (counter)
    • surgeshield_requests_blocked_total (counter for 429 status)
    • surgeshield_active_keys (gauge)
    • surgeshield_evaluation_duration_seconds (histogram)
  • ๐Ÿ“‰ 1-Click Grafana Template: Pre-built dashboards/grafana_surgeshield.json dashboard configuration ready for instant Grafana import.
  • ๐Ÿ”‘ Flexible Key Extraction Strategies: Client IP (X-Forwarded-For, X-Real-IP), HTTP Headers (Authorization, X-API-Key), Route URI Path, and Composite keys.
  • ๐Ÿงน Automated Background TTL Eviction: Spawns a background worker (tokio::spawn) to sweep and purge inactive client states, preventing OOM memory leaks.
  • ๐ŸŒ Seamless Tower / Axum Middleware: Integrates natively as a Tower::Layer in Axum, Hyper, or any Tower-compatible service stack.
  • ๐Ÿ“œ Standards-Compliant HTTP Headers: Automatically injects X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, and Retry-After headers.

๐Ÿ—๏ธ System Architecture

graph TD
    Client[๐Ÿ“ฑ Client Request] --> MW[๐Ÿ›ก๏ธ SurgeShield Middleware Layer]
    MW --> Metrics[๐Ÿ“Š Record Prometheus Telemetry]
    MW --> KeyExt[๐Ÿ”‘ Key Extractor]
    
    KeyExt -->|Single-Instance Node| MemStore[๐Ÿ’พ MemoryStore / DashMap]
    KeyExt -->|Multi-Node Cluster| RedisStore[โšก RedisStore / Atomic Lua]
    
    MemStore --> Engine{๐Ÿงฎ Rate Limit Engine}
    RedisStore --> Engine
    
    Engine -->|Quota Available| Pass[โœ… Allow Request - 200 OK]
    Engine -->|Quota Exceeded| Reject[โ›” Block Request - 429 Rate Limited]

    Pass --> AppRoute[๐Ÿš€ Application Route Handler]
    Reject --> Client

    Prometheus[๐Ÿ”ฅ Prometheus Server] -->|Scrapes GET /metrics| MetricsEndpoint[๐Ÿ“ˆ Prometheus Endpoint /metrics]
    Browser[๐ŸŒ Web Browser] -->|Views GET /dashboard| UI[๐Ÿ–ฅ๏ธ Embedded Telemetry Web UI]

    classDef clientStyle fill:#2563eb,stroke:#1d4ed8,stroke-width:2px,color:#ffffff;
    classDef mwStyle fill:#7c3aed,stroke:#6d28d9,stroke-width:2px,color:#ffffff;
    classDef storeStyle fill:#0284c7,stroke:#0369a1,stroke-width:2px,color:#ffffff;
    classDef engineStyle fill:#d97706,stroke:#b45309,stroke-width:2px,color:#ffffff;
    classDef passStyle fill:#059669,stroke:#047857,stroke-width:2px,color:#ffffff;
    classDef rejectStyle fill:#dc2626,stroke:#b91c1c,stroke-width:2px,color:#ffffff;
    classDef obsStyle fill:#0891b2,stroke:#0e7490,stroke-width:2px,color:#ffffff;

    class Client,Browser clientStyle;
    class MW,KeyExt mwStyle;
    class MemStore,RedisStore storeStyle;
    class Engine engineStyle;
    class Pass,AppRoute passStyle;
    class Reject rejectStyle;
    class Metrics,Prometheus,MetricsEndpoint,UI obsStyle;
Loading

๐Ÿš€ Quick Start

1. In-Memory Rate Limiting

use axum::{routing::get, Json, Router};
use rust_rate_limiter::{
    config::KeyExtractor,
    init_prometheus,
    middleware::RateLimiterLayer,
    render_dashboard,
    store::MemoryStore,
};
use serde_json::{json, Value};

#[tokio::main]
async fn main() {
    let prometheus_handle = init_prometheus().unwrap();

    // In-memory store: 5 request burst capacity, refills 1 token/sec
    let store = MemoryStore::new_token_bucket(5, 1.0);
    let rate_limiter = RateLimiterLayer::new(store, KeyExtractor::ClientIp);

    let app = Router::new()
        .route("/api/data", get(|| async { Json(json!({"status": "success"})) }))
        .layer(rate_limiter)
        .route("/metrics", get(move || async move { prometheus_handle.render() }))
        .route("/dashboard", get(render_dashboard));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

2. Multi-Node Cluster Rate Limiting (RedisStore)

For Kubernetes pods or load-balanced cloud nodes, use RedisStore to share the exact same rate limit quota across all instances:

use rust_rate_limiter::{RedisStore, RateLimiterLayer, KeyExtractor};
use axum::{Router, routing::get};

#[tokio::main]
async fn main() {
    // Distributed Redis Store: 100 requests burst capacity, refills 10 tokens/sec
    let redis_store = RedisStore::new("redis://127.0.0.1:6379/", 100, 10.0)
        .await
        .expect("Failed to connect to Redis cluster");

    let rate_limiter = RateLimiterLayer::new(redis_store, KeyExtractor::ClientIp);

    let app = Router::new()
        .route("/api/v1/resource", get(|| async { "Cluster protected resource" }))
        .layer(rate_limiter);

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

๐Ÿ–ฅ๏ธ Live Embedded Telemetry Dashboard (GET /dashboard)

Open http://localhost:3000/dashboard in any web browser to view the real-time visual UI:

  • Live Request Counters: Real-time counter for Allowed (200 OK) vs Blocked (429 Rate Limited) requests.
  • Interactive Chart.js Graph: Live line chart plotting throughput trends per second.
  • Traffic Breakdown: Doughnut chart showing the percentage of blocked traffic.
  • Evaluation Latency: Live microsecond calculation speed metric.

๐Ÿ“Š Prometheus & Grafana Integration

1. Prometheus Scraping (GET /metrics)

Exposes standard metrics format:

# HELP surgeshield_requests_allowed_total Total number of allowed requests
# TYPE surgeshield_requests_allowed_total counter
surgeshield_requests_allowed_total{key_id="127.0.0.1"} 42

# HELP surgeshield_requests_blocked_total Total number of blocked 429 requests
# TYPE surgeshield_requests_blocked_total counter
surgeshield_requests_blocked_total{key_id="127.0.0.1"} 8

# HELP surgeshield_evaluation_duration_seconds Rate limit evaluation latency in seconds
# TYPE surgeshield_evaluation_duration_seconds summary

2. 1-Click Grafana Dashboard Import

Import dashboards/grafana_surgeshield.json directly into Grafana to get instant visual panels for Allowed vs Blocked RPS, Active Keys Gauge, and Traffic Flow.


๐Ÿงช Testing

Execute the test suite covering unit math, 100-thread concurrency, and Axum middleware integration:

cargo test

Run the live example server:

cargo run --example axum_server

๐Ÿ“ Repository Structure

D:\Projects\rust_rate_limiter/
โ”œโ”€โ”€ Cargo.toml               # Project manifest & dependencies
โ”œโ”€โ”€ README.md                # Project documentation
โ”œโ”€โ”€ .gitignore               # Git build artifact exclusions
โ”œโ”€โ”€ .vscode/
โ”‚   โ””โ”€โ”€ settings.json        # VS Code workspace exclusions
โ”œโ”€โ”€ dashboards/
โ”‚   โ””โ”€โ”€ grafana_surgeshield.json # 1-Click Grafana dashboard template
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ lib.rs               # Main library entry point & exports
โ”‚   โ”œโ”€โ”€ error.rs             # Custom Error types and Result alias
โ”‚   โ”œโ”€โ”€ config.rs            # KeyExtractor enum & configuration rules
โ”‚   โ”œโ”€โ”€ metrics/             # Prometheus telemetry exporter & counters
โ”‚   โ”‚   โ””โ”€โ”€ mod.rs
โ”‚   โ”œโ”€โ”€ dashboard/           # Embedded HTML/JS visual web dashboard
โ”‚   โ”‚   โ””โ”€โ”€ mod.rs
โ”‚   โ”œโ”€โ”€ engine/              # Core rate limiter algorithm implementations
โ”‚   โ”‚   โ”œโ”€โ”€ mod.rs           # Engine traits & decision outcome types
โ”‚   โ”‚   โ”œโ”€โ”€ token_bucket.rs  # Token Bucket algorithm
โ”‚   โ”‚   โ””โ”€โ”€ sliding_window.rs# Sliding Window Counter algorithm
โ”‚   โ”œโ”€โ”€ store/               # Storage layer abstractions
โ”‚   โ”‚   โ”œโ”€โ”€ mod.rs           # RateLimitStore async trait
โ”‚   โ”‚   โ”œโ”€โ”€ memory.rs        # DashMap in-memory store + background TTL worker
โ”‚   โ”‚   โ””โ”€โ”€ redis.rs         # Redis atomic Lua distributed cluster store
โ”‚   โ””โ”€โ”€ middleware/          # Tower & Axum middleware integration
โ”‚       โ”œโ”€โ”€ mod.rs           # Tower Layer & Service implementations
โ”‚       โ””โ”€โ”€ headers.rs       # HTTP header injection logic
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ axum_server.rs       # Complete, runnable Axum web server with /metrics & /dashboard
โ””โ”€โ”€ tests/
    โ”œโ”€โ”€ engine_tests.rs      # Unit tests for algorithms & refill logic
    โ”œโ”€โ”€ concurrency_tests.rs # 100-thread multi-threaded stress tests
    โ””โ”€โ”€ middleware_tests.rs  # Axum HTTP integration & header tests

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more information.

Crafted with โค๏ธ and ๐Ÿฆ€ Rust by 8ernity

About

High-performance async Rust rate limiter & DDoS defense middleware featuring Redis cluster support, automatic IP blacklisting (The Jail), hot-reloading TOML configs, and a real-time liquid glass telemetry dashboard.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages