rater 0.1.2

High-performance, lock-free, thread-safe rate limiter using token bucket algorithm with per-IP rate limiting support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! # Rater - High-Performance Rate Limiter for Rust
//!
//! A blazing-fast, lock-free rate limiting library that helps you control the flow of requests
//! in your application. Think of it as a traffic controller that ensures your system doesn't
//! get overwhelmed by too many requests at once.
//!
//! ## What is Rate Limiting?
//!
//! Rate limiting is like having a bouncer at a club - it controls how many requests can enter
//! your system within a specific time period. This prevents abuse, ensures fair usage, and
//! protects your services from being overwhelmed.
//!
//! ## The Token Bucket Algorithm
//!
//! This library uses the token bucket algorithm, which works like a vending machine:
//!
//! ```text
//!     Token Bucket Visualization:
//!
//!     Time 0:    [🪙🪙🪙🪙🪙] (5 tokens available)
//!     Request 1: [🪙🪙🪙🪙] ✅ (takes 1 token)
//!     Request 2: [🪙🪙🪙] ✅ (takes 1 token)
//!     Time +1s:  [🪙🪙🪙🪙🪙] (refilled to max)
//!     Request 3: [🪙🪙🪙🪙] ✅ (takes 1 token)
//! ```
//!
//! - **Tokens** = Permission to make a request
//! - **Bucket** = Container holding a limited number of tokens
//! - **Refill** = Tokens are added back at a steady rate
//!
//! ## Features
//!
//! - 🔒 **Lock-free Implementation** - Multiple threads can use it simultaneously without blocking
//! - ⚡ **Blazing Fast** - Optimized for your CPU architecture (uses PAUSE on Intel, YIELD on ARM)
//! - 🔄 **Smart Refilling** - Automatically adjusts when under heavy load
//! - 🌐 **Per-IP Limiting** - Different limits for different users/IPs
//! - 📊 **Real-time Metrics** - Monitor performance and detect issues
//! - 🛡️ **Thread-Safe** - Safe to share across threads without extra synchronization
//!
//! ## Quick Start
//!
//! ### Basic Rate Limiting
//!
//! ```rust
//! use rater::RateLimiter;
//!
//! // Create a rate limiter:
//! // - 100 tokens maximum capacity (burst limit)
//! // - Refills 10 tokens every second
//! let limiter = RateLimiter::new(100, 10);
//!
//! // Try to process a request
//! if limiter.try_acquire() {
//!     println!("✅ Request approved - processing...");
//!     // Your request handling code here
//! } else {
//!     println!("⛔ Rate limited - try again later");
//!     // Return 429 Too Many Requests
//! }
//! ```
//!
//! ### Advanced Usage with Builder Pattern
//!
//! ```rust
//! use rater::{RateLimiterBuilder, MemoryOrdering};
//!
//! let limiter = RateLimiterBuilder::new()
//!     .max_tokens(1000)           // Maximum burst size
//!     .refill_rate(100)           // Tokens per interval
//!     .refill_interval_ms(1000)   // Refill every second
//!     .memory_ordering(MemoryOrdering::AcquireRelease)  // Memory consistency
//!     .build();
//!
//! // Acquire multiple tokens at once (for expensive operations)
//! if limiter.try_acquire_n(5) {
//!     println!("Acquired 5 tokens for expensive operation");
//! }
//! ```
//!
//! ### Per-IP Rate Limiting
//!
//! ```rust
//! use rater::{IpRateLimiterManager, RateLimiterConfig};
//! use std::net::IpAddr;
//!
//! // Create a manager that handles rate limiting per IP address
//! let config = RateLimiterConfig::per_second(10);  // 10 requests per second per IP
//! let manager = IpRateLimiterManager::new(config);
//!
//! // In your request handler:
//! let client_ip: IpAddr = "192.168.1.100".parse().unwrap();
//!
//! if manager.try_acquire(client_ip) {
//!     // Process request
//! } else {
//!     // Rate limit this specific IP
//! }
//! ```
//!
//! ## Architecture Overview
//!
//! ```text
//!                    ┌─────────────────────────┐
//!                    │   Your Application      │
//!                    └──────────┬──────────────┘
//!//!                    ┌──────────▼──────────────┐
//!                    │    Rate Limiter API     │
//!                    ├──────────────────────────┤
//!                    │  • try_acquire()         │
//!                    │  • try_acquire_n()       │
//!                    │  • metrics()             │
//!                    └──────────┬───────────────┘
//!//!                ┌──────────────┴───────────────┐
//!                │                               │
//!     ┌──────────▼──────────┐       ┌───────────▼──────────┐
//!     │   Token Bucket      │       │   IP Manager         │
//!     ├─────────────────────┤       ├──────────────────────┤
//!     │ • Atomic tokens     │       │ • Per-IP limiters    │
//!     │ • Auto refill       │       │ • Auto cleanup       │
//!     │ • Lock-free CAS     │       │ • LRU eviction       │
//!     └─────────────────────┘       └──────────────────────┘
//! ```
//!
//! ## Performance Characteristics
//!
//! | Operation | Time Complexity | Space Complexity |
//! |-----------|----------------|------------------|
//! | try_acquire() | O(1)* | O(1) |
//! | try_acquire_n() | O(1)* | O(1) |
//! | refill | O(1)* | O(1) |
//! | metrics | O(1) | O(1) |
//!
//! *Amortized constant time with bounded retries under contention
//!
//! ## Common Use Cases
//!
//! 1. **API Rate Limiting** - Prevent API abuse and ensure fair usage
//! 2. **Database Protection** - Limit queries to prevent overload
//! 3. **Microservice Communication** - Control inter-service request rates
//! 4. **Background Job Processing** - Throttle job execution rates
//! 5. **Resource Access Control** - Limit access to expensive resources
//!
//! ## Thread Safety
//!
//! All types are thread-safe and can be shared across threads:
//! - `RateLimiter` - Safe to share via `Arc<RateLimiter>`
//! - `IpRateLimiterManager` - Safe to share via `Arc<IpRateLimiterManager>`
//!
//! ## Memory Ordering
//!
//! Choose the right memory ordering for your use case:
//! - `Relaxed` - Fastest, use when exact ordering doesn't matter
//! - `AcquireRelease` - Balanced (default), ensures proper synchronization
//! - `Sequential` - Strongest guarantees, use when strict ordering is critical
//!
//! ## Examples
//!
//! See the `examples/` directory for complete examples:
//! - `basic.rs` - Simple rate limiting
//! - `ip_limiting.rs` - Per-IP rate limiting with cleanup
//!
//! ## Safety
//!
//! This crate uses `unsafe` code only for:
//! - Platform-specific CPU pause instructions (for efficient spinning)
//! - Cache alignment optimizations (to prevent false sharing)
//!
//! All unsafe code is thoroughly tested and documented.

#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(
    missing_docs,
    rust_2018_idioms,
    unreachable_pub,
    missing_debug_implementations
)]
#![forbid(unsafe_op_in_unsafe_fn)]

// Internal module
mod rate_limiter;

// Public re-exports
pub use rate_limiter::{
    cpu_relax, current_time_ms, current_time_ns, current_time_us, HealthStatus,
    IpRateLimiterManager, ManagerStats, MemoryOrdering, RateLimiter, RateLimiterConfig,
    RateLimiterMetrics,
};

/// A rate limiter wrapped in `Arc` for convenient thread-safe sharing.
///
/// # Example
/// ```rust
/// use rater::{RateLimiter, SharedRateLimiter};
/// use std::sync::Arc;
///
/// let limiter = RateLimiter::new(100, 10);
/// let shared: SharedRateLimiter = Arc::new(limiter);
///
/// // Now you can clone and share across threads
/// let limiter_clone = shared.clone();
/// std::thread::spawn(move || {
///     limiter_clone.try_acquire();
/// });
/// ```
pub type SharedRateLimiter = std::sync::Arc<RateLimiter>;

/// An IP rate limiter manager wrapped in `Arc` for convenient thread-safe sharing.
///
/// This is useful when you need to share the IP manager across multiple threads
/// or async tasks in a web server.
pub type SharedIpManager = std::sync::Arc<IpRateLimiterManager>;

/// Version information for the crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Minimum supported Rust version.
///
/// This crate requires at least Rust 1.70.0 due to:
/// - Stable atomic operations
/// - Const generics
/// - Edition 2021 features
pub const MSRV: &str = "1.70.0";

/// Prelude module for convenient imports.
///
/// Import everything you need with a single line:
/// ```rust
/// use rater::prelude::*;
/// ```
pub mod prelude {

    pub use crate::{
        HealthStatus, IpRateLimiterManager, ManagerStats, MemoryOrdering, RateLimiter,
        RateLimiterConfig, RateLimiterMetrics, SharedIpManager, SharedRateLimiter,
    };
}

/// Builder pattern for creating rate limiters with custom configuration.
///
/// The builder pattern provides a fluent API for constructing rate limiters
/// with validated configuration. This is the recommended way to create
/// rate limiters with non-default settings.
///
/// # Example
///
/// ```rust
/// use rater::{RateLimiterBuilder, MemoryOrdering};
///
/// // Build a rate limiter for 100 requests per minute
/// let limiter = RateLimiterBuilder::new()
///     .max_tokens(100)              // Burst capacity
///     .refill_rate(100)            // Tokens to add
///     .refill_interval_ms(60_000)  // Every minute
///     .memory_ordering(MemoryOrdering::Relaxed)  // Fast mode
///     .build();
///
/// // Or use try_build() for error handling
/// let result = RateLimiterBuilder::new()
///     .max_tokens(0)  // Invalid!
///     .try_build();
///
/// assert!(result.is_err());
/// ```
#[derive(Debug, Clone)]
pub struct RateLimiterBuilder {
    config: RateLimiterConfig,
}

impl RateLimiterBuilder {
    /// Creates a new builder with default configuration.
    ///
    /// Default configuration:
    /// - 50 maximum tokens
    /// - 10 tokens refill rate
    /// - 1000ms (1 second) refill interval
    /// - AcquireRelease memory ordering
    pub fn new() -> Self {
        Self {
            config: RateLimiterConfig::default(),
        }
    }

    /// Sets the maximum number of tokens the bucket can hold.
    ///
    /// This is also known as the "burst capacity" - the maximum number
    /// of requests that can be made in rapid succession.
    ///
    /// # Arguments
    ///
    /// * `tokens` - Maximum token capacity (must be > 0)
    pub fn max_tokens(mut self, tokens: u64) -> Self {
        self.config.max_tokens = tokens;
        self
    }

    /// Sets the refill rate (tokens added per interval).
    ///
    /// This determines the sustained rate limit. For example, with a
    /// refill_rate of 10 and refill_interval_ms of 1000, you get
    /// 10 requests per second sustained rate.
    ///
    /// # Arguments
    ///
    /// * `rate` - Number of tokens to add each interval
    pub fn refill_rate(mut self, rate: u32) -> Self {
        self.config.refill_rate = rate;
        self
    }

    /// Sets the refill interval in milliseconds.
    ///
    /// How often tokens are added back to the bucket.
    /// Common values:
    /// - 1000 ms = per second
    /// - 60000 ms = per minute
    /// - 100 ms = 10 times per second
    ///
    /// # Arguments
    ///
    /// * `ms` - Interval between refills in milliseconds (must be > 0)
    pub fn refill_interval_ms(mut self, ms: u64) -> Self {
        self.config.refill_interval_ms = ms;
        self
    }

    /// Sets the memory ordering strategy for atomic operations.
    ///
    /// - `Relaxed`: Fastest but weakest guarantees
    /// - `AcquireRelease`: Balanced performance and correctness (default)
    /// - `Sequential`: Strongest guarantees but slower
    ///
    /// Unless you have specific requirements, use the default.
    pub fn memory_ordering(mut self, ordering: MemoryOrdering) -> Self {
        self.config.ordering = ordering;
        self
    }

    /// Builds the rate limiter with the configured settings.
    ///
    /// # Panics
    ///
    /// Panics if the configuration is invalid:
    /// - `max_tokens` is 0
    /// - `refill_interval_ms` is 0
    /// - `refill_rate` exceeds `max_tokens`
    ///
    /// Use `try_build()` if you want to handle errors.
    pub fn build(self) -> RateLimiter {
        RateLimiter::with_config(self.config)
    }

    /// Attempts to build the rate limiter, returning an error if invalid.
    ///
    /// This is the safe version that returns a `Result` instead of panicking.
    ///
    /// # Errors
    ///
    /// Returns an error message if configuration is invalid.
    pub fn try_build(self) -> Result<RateLimiter, &'static str> {
        self.config.validate()?;
        Ok(RateLimiter::with_config(self.config))
    }
}

impl Default for RateLimiterBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_basic_functionality() {
        let config = RateLimiterConfig::new(10, 1, 600_000);
        let limiter = RateLimiter::with_config(config);

        for _ in 0..10 {
            assert!(limiter.try_acquire());
        }

        assert!(!limiter.try_acquire());

        let metrics = limiter.metrics();
        assert_eq!(metrics.total_acquired, 10);
        assert_eq!(metrics.total_rejected, 1);
    }

    #[test]
    fn test_builder() {
        let limiter = RateLimiterBuilder::new()
            .max_tokens(50)
            .refill_rate(5)
            .refill_interval_ms(1000)
            .build();

        assert_eq!(limiter.available_tokens(), 50);
    }

    #[test]
    fn test_builder_validation() {
        let result = RateLimiterBuilder::new().max_tokens(0).try_build();

        assert!(result.is_err());
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_thread_safety() {
        let config = RateLimiterConfig::new(1000, 100, 60_000);
        let limiter = Arc::new(RateLimiter::with_config(config));
        let mut handles = vec![];

        for _ in 0..10 {
            let limiter_clone = limiter.clone();
            let handle = thread::spawn(move || {
                let mut acquired = 0;
                for _ in 0..200 {
                    if limiter_clone.try_acquire() {
                        acquired += 1;
                    }
                }
                acquired
            });
            handles.push(handle);
        }

        let total: u32 = handles.into_iter().map(|h| h.join().unwrap()).sum();

        assert!(total <= 1100, "total={total}");
        assert!(total >= 900, "total={total}");
    }
    #[test]
    fn test_prelude_imports() {
        // Test that prelude exports work
        use crate::prelude::*;

        let _limiter = RateLimiter::new(10, 1);
        let _config = RateLimiterConfig::default();
        let _ordering = MemoryOrdering::AcquireRelease;
        let _status = HealthStatus::Healthy;
    }

    #[test]
    fn test_shared_types() {
        let limiter = RateLimiter::new(10, 1);
        let _shared: SharedRateLimiter = std::sync::Arc::new(limiter);

        let manager = IpRateLimiterManager::new(RateLimiterConfig::default());
        let _shared_manager: SharedIpManager = std::sync::Arc::new(manager);
    }

    #[test]
    fn test_constants() {
        assert!(!VERSION.is_empty());
        assert_eq!(MSRV, "1.70.0");
    }

    #[test]
    fn test_builder_default() {
        let builder = RateLimiterBuilder::default();
        let limiter = builder.build();
        assert!(limiter.available_tokens() > 0);
    }

    #[test]
    fn test_builder_chain() {
        let limiter = RateLimiterBuilder::new()
            .max_tokens(100)
            .refill_rate(10)
            .refill_interval_ms(500)
            .memory_ordering(MemoryOrdering::Sequential)
            .build();

        assert_eq!(limiter.available_tokens(), 100);
    }

    #[test]
    fn test_builder_try_build_refill_rate_zero() {
        let result = RateLimiterBuilder::new().refill_rate(0).try_build();
        assert!(result.is_err());
    }

    #[test]
    fn test_builder_try_build_success() {
        let result = RateLimiterBuilder::new()
            .max_tokens(50)
            .refill_rate(10)
            .refill_interval_ms(1000)
            .try_build();
        assert!(result.is_ok());
        let limiter = result.unwrap();
        assert_eq!(limiter.available_tokens(), 50);
    }

    #[test]
    fn test_builder_debug() {
        let builder = RateLimiterBuilder::new();
        let debug = format!("{:?}", builder);
        assert!(debug.contains("RateLimiterBuilder"));
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_metrics_after_mixed_operations() {
        let config = RateLimiterConfig::new(10, 1, 600_000);
        let limiter = RateLimiter::with_config(config);

        // 5 single acquires
        for _ in 0..5 {
            assert!(limiter.try_acquire());
        }

        // 1 bulk acquire of 3
        assert!(limiter.try_acquire_n(3));

        // 1 failed bulk acquire
        assert!(!limiter.try_acquire_n(5));

        // 2 remaining single
        assert!(limiter.try_acquire());
        assert!(limiter.try_acquire());

        // 1 failed single
        assert!(!limiter.try_acquire());

        let metrics = limiter.metrics();
        // Counts attempts: 5 + 1 + 1 + 1 = 8 success, 1 + 1 = 2 fail
        assert_eq!(metrics.total_acquired, 8);
        assert_eq!(metrics.total_rejected, 2);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_shared_limiter_across_threads() {
        let shared: SharedRateLimiter = std::sync::Arc::new(RateLimiter::new(100, 10));
        let mut handles = vec![];

        for _ in 0..4 {
            let s = shared.clone();
            handles.push(thread::spawn(move || {
                let mut count = 0u32;
                for _ in 0..50 {
                    if s.try_acquire() {
                        count += 1;
                    }
                }
                count
            }));
        }

        let total: u32 = handles.into_iter().map(|h| h.join().unwrap()).sum();
        assert!(total > 0 && total <= 100);
    }

    #[test]
    fn test_version_is_valid_semver() {
        let parts: Vec<&str> = VERSION.split('.').collect();
        assert_eq!(parts.len(), 3, "VERSION should be semver: {}", VERSION);
        for part in &parts {
            assert!(part.parse::<u32>().is_ok(), "Invalid semver part: {}", part);
        }
    }
}