Rate-Guard
A flexible and type-safe rate limiting library for Rust.
Rate-Guard provides a unified interface for multiple rate limiting algorithms with Duration-based configuration, type-safe time handling, and comprehensive error reporting. It's designed to integrate seamlessly with Rust's async ecosystem while maintaining high performance through zero-cost abstractions.
Features
- Duration-Based Configuration: All timing parameters use
std::time::Duration - Multiple Algorithms: token bucket, fixed window counter, sliding window counter, and approximate sliding window
- Time Source Abstraction: Pluggable time sources (mock, std::time, etc.)
- Precision Control: Compile-time precision selection for optimal performance
- Builder Pattern: Fluent configuration API with compile-time validation
- Zero-Cost Abstractions: High-level ergonomics without runtime overhead
- Comprehensive Testing: Built-in mock time source for deterministic testing
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
# Optional: Enable std::time support
= { = "0.1.0", = ["std-time"] }
Basic usage:
use ;
use TokenBucketBuilder;
use Duration;
// Create a token bucket that allows 100 requests with 10 refills every 100ms
let bucket = builder
.capacity
.refill_amount
.refill_every
.with_time
.
.build
.unwrap;
// Check if request is allowed
match bucket.try_acquire
// Check remaining capacity
println!;
Usage Examples
Token Bucket (Burst-friendly)
Allows bursts up to capacity with sustained rate limiting:
use ;
use TokenBucketBuilder;
use Duration;
let time_source = new;
let bucket = builder
.capacity
.refill_amount
.refill_every
.with_time
.
.build
.unwrap;
assert!;
time_source.advance;
assert_eq!; // 50 + 20 refill
Fixed Window Counter
use ;
use FixedWindowCounterBuilder;
use Duration;
let counter = builder
.capacity
.window_duration
.with_time
.
.build
.unwrap;
assert!;
Sliding Window Counter
use ;
use SlidingWindowCounterBuilder;
use Duration;
let counter = builder
.capacity
.bucket_duration
.bucket_count
.with_time
.
.build
.unwrap;
assert!;
Approximate Sliding Window
use ;
use ApproximateSlidingWindowBuilder;
use Duration;
let window = builder
.capacity
.window_duration
.with_time
.
.build
.unwrap;
assert!;
Production Usage
For detailed production environment integration examples, please refer to the examples/ directory:
token_bucket_sync.rs- Token Bucket sync exampletoken_bucket_async.rs- Token Bucket async examplefixed_window_counter_sync.rs- Fixed window counter sync examplefixed_window_counter_async.rs- Fixed window counter async examplesliding_window_counter_sync.rs- Sliding window counter sync examplesliding_window_counter_async.rs- Sliding window counter async exampleapprox_sliding_window_sync.rs- Approximate sliding window sync exampleapprox_sliding_window_async.rs- Approximate sliding window async example
Recommended Production Configuration
use ;
use TokenBucketBuilder;
use Duration;
Tip: Start with conservative settings and adjust based on your actual requirements
Verbose Error Handling
use ;
use TokenBucketBuilder;
use Duration;
let bucket = builder
.capacity
.refill_amount
.refill_every
.with_time
.
.build
.unwrap;
bucket.try_acquire.unwrap;
match bucket.try_acquire_verbose
Testing with Mock Time
use ;
use TokenBucketBuilder;
use Duration;
let time_source = new;
let bucket = builder
.capacity
.refill_amount
.refill_every
.with_time
.
.build
.unwrap;
assert!;
assert_eq!;
time_source.advance;
assert_eq!;
Precision Types
use ;
use TokenBucketBuilder;
use Duration;
let nano_bucket = builder
.capacity.refill_amount
.refill_every
.with_time
.
.build.unwrap;
let micro_bucket = builder
.capacity.refill_amount
.refill_every
.with_time
.
.build.unwrap;
let milli_bucket = builder
.capacity.refill_amount
.refill_every
.with_time
.
.build.unwrap;
let sec_bucket = builder
.capacity.refill_amount
.refill_every
.with_time
.
.build.unwrap;
Feature Flags
Time Sources (Optional)
std-time- Standard library time source (recommended for production)tokio-time- Tokio time source (for async environments)
Precision Types (Mutually Exclusive, defaults to u64)
tick-u64(default) - 64-bit precision, suitable for most scenariostick-u128- 128-bit precision, for extreme long-duration use cases
# Default configuration
= "0.1.0"
# Production recommended
= { = "0.1.0", = ["std-time"] }
# Special requirements: high precision
= { = "0.1.0", = ["std-time", "tick-u128"] }
Minimum Supported Rust Version (MSRV)
This crate supports Rust 1.70.0 or newer.
Feature-specific Requirements
- Basic functionality: Works with the specified MSRV
tokio-timefeature: Requires Rust 1.70.0+ (tokio requirement)
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.