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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! This module provides comprehensive performance monitoring and health analysis
//! for rate limiters. It helps you understand how your rate limiting is performing
//! and detect when your system is under stress.
//!
//! ## Metrics Overview
//!
//! ```text
//!     Metrics Dashboard:
//!     ┌─────────────────────────────────────┐
//!     │  Success Rate: 85%                 │
//!     │  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░  (85/100)     │
//!     │                                     │
//!     │  Token Usage: 70%                   │
//!     │  ▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░  (70/100)   │
//!     │                                     │
//!     │  Health: ✅ Healthy                 │
//!     │  Pressure: Low                      │
//!     │  Max Wait: 1.5ms                    │
//!     └─────────────────────────────────────┘
//! ```

use std::fmt;

/// Comprehensive metrics for rate limiter performance analysis.
///
/// This struct provides a snapshot of all important rate limiter metrics,
/// allowing you to monitor performance, detect issues, and make informed
/// decisions about capacity planning.
///
/// ## Key Metrics Explained
///
/// ### Success Metrics
/// - **total_acquired**: Successfully processed requests
/// - **total_rejected**: Requests that were rate limited
/// - **success_rate**: Percentage of successful requests
///
/// ### Capacity Metrics
/// - **current_tokens**: Available capacity right now
/// - **max_tokens**: Maximum possible capacity
/// - **utilization**: How much of the capacity is being used
///
/// ### Pressure Indicators
/// - **consecutive_rejections**: Recent rejection streak (high = pressure)
/// - **pressure_ratio**: Overall rejection ratio
/// - **max_wait_time_ns**: Longest wait observed
///
/// ## Example Usage
///
/// ```rust
/// use rater::RateLimiter;
///
/// let limiter = RateLimiter::new(100, 10);
/// // ... use the limiter ...
///
/// let metrics = limiter.metrics();
///
/// // Check health
/// if metrics.is_under_pressure() {
///     println!("⚠️ System under pressure!");
///     println!("Success rate: {:.1}%", metrics.success_rate() * 100.0);
/// }
///
/// // Display comprehensive report
/// println!("{}", metrics.summary());
/// ```
#[derive(Debug, Clone)]
pub struct RateLimiterMetrics {
    /// Total number of tokens successfully acquired.
    /// This represents the number of allowed requests.
    pub total_acquired: u64,

    /// Total number of token acquisition attempts that were rejected.
    /// This represents the number of rate-limited requests.
    pub total_rejected: u64,

    /// Total number of refill operations performed.
    /// High numbers indicate the limiter has been active for a while.
    pub total_refills: u64,

    /// Current number of available tokens in the bucket.
    /// This is the immediate capacity available.
    pub current_tokens: u64,

    /// Maximum capacity of the token bucket.
    /// This is the burst limit configured for the limiter.
    pub max_tokens: u64,

    /// Number of consecutive rejections without a successful acquisition.
    /// High values (>10) indicate sustained pressure.
    pub consecutive_rejections: u32,

    /// Maximum wait time observed in nanoseconds.
    /// Useful for identifying contention issues.
    pub max_wait_time_ns: u64,

    /// Ratio of rejected requests to total requests (0.0 to 1.0).
    /// Values above 0.3 indicate significant pressure.
    pub pressure_ratio: f64,
}

impl RateLimiterMetrics {
    /// Calculates the success rate of token acquisitions.
    ///
    /// # Returns
    ///
    /// A value between 0.0 and 1.0, where:
    /// - 1.0 = 100% success (no rejections)
    /// - 0.5 = 50% success (half rejected)
    /// - 0.0 = 0% success (all rejected)
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// if metrics.success_rate() < 0.8 {
    ///     println!("Warning: High rejection rate!");
    /// }
    /// ```
    #[inline]
    pub fn success_rate(&self) -> f64 {
        let total = self.total_acquired + self.total_rejected;
        if total == 0 {
            1.0 // No requests yet, assume success
        } else {
            self.total_acquired as f64 / total as f64
        }
    }

    /// Calculates the rejection rate (inverse of success rate).
    ///
    /// # Returns
    ///
    /// A value between 0.0 and 1.0 representing the fraction of rejected requests.
    #[inline]
    pub fn rejection_rate(&self) -> f64 {
        1.0 - self.success_rate()
    }

    /// Determines if the rate limiter is under immediate pressure.
    ///
    /// Immediate pressure means:
    /// - Success rate below 50%, OR
    /// - No tokens currently available
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// if metrics.is_under_pressure() {
    ///     // Consider backing off or queueing requests
    /// }
    /// ```
    #[inline]
    pub fn is_under_pressure(&self) -> bool {
        self.success_rate() < 0.5 || self.current_tokens == 0
    }

    /// Calculates the current utilization of the token bucket.
    ///
    /// Utilization shows how much of the capacity is being used:
    /// - 0.0 = Bucket is full (no usage)
    /// - 0.5 = Half capacity used
    /// - 1.0 = Bucket is empty (full usage)
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// if metrics.utilization() > 0.9 {
    ///     println!("Running at high utilization!");
    /// }
    /// ```
    #[inline]
    pub fn utilization(&self) -> f64 {
        if self.max_tokens == 0 {
            0.0
        } else {
            1.0 - (self.current_tokens as f64 / self.max_tokens as f64)
        }
    }

    /// Returns the percentage of available tokens.
    ///
    /// This is the inverse of utilization, showing remaining capacity:
    /// - 100% = Bucket is full
    /// - 50% = Half capacity available
    /// - 0% = No tokens available
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// println!("Available capacity: {:.1}%", metrics.availability_percentage());
    /// ```
    #[inline]
    pub fn availability_percentage(&self) -> f64 {
        if self.max_tokens == 0 {
            0.0
        } else {
            (self.current_tokens as f64 / self.max_tokens as f64) * 100.0
        }
    }

    /// Determines if the rate limiter is under sustained pressure.
    ///
    /// Sustained pressure indicates ongoing high demand that exceeds capacity.
    /// This is detected when:
    /// - More than 10 consecutive rejections, OR
    /// - Overall rejection ratio above 30%
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// if metrics.is_under_sustained_pressure() {
    ///     // Consider scaling up capacity or implementing backpressure
    ///     println!("System needs intervention!");
    /// }
    /// ```
    #[inline]
    pub fn is_under_sustained_pressure(&self) -> bool {
        self.consecutive_rejections > 10 || self.pressure_ratio > 0.3
    }

    /// Returns the maximum wait time in microseconds.
    ///
    /// Converts nanoseconds to microseconds for easier reading.
    #[inline]
    pub fn max_wait_time_us(&self) -> f64 {
        self.max_wait_time_ns as f64 / 1000.0
    }

    /// Returns the maximum wait time in milliseconds.
    ///
    /// Converts nanoseconds to milliseconds for easier reading.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// if metrics.max_wait_time_ms() > 10.0 {
    ///     println!("High contention detected: {:.2}ms max wait",
    ///              metrics.max_wait_time_ms());
    /// }
    /// ```
    #[inline]
    pub fn max_wait_time_ms(&self) -> f64 {
        self.max_wait_time_ns as f64 / 1_000_000.0
    }

    /// Returns the total number of requests (acquired + rejected).
    #[inline]
    pub fn total_requests(&self) -> u64 {
        self.total_acquired + self.total_rejected
    }

    /// Determines the health status of the rate limiter.
    ///
    /// Health status provides a quick assessment of the limiter's state:
    /// - **Healthy**: Operating normally
    /// - **Degraded**: Under some pressure but functional
    /// - **Critical**: Severe pressure, intervention needed
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::{HealthStatus, RateLimiter};
    ///
    /// let limiter = RateLimiter::new(100, 10);
    /// let metrics = limiter.metrics();
    /// match metrics.health_status() {
    ///     HealthStatus::Healthy => println!("✅ All good"),
    ///     HealthStatus::Degraded => println!("⚠️ Monitor closely"),
    ///     HealthStatus::Critical => println!("🔴 Take action!"),
    /// }
    /// ```
    pub fn health_status(&self) -> HealthStatus {
        if self.is_under_sustained_pressure() {
            HealthStatus::Critical
        } else if self.is_under_pressure() {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        }
    }

    /// Generates a human-readable summary of the metrics.
    ///
    /// This provides a comprehensive report suitable for logging or display.
    ///
    /// # Example Output
    ///
    /// ```text
    /// RateLimiter Metrics:
    /// ├─ Performance:
    /// │  ├─ Success Rate: 85.50%
    /// │  ├─ Rejection Rate: 14.50%
    /// │  └─ Max Wait Time: 1.234ms
    /// ├─ Capacity:
    /// │  ├─ Available Tokens: 75/100
    /// │  ├─ Utilization: 25.00%
    /// │  └─ Availability: 75.00%
    /// └─ Health:
    ///    ├─ Status: Healthy
    ///    └─ Under Pressure: false
    /// ```
    pub fn summary(&self) -> String {
        format!(
            "RateLimiter Metrics:\n\
             ├─ Performance:\n\
             │  ├─ Success Rate: {:.2}%\n\
             │  ├─ Rejection Rate: {:.2}%\n\
             │  └─ Max Wait Time: {:.3}ms\n\
             ├─ Capacity:\n\
             │  ├─ Available Tokens: {}/{}\n\
             │  ├─ Utilization: {:.2}%\n\
             │  └─ Availability: {:.2}%\n\
             ├─ Counters:\n\
             │  ├─ Total Acquired: {}\n\
             │  ├─ Total Rejected: {}\n\
             │  ├─ Total Refills: {}\n\
             │  └─ Consecutive Rejections: {}\n\
             └─ Health:\n\
                ├─ Status: {:?}\n\
                ├─ Under Pressure: {}\n\
                └─ Under Sustained Pressure: {}",
            self.success_rate() * 100.0,
            self.rejection_rate() * 100.0,
            self.max_wait_time_ms(),
            self.current_tokens,
            self.max_tokens,
            self.utilization() * 100.0,
            self.availability_percentage(),
            self.total_acquired,
            self.total_rejected,
            self.total_refills,
            self.consecutive_rejections,
            self.health_status(),
            self.is_under_pressure(),
            self.is_under_sustained_pressure()
        )
    }
}

impl fmt::Display for RateLimiterMetrics {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.summary())
    }
}

/// Health status indicator for the rate limiter.
///
/// Provides a simple three-level assessment of rate limiter health,
/// making it easy to trigger alerts or take action based on status.
///
/// ## Status Levels
///
/// ```text
///     Healthy ──────► Normal operation, plenty of capacity
//////     Degraded ─────► Some pressure, monitor closely
//////     Critical ─────► Severe pressure, immediate action needed
/// ```
///
/// ## Example Usage
///
/// ```rust
/// use tracing::{error, warn};
/// use rater::{RateLimiter, HealthStatus};
///
/// let limiter = RateLimiter::new(100, 10);
/// // ... heavy usage ...
///
/// let metrics = limiter.metrics();
/// let health = metrics.health_status();
///
/// // Take action based on health
/// match health {
///     HealthStatus::Healthy => {
///         // Normal operation
///     }
///     HealthStatus::Degraded => {
///         // Log warning, consider scaling
///         warn!("Rate limiter degraded: {}", health.suggested_action());
///     }
///     HealthStatus::Critical => {
///         // Alert on-call, scale immediately
///         error!("Rate limiter critical: {}", health.suggested_action());
///     }
/// }
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthStatus {
    /// Operating normally with good success rates.
    ///
    /// Indicates:
    /// - Success rate above 50%
    /// - Tokens available
    /// - Low rejection count
    Healthy,

    /// Under some pressure but still functional.
    ///
    /// Indicates:
    /// - Success rate below 50% OR
    /// - No tokens currently available
    /// - System can recover if load decreases
    Degraded,

    /// Under severe pressure, intervention recommended.
    ///
    /// Indicates:
    /// - Sustained high rejection rate (>30%) OR
    /// - Many consecutive rejections (>10)
    /// - System needs scaling or load reduction
    Critical,
}

impl HealthStatus {
    /// Returns true if the status indicates any problems.
    ///
    /// Useful for simple health checks.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let rater = RateLimiter::new(100, 10);
    /// let health  = rater.metrics().health_status();
    /// if health.is_unhealthy() {
    ///     // Take corrective action
    ///     true;
    /// }
    /// ```
    pub fn is_unhealthy(&self) -> bool {
        !matches!(self, Self::Healthy)
    }

    /// Returns a suggested action based on the health status.
    ///
    /// Provides actionable guidance for operators.
    ///
    /// # Example
    ///
    /// ```rust
    /// use rater::RateLimiter;
    ///
    /// let rater = RateLimiter::new(100, 10);
    /// let health  = rater.metrics().health_status();
    /// println!("Recommendation: {}", health.suggested_action());
    /// ```
    pub fn suggested_action(&self) -> &'static str {
        match self {
            Self::Healthy => "No action needed",
            Self::Degraded => "Monitor closely, consider increasing capacity",
            Self::Critical => "Immediate action required: scale up or reduce load",
        }
    }
}

impl fmt::Display for HealthStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Healthy => write!(f, "✅ Healthy"),
            Self::Degraded => write!(f, "⚠️ Degraded"),
            Self::Critical => write!(f, "🔴 Critical"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_metrics_calculations() {
        let metrics = RateLimiterMetrics {
            total_acquired: 80,
            total_rejected: 20,
            total_refills: 10,
            current_tokens: 25,
            max_tokens: 100,
            consecutive_rejections: 5,
            max_wait_time_ns: 1_000_000,
            pressure_ratio: 0.2,
        };

        assert_eq!(metrics.success_rate(), 0.8);
        assert_eq!(metrics.utilization(), 0.75);
        assert!(!metrics.is_under_pressure());
        assert_eq!(metrics.health_status(), HealthStatus::Healthy);
    }

    #[test]
    fn test_health_status() {
        let metrics = RateLimiterMetrics {
            total_acquired: 40,
            total_rejected: 60,
            total_refills: 10,
            current_tokens: 0,
            max_tokens: 100,
            consecutive_rejections: 15,
            max_wait_time_ns: 0,
            pressure_ratio: 0.6,
        };

        assert!(metrics.is_under_pressure());
        assert!(metrics.is_under_sustained_pressure());
        assert_eq!(metrics.health_status(), HealthStatus::Critical);
    }

    #[test]
    fn test_edge_cases() {
        // Test with zero totals
        let metrics = RateLimiterMetrics {
            total_acquired: 0,
            total_rejected: 0,
            total_refills: 0,
            current_tokens: 50,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.0,
        };

        assert_eq!(metrics.success_rate(), 1.0);
        assert_eq!(metrics.utilization(), 0.5);
        assert!(!metrics.is_under_pressure());

        // Test with max_tokens = 0
        let metrics = RateLimiterMetrics {
            total_acquired: 0,
            total_rejected: 0,
            total_refills: 0,
            current_tokens: 0,
            max_tokens: 0,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.0,
        };

        assert_eq!(metrics.utilization(), 0.0);
        assert_eq!(metrics.availability_percentage(), 0.0);
    }
    #[test]
    fn test_health_status_methods() {
        assert!(!HealthStatus::Healthy.is_unhealthy());
        assert!(HealthStatus::Degraded.is_unhealthy());
        assert!(HealthStatus::Critical.is_unhealthy());

        assert_eq!(HealthStatus::Healthy.suggested_action(), "No action needed");
        assert!(HealthStatus::Degraded
            .suggested_action()
            .contains("Monitor"));
        assert!(HealthStatus::Critical
            .suggested_action()
            .contains("Immediate"));
    }

    #[test]
    fn test_health_status_display() {
        let healthy = format!("{}", HealthStatus::Healthy);
        assert!(healthy.contains("Healthy"));

        let degraded = format!("{}", HealthStatus::Degraded);
        assert!(degraded.contains("Degraded"));

        let critical = format!("{}", HealthStatus::Critical);
        assert!(critical.contains("Critical"));
    }

    #[test]
    fn test_metrics_display() {
        let metrics = RateLimiterMetrics {
            total_acquired: 100,
            total_rejected: 20,
            total_refills: 5,
            current_tokens: 30,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 1_500_000,
            pressure_ratio: 0.1,
        };

        let display = format!("{}", metrics);
        assert!(display.contains("RateLimiter Metrics"));
        assert!(display.contains("Success Rate"));

        let summary = metrics.summary();
        assert!(summary.contains("Performance"));
        assert!(summary.contains("Capacity"));
        assert!(summary.contains("Health"));
    }

    #[test]
    fn test_metrics_time_conversions() {
        let metrics = RateLimiterMetrics {
            total_acquired: 0,
            total_rejected: 0,
            total_refills: 0,
            current_tokens: 0,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 1_500_000_000, // 1.5 seconds
            pressure_ratio: 0.0,
        };

        assert_eq!(metrics.max_wait_time_us(), 1_500_000.0);
        assert_eq!(metrics.max_wait_time_ms(), 1_500.0);
    }

    #[test]
    fn test_total_requests() {
        let metrics = RateLimiterMetrics {
            total_acquired: 75,
            total_rejected: 25,
            total_refills: 0,
            current_tokens: 0,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.0,
        };

        assert_eq!(metrics.total_requests(), 100);
    }

    #[test]
    fn test_degraded_status() {
        // Under pressure but not sustained: success < 50% but low consecutive rejections
        let metrics = RateLimiterMetrics {
            total_acquired: 30,
            total_rejected: 70,
            total_refills: 0,
            current_tokens: 5,
            max_tokens: 100,
            consecutive_rejections: 3,
            max_wait_time_ns: 0,
            pressure_ratio: 0.1,
        };

        assert!(metrics.is_under_pressure());
        assert!(!metrics.is_under_sustained_pressure());
        assert_eq!(metrics.health_status(), HealthStatus::Degraded);
    }

    #[test]
    fn test_degraded_by_empty_tokens() {
        // Tokens empty but success rate is still high
        let metrics = RateLimiterMetrics {
            total_acquired: 90,
            total_rejected: 10,
            total_refills: 5,
            current_tokens: 0,
            max_tokens: 100,
            consecutive_rejections: 2,
            max_wait_time_ns: 0,
            pressure_ratio: 0.1,
        };

        assert!(metrics.is_under_pressure());
        assert_eq!(metrics.health_status(), HealthStatus::Degraded);
    }

    #[test]
    fn test_rejection_rate() {
        let metrics = RateLimiterMetrics {
            total_acquired: 60,
            total_rejected: 40,
            total_refills: 0,
            current_tokens: 50,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.0,
        };

        assert!((metrics.rejection_rate() - 0.4).abs() < 0.001);
        assert!((metrics.success_rate() - 0.6).abs() < 0.001);
    }

    #[test]
    fn test_sustained_pressure_by_consecutive_rejections() {
        let metrics = RateLimiterMetrics {
            total_acquired: 90,
            total_rejected: 10,
            total_refills: 0,
            current_tokens: 0,
            max_tokens: 100,
            consecutive_rejections: 15,
            max_wait_time_ns: 0,
            pressure_ratio: 0.05,
        };

        assert!(metrics.is_under_sustained_pressure());
        assert_eq!(metrics.health_status(), HealthStatus::Critical);
    }

    #[test]
    fn test_sustained_pressure_by_pressure_ratio() {
        let metrics = RateLimiterMetrics {
            total_acquired: 60,
            total_rejected: 40,
            total_refills: 0,
            current_tokens: 50,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.4,
        };

        assert!(metrics.is_under_sustained_pressure());
    }

    #[test]
    fn test_utilization_boundaries() {
        // Full bucket = 0% utilization
        let full = RateLimiterMetrics {
            total_acquired: 0,
            total_rejected: 0,
            total_refills: 0,
            current_tokens: 100,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.0,
        };
        assert_eq!(full.utilization(), 0.0);
        assert_eq!(full.availability_percentage(), 100.0);

        // Empty bucket = 100% utilization
        let empty = RateLimiterMetrics {
            total_acquired: 0,
            total_rejected: 0,
            total_refills: 0,
            current_tokens: 0,
            max_tokens: 100,
            consecutive_rejections: 0,
            max_wait_time_ns: 0,
            pressure_ratio: 0.0,
        };
        assert_eq!(empty.utilization(), 1.0);
        assert_eq!(empty.availability_percentage(), 0.0);
    }

    #[test]
    fn test_metrics_clone() {
        let metrics = RateLimiterMetrics {
            total_acquired: 42,
            total_rejected: 13,
            total_refills: 7,
            current_tokens: 30,
            max_tokens: 100,
            consecutive_rejections: 2,
            max_wait_time_ns: 500,
            pressure_ratio: 0.1,
        };
        let cloned = metrics.clone();

        assert_eq!(cloned.total_acquired, 42);
        assert_eq!(cloned.total_rejected, 13);
        assert_eq!(cloned.current_tokens, 30);
    }

    #[test]
    fn test_health_status_clone_eq() {
        let h1 = HealthStatus::Healthy;
        let h2 = h1;
        assert_eq!(h1, h2);

        let h3 = HealthStatus::Critical;
        assert_ne!(h1, h3);
    }

    #[test]
    fn test_summary_contains_all_fields() {
        let metrics = RateLimiterMetrics {
            total_acquired: 50,
            total_rejected: 10,
            total_refills: 3,
            current_tokens: 40,
            max_tokens: 100,
            consecutive_rejections: 1,
            max_wait_time_ns: 2_000_000,
            pressure_ratio: 0.1,
        };

        let summary = metrics.summary();
        assert!(summary.contains("50")); // total_acquired
        assert!(summary.contains("10")); // total_rejected
        assert!(summary.contains("3")); // total_refills
        assert!(summary.contains("40/100")); // tokens
        assert!(summary.contains("Consecutive Rejections: 1"));
    }
}