Expand description
Custom rate limiting library with Redis and Axum support
This library provides middleware for API rate limiting and API key validation.
§Features
- Rate Limiting: Configurable rate limiting with Redis backend
- API Key Validation: Validate requests using x-api-key header
- Per-Key Rate Limits: Different rate limits per API key
- Extensible Design: Custom key stores and rate limiting strategies
- Redis Integration: Default Redis-based storage for keys and rate limits
- Axum Middleware: Ready-to-use middleware for Axum web framework
§Basic Usage
use barnacle_rs::{
create_api_key_layer, ApiKeyMiddlewareConfig, BarnacleConfig
};
#[cfg(feature = "redis")]
use barnacle_rs::{RedisApiKeyStore, RedisBarnacleStore, deadpool_redis};
use std::time::Duration;
// Create Redis stores (requires "redis" feature)
#[cfg(feature = "redis")]
let redis_pool = deadpool_redis::Config::from_url("redis://localhost")
.create_pool(Some(deadpool_redis::Runtime::Tokio1))?;
#[cfg(feature = "redis")]
let api_key_store = RedisApiKeyStore::new(redis_pool.clone());
#[cfg(feature = "redis")]
let rate_limit_store = RedisBarnacleStore::new(redis_pool);
// Create middleware
#[cfg(feature = "redis")]
let middleware = create_api_key_layer(api_key_store, rate_limit_store);
// Use with Axum router
// let app = axum::Router::new()
// .route("/api/data", axum::routing::get(handler))
// .layer(middleware);
Re-exports§
pub use tracing;
pub use deadpool_redis;
Structs§
- ApiKey
Layer - Layer for API key validation and rate limiting
- ApiKey
Middleware Config - Configuration for API key middleware
- ApiKey
Validation Result - API key validation result
- Barnacle
Config - Rate limiter configuration
- Barnacle
Context - Rate limiting context that includes route information
- Barnacle
Layer - Generic rate limiting layer that can extract keys from request bodies
- Legacy
Barnacle Result - Result of an increment attempt
- Redis
ApiKey Store - Redis
Barnacle Store - Implementation of BarnacleStore using Redis with connection pooling. This struct encapsulates Arc internally, so consumers don’t need to wrap it.
- Static
ApiKey Config - Per-key rate limiting configuration for static configurations
- Static
ApiKey Store - Static API key store that uses a predefined set of keys Useful for simple configurations where keys are known at compile time
Enums§
- Barnacle
Error - Main error type for the Barnacle library
- Barnacle
Key - Identification key for rate limiting (e.g., email, api-key, IP)
- Reset
OnSuccess
Constants§
Traits§
- ApiKey
Store - Trait for API key validation and configuration retrieval
- Barnacle
Store - Trait to abstract the rate limiter storage backend (e.g., Redis)
- KeyExtractable
- Trait to extract the key from any payload type
Functions§
- create_
api_ key_ layer - create_
api_ key_ layer_ with_ config - create_
api_ key_ layer_ with_ custom_ validator - create_
barnacle_ layer - Helper function to create the barnacle layer without payload deserialization
- create_
barnacle_ layer_ for_ payload - Helper function to create the barnacle layer for payload-based key extraction
Type Aliases§
- Barnacle
Result - Result type alias for Barnacle operations