tracing-throttle 0.4.2

High-performance log deduplication and rate limiting for the tracing ecosystem
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
//! Rate limiter coordination logic.
//!
//! The rate limiter decides whether events should be allowed or suppressed
//! based on policies and tracks suppression counts.

use crate::application::circuit_breaker::CircuitBreaker;
use crate::application::metrics::Metrics;
use crate::application::ports::Storage;
use crate::application::registry::SuppressionRegistry;
use crate::domain::{
    policy::{PolicyDecision, RateLimitPolicy},
    signature::EventSignature,
};

#[cfg(feature = "human-readable")]
use crate::domain::metadata::EventMetadata;
use std::panic;
use std::sync::Arc;

/// Decision about how to handle an event.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LimitDecision {
    /// Allow the event to pass through
    Allow,
    /// Suppress the event
    Suppress,
}

/// Coordinates rate limiting decisions.
#[derive(Clone)]
pub struct RateLimiter<S>
where
    S: Storage<EventSignature, crate::application::registry::EventState> + Clone,
{
    registry: SuppressionRegistry<S>,
    metrics: Metrics,
    circuit_breaker: Arc<CircuitBreaker>,
}

impl<S> RateLimiter<S>
where
    S: Storage<EventSignature, crate::application::registry::EventState> + Clone,
{
    /// Create a new rate limiter.
    ///
    /// # Arguments
    /// * `registry` - The suppression registry (which contains the clock)
    /// * `metrics` - Metrics tracker
    /// * `circuit_breaker` - Circuit breaker for fail-safe operation
    pub fn new(
        registry: SuppressionRegistry<S>,
        metrics: Metrics,
        circuit_breaker: Arc<CircuitBreaker>,
    ) -> Self {
        Self {
            registry,
            metrics,
            circuit_breaker,
        }
    }

    /// Process an event and decide whether to allow or suppress it.
    ///
    /// # Arguments
    /// * `signature` - The event signature
    ///
    /// # Returns
    /// A `LimitDecision` indicating whether to allow or suppress the event.
    ///
    /// # Fail-Safe Behavior
    /// If rate limiting operations fail (circuit breaker open), this method fails open
    /// and allows all events through to preserve observability.
    ///
    /// # Performance
    /// This method is designed for the hot path:
    /// - Fast hash lookup in sharded map
    /// - Lock-free atomic operations where possible
    /// - No allocations in common case
    pub fn check_event(&self, signature: EventSignature) -> LimitDecision {
        // Check circuit breaker state
        if !self.circuit_breaker.allow_request() {
            // Circuit is open, fail open (allow all events)
            self.metrics.record_allowed();
            return LimitDecision::Allow;
        }

        // Attempt rate limiting operation with panic protection
        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
            self.registry.with_event_state(signature, |state, now| {
                // Ask the policy whether to allow this event
                let decision = state.policy.register_event(now);

                match decision {
                    PolicyDecision::Allow => LimitDecision::Allow,
                    PolicyDecision::Suppress => {
                        // Record the suppression
                        state.counter.record_suppression(now);
                        LimitDecision::Suppress
                    }
                }
            })
        }));

        let decision = match result {
            Ok(decision) => {
                // Operation succeeded
                self.circuit_breaker.record_success();
                decision
            }
            Err(_) => {
                // Operation panicked, record failure and fail open
                self.circuit_breaker.record_failure();
                LimitDecision::Allow
            }
        };

        // Record metrics
        match decision {
            LimitDecision::Allow => self.metrics.record_allowed(),
            LimitDecision::Suppress => self.metrics.record_suppressed(),
        }

        decision
    }

    /// Process an event with metadata and decide whether to allow or suppress it.
    ///
    /// This method captures event metadata on first occurrence for human-readable summaries.
    ///
    /// **Note:** Only available with the `human-readable` feature flag.
    ///
    /// # Arguments
    /// * `signature` - The event signature
    /// * `metadata` - Event details (level, message, target, fields)
    ///
    /// # Returns
    /// A `LimitDecision` indicating whether to allow or suppress the event.
    ///
    /// # Fail-Safe Behavior
    /// Same as `check_event`: fails open if rate limiting operations fail.
    #[cfg(feature = "human-readable")]
    pub fn check_event_with_metadata(
        &self,
        signature: EventSignature,
        metadata: EventMetadata,
    ) -> LimitDecision {
        // Check circuit breaker state
        if !self.circuit_breaker.allow_request() {
            // Circuit is open, fail open (allow all events)
            self.metrics.record_allowed();
            return LimitDecision::Allow;
        }

        // Attempt rate limiting operation with panic protection
        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
            self.registry.with_event_state(signature, |state, now| {
                // Capture metadata on first occurrence
                state.set_metadata(metadata);

                // Ask the policy whether to allow this event
                let decision = state.policy.register_event(now);

                match decision {
                    PolicyDecision::Allow => LimitDecision::Allow,
                    PolicyDecision::Suppress => {
                        // Record the suppression
                        state.counter.record_suppression(now);
                        LimitDecision::Suppress
                    }
                }
            })
        }));

        let decision = match result {
            Ok(decision) => {
                // Operation succeeded
                self.circuit_breaker.record_success();
                decision
            }
            Err(_) => {
                // Operation panicked, record failure and fail open
                self.circuit_breaker.record_failure();
                LimitDecision::Allow
            }
        };

        // Record metrics
        match decision {
            LimitDecision::Allow => self.metrics.record_allowed(),
            LimitDecision::Suppress => self.metrics.record_suppressed(),
        }

        decision
    }

    /// Get a reference to the registry.
    pub fn registry(&self) -> &SuppressionRegistry<S> {
        &self.registry
    }

    /// Get a reference to the metrics.
    pub fn metrics(&self) -> &Metrics {
        &self.metrics
    }

    /// Get a reference to the circuit breaker.
    pub fn circuit_breaker(&self) -> &Arc<CircuitBreaker> {
        &self.circuit_breaker
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::application::circuit_breaker::CircuitBreakerConfig;
    use crate::domain::policy::Policy;
    use crate::infrastructure::clock::SystemClock;
    use crate::infrastructure::mocks::MockClock;
    use crate::infrastructure::storage::ShardedStorage;
    use std::sync::Arc;
    use std::time::Instant;

    #[test]
    fn test_rate_limiter_basic() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(2).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let limiter = RateLimiter::new(registry, Metrics::new(), Arc::new(CircuitBreaker::new()));

        let sig = EventSignature::simple("INFO", "Test message");

        // First two events allowed
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);

        // Third and beyond suppressed
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);
    }

    #[test]
    fn test_rate_limiter_with_mock_clock() {
        use std::time::Duration;

        let storage = Arc::new(ShardedStorage::new());
        let mock_clock = Arc::new(MockClock::new(Instant::now()));
        let policy = Policy::time_window(2, Duration::from_secs(60)).unwrap();
        let registry = SuppressionRegistry::new(storage, mock_clock.clone(), policy);
        let limiter = RateLimiter::new(registry, Metrics::new(), Arc::new(CircuitBreaker::new()));

        let sig = EventSignature::simple("INFO", "Test");

        // First 2 allowed
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);

        // 3rd suppressed
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);

        // Advance time by 61 seconds
        mock_clock.advance(Duration::from_secs(61));

        // Should allow again
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);
    }

    #[test]
    fn test_rate_limiter_different_signatures() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(1).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let limiter = RateLimiter::new(registry, Metrics::new(), Arc::new(CircuitBreaker::new()));

        let sig1 = EventSignature::simple("INFO", "Message 1");
        let sig2 = EventSignature::simple("INFO", "Message 2");

        // Each signature has independent limits
        assert_eq!(limiter.check_event(sig1), LimitDecision::Allow);
        assert_eq!(limiter.check_event(sig2), LimitDecision::Allow);

        assert_eq!(limiter.check_event(sig1), LimitDecision::Suppress);
        assert_eq!(limiter.check_event(sig2), LimitDecision::Suppress);
    }

    #[test]
    fn test_rate_limiter_suppression_counting() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(1).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let limiter = RateLimiter::new(
            registry.clone(),
            Metrics::new(),
            Arc::new(CircuitBreaker::new()),
        );

        let sig = EventSignature::simple("INFO", "Test");

        // Allow first
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);

        // Suppress next 3
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);

        // Check counter - 3 suppressions recorded
        registry.with_event_state(sig, |state, _now| {
            assert_eq!(state.counter.count(), 3);
        });
    }

    #[test]
    fn test_concurrent_rate_limiting() {
        use std::thread;

        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(50).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let limiter = Arc::new(RateLimiter::new(
            registry,
            Metrics::new(),
            Arc::new(CircuitBreaker::new()),
        ));

        let sig = EventSignature::simple("INFO", "Concurrent test");
        let mut handles = vec![];

        for _ in 0..10 {
            let limiter_clone = Arc::clone(&limiter);
            let handle = thread::spawn(move || {
                let mut allowed = 0;
                let mut suppressed = 0;

                for _ in 0..20 {
                    match limiter_clone.check_event(sig) {
                        LimitDecision::Allow => allowed += 1,
                        LimitDecision::Suppress => suppressed += 1,
                    }
                }

                (allowed, suppressed)
            });
            handles.push(handle);
        }

        let mut total_allowed = 0;
        let mut total_suppressed = 0;

        for handle in handles {
            let (allowed, suppressed) = handle.join().unwrap();
            total_allowed += allowed;
            total_suppressed += suppressed;
        }

        // Total events = 10 threads * 20 events = 200
        assert_eq!(total_allowed + total_suppressed, 200);

        // Should have allowed at most 50 (policy limit)
        assert!(total_allowed <= 50);

        // Should have suppressed the rest
        assert!(total_suppressed >= 150);
    }

    #[test]
    fn test_fail_open_when_circuit_breaker_open() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(1).unwrap(); // Very restrictive
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::new());
        let limiter = RateLimiter::new(registry, Metrics::new(), cb.clone());

        let sig = EventSignature::simple("ERROR", "Critical failure");

        // First event allowed
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);

        // Open circuit breaker by recording many failures
        for _ in 0..10 {
            cb.record_failure();
        }
        assert!(!cb.allow_request(), "Circuit breaker should be open");

        // Even though policy would suppress (count exceeded),
        // circuit breaker being open causes fail-open behavior
        let decision = limiter.check_event(sig);
        assert_eq!(
            decision,
            LimitDecision::Allow,
            "Should fail open when circuit breaker is open"
        );

        // Verify metrics recorded as allowed
        assert_eq!(limiter.metrics().events_allowed(), 2);
    }

    #[test]
    fn test_fail_open_updates_metrics() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(1).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::new());
        let limiter = RateLimiter::new(registry, Metrics::new(), cb.clone());

        let sig = EventSignature::simple("ERROR", "Test");

        // Open the circuit breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        // Process multiple events while circuit is open
        for _ in 0..5 {
            limiter.check_event(sig);
        }

        // All should be recorded as allowed (fail-open)
        assert_eq!(limiter.metrics().events_allowed(), 5);
        assert_eq!(limiter.metrics().events_suppressed(), 0);
    }

    #[test]
    fn test_circuit_breaker_half_open_allows_some_requests() {
        use std::time::Duration;

        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(1).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::with_config(CircuitBreakerConfig {
            failure_threshold: 5,
            recovery_timeout: Duration::from_millis(10),
        }));
        let limiter = RateLimiter::new(registry, Metrics::new(), cb.clone());

        let sig = EventSignature::simple("ERROR", "Test");

        // Open circuit breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        // Wait for recovery timeout
        std::thread::sleep(Duration::from_millis(20));

        // Circuit should now be half-open
        // First request should be allowed through for testing
        let decision = limiter.check_event(sig);
        assert_eq!(decision, LimitDecision::Allow);

        // Since the operation succeeded, circuit breaker records success
        assert_eq!(cb.consecutive_failures(), 0);
    }

    #[test]
    fn test_normal_operation_after_circuit_breaker_closes() {
        use std::time::Duration;

        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(2).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::with_config(CircuitBreakerConfig {
            failure_threshold: 5,
            recovery_timeout: Duration::from_millis(10),
        }));
        let limiter = RateLimiter::new(registry, Metrics::new(), cb.clone());

        let sig = EventSignature::simple("INFO", "Test");

        // Open circuit breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        // Wait for recovery
        std::thread::sleep(Duration::from_millis(20));

        // Process events - should allow and record success
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);

        // Circuit breaker should be closed now
        assert_eq!(cb.consecutive_failures(), 0);

        // Normal rate limiting should work again
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress);
    }

    #[test]
    fn test_successful_operations_record_success_to_circuit_breaker() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(10).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::new());
        let limiter = RateLimiter::new(registry, Metrics::new(), cb.clone());

        let sig = EventSignature::simple("INFO", "Test");

        // Process events successfully
        for _ in 0..5 {
            limiter.check_event(sig);
        }

        // Circuit breaker should have no failures recorded
        assert_eq!(cb.consecutive_failures(), 0);
    }

    #[test]
    fn test_concurrent_fail_open_behavior() {
        use std::thread;

        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(5).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::new());
        let limiter = Arc::new(RateLimiter::new(registry, Metrics::new(), cb.clone()));

        // Open circuit breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        let sig = EventSignature::simple("ERROR", "Concurrent fail-open test");
        let mut handles = vec![];

        // Spawn multiple threads checking events while circuit is open
        for _ in 0..5 {
            let limiter_clone = Arc::clone(&limiter);
            let handle = thread::spawn(move || {
                let mut all_allowed = true;
                for _ in 0..10 {
                    if limiter_clone.check_event(sig) != LimitDecision::Allow {
                        all_allowed = false;
                    }
                }
                all_allowed
            });
            handles.push(handle);
        }

        // All threads should see only Allow decisions (fail-open)
        for handle in handles {
            assert!(
                handle.join().unwrap(),
                "All events should be allowed when circuit is open"
            );
        }

        // Total events = 5 threads * 10 events = 50
        // All should be allowed (fail-open)
        assert_eq!(limiter.metrics().events_allowed(), 50);
        assert_eq!(limiter.metrics().events_suppressed(), 0);
    }

    #[test]
    fn test_metrics_consistency_during_fail_open() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(2).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::new());
        let limiter = RateLimiter::new(registry, Metrics::new(), cb.clone());

        let sig = EventSignature::simple("INFO", "Test");

        // Normal operation
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow); // 1 allowed
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow); // 2 allowed
        assert_eq!(limiter.check_event(sig), LimitDecision::Suppress); // 1 suppressed

        // Open circuit breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        // Fail-open events
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow); // 3 allowed

        // Verify metrics are consistent
        let snapshot = limiter.metrics().snapshot();
        assert_eq!(snapshot.events_allowed, 3);
        assert_eq!(snapshot.events_suppressed, 1);
        assert_eq!(snapshot.total_events(), 4);
    }

    #[test]
    fn test_registry_state_unaffected_by_circuit_breaker() {
        let storage = Arc::new(ShardedStorage::new());
        let clock = Arc::new(SystemClock::new());
        let policy = Policy::count_based(1).unwrap();
        let registry = SuppressionRegistry::new(storage, clock, policy);
        let cb = Arc::new(CircuitBreaker::new());
        let limiter = RateLimiter::new(registry.clone(), Metrics::new(), cb.clone());

        let sig = EventSignature::simple("INFO", "Test");

        // Allow first event, establish state
        assert_eq!(limiter.check_event(sig), LimitDecision::Allow);

        // Verify state exists
        let initial_count = registry.with_event_state(sig, |state, _| state.counter.count());
        assert_eq!(initial_count, 0);

        // Open circuit breaker
        for _ in 0..10 {
            cb.record_failure();
        }

        // Process events while circuit is open (fail-open)
        for _ in 0..5 {
            limiter.check_event(sig);
        }

        // Registry state should NOT be modified during fail-open
        // (circuit breaker short-circuits before registry access)
        let final_count = registry.with_event_state(sig, |state, _| state.counter.count());
        assert_eq!(
            final_count, initial_count,
            "Registry state should not change during fail-open"
        );
    }
}