hyperlane-plugin-rate-limit 22.12.0

A 64-way sharded lock-free atomic GCRA and fixed-window rate limiter plugin for the Hyperlane framework with extensible KeyExtractor and RateLimitStore traits and RFC RateLimit headers.
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
use super::*;

/// Default construction for [`RateLimitPolicy`].
impl Default for RateLimitPolicy {
    /// Constructs a default [`RateLimitPolicy`] (100 req / 60 s, GCRA, 65 536 max keys).
    fn default() -> Self {
        Self {
            requests_per_window: DEFAULT_RATE_LIMIT_REQUESTS,
            window_ms: DEFAULT_RATE_LIMIT_WINDOW_MS,
            burst: DEFAULT_RATE_LIMIT_BURST,
            algorithm: RateLimitAlgorithm::Gcra,
            max_keys: DEFAULT_RATE_LIMIT_MAX_KEYS,
            include_headers: true,
            exempt_paths: Vec::new(),
        }
    }
}

/// Inherent fluent builders for [`RateLimitPolicy`].
impl RateLimitPolicy {
    /// Constructs a policy with `requests_per_window` allowed over `window_ms`.
    ///
    /// # Arguments
    ///
    /// - `u64` - Sustained requests per window.
    /// - `u64` - Window duration in milliseconds.
    ///
    /// # Returns
    ///
    /// - `Self` - Constructed rate-limit policy.
    pub fn new(requests_per_window: u64, window_ms: u64) -> Self {
        let count: u64 = requests_per_window.max(1);
        Self {
            requests_per_window: count,
            window_ms: window_ms.max(1),
            burst: count,
            algorithm: RateLimitAlgorithm::Gcra,
            max_keys: DEFAULT_RATE_LIMIT_MAX_KEYS,
            include_headers: true,
            exempt_paths: Vec::new(),
        }
    }

    /// Sets the burst allowance for GCRA enforcement.
    ///
    /// # Arguments
    ///
    /// - `u64` - Maximum instantaneous burst capacity.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated policy.
    pub fn with_burst(mut self, burst: u64) -> Self {
        self.set_burst(burst.max(1));
        self
    }

    /// Sets the rate-limiting algorithm (`Gcra` or `FixedWindow`).
    ///
    /// # Arguments
    ///
    /// - `RateLimitAlgorithm` - Desired enforcement algorithm.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated policy.
    pub fn with_algorithm(mut self, algorithm: RateLimitAlgorithm) -> Self {
        self.set_algorithm(algorithm);
        self
    }

    /// Sets the maximum tracked key capacity across all 64 shards.
    ///
    /// # Arguments
    ///
    /// - `usize` - Maximum number of concurrent tracked keys.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated policy.
    pub fn with_max_keys(mut self, max_keys: usize) -> Self {
        self.set_max_keys(max_keys.max(1));
        self
    }

    /// Enables or disables `RateLimit-*` response headers.
    ///
    /// # Arguments
    ///
    /// - `bool` - Whether to attach headers.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated policy.
    pub fn with_include_headers(mut self, include_headers: bool) -> Self {
        self.set_include_headers(include_headers);
        self
    }

    /// Sets exact request paths exempted from rate limiting.
    ///
    /// # Arguments
    ///
    /// - `I` - Iterator of exact exempt paths.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated policy.
    pub fn with_exempt_paths<I, S>(mut self, paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.set_exempt_paths(paths.into_iter().map(Into::into).collect());
        self
    }
}

/// Inherent implementation of [`BucketEntry`].
impl BucketEntry {
    /// Creates a fresh zero-initialized atomic bucket entry.
    ///
    /// # Arguments
    ///
    /// - `u64` - Current timestamp in nanoseconds since limiter epoch.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized atomic bucket.
    pub(crate) fn new(now_ns: u64) -> Self {
        Self {
            primary: AtomicU64::new(0),
            counter: AtomicU64::new(0),
            last_seen_ns: AtomicU64::new(now_ns),
        }
    }

    /// Evaluates one GCRA request atomically using a lock-free CAS loop.
    ///
    /// # Arguments
    ///
    /// - `u64` - Current elapsed nanoseconds since limiter epoch.
    /// - `u64` - Emission interval in nanoseconds per token.
    /// - `u64` - Burst tolerance in nanoseconds (`emission_interval_ns * burst`).
    /// - `u64` - Effective burst limit.
    ///
    /// # Returns
    ///
    /// - `RateLimitDecision` - Admission decision and remaining tokens.
    pub(crate) fn check_gcra(
        &self,
        now_ns: u64,
        emission_interval_ns: u64,
        burst_tolerance_ns: u64,
        limit: u64,
    ) -> RateLimitDecision {
        let Self {
            primary,
            last_seen_ns,
            ..
        } = self;
        last_seen_ns.store(now_ns, Ordering::Relaxed);
        let max_tat: u64 = now_ns.saturating_add(burst_tolerance_ns);
        let mut observed: u64 = primary.load(Ordering::Relaxed);
        loop {
            let base: u64 = observed.max(now_ns);
            let next_tat: u64 = base.saturating_add(emission_interval_ns);
            if next_tat > max_tat {
                let wait_ns: u64 = next_tat.saturating_sub(max_tat);
                let reset_ns: u64 = observed.saturating_sub(now_ns);
                return RateLimitDecision {
                    allowed: false,
                    limit,
                    remaining: 0,
                    reset_after_secs: ceil_nanos_to_secs(reset_ns),
                    retry_after_secs: ceil_nanos_to_secs(wait_ns).max(1),
                };
            }
            match primary.compare_exchange_weak(
                observed,
                next_tat,
                Ordering::AcqRel,
                Ordering::Relaxed,
            ) {
                Ok(_) => {
                    let headroom_ns: u64 = max_tat.saturating_sub(next_tat);
                    let remaining: u64 = headroom_ns / emission_interval_ns;
                    let reset_ns: u64 = next_tat.saturating_sub(now_ns);
                    return RateLimitDecision {
                        allowed: true,
                        limit,
                        remaining,
                        reset_after_secs: ceil_nanos_to_secs(reset_ns),
                        retry_after_secs: 0,
                    };
                }
                Err(actual) => observed = actual,
            }
        }
    }

    /// Evaluates one fixed-window request atomically without locks.
    ///
    /// # Arguments
    ///
    /// - `u64` - Current elapsed nanoseconds since limiter epoch.
    /// - `u64` - Window size in nanoseconds.
    /// - `u64` - Maximum requests permitted in one window.
    ///
    /// # Returns
    ///
    /// - `RateLimitDecision` - Admission decision and remaining quota.
    pub(crate) fn check_fixed_window(
        &self,
        now_ns: u64,
        window_ns: u64,
        limit: u64,
    ) -> RateLimitDecision {
        let Self {
            primary,
            counter,
            last_seen_ns,
        } = self;
        last_seen_ns.store(now_ns, Ordering::Relaxed);
        let current_win: u64 = (now_ns / window_ns).saturating_add(1);
        let elapsed_in_win: u64 = now_ns % window_ns;
        let reset_ns: u64 = window_ns.saturating_sub(elapsed_in_win);
        let reset_secs: u64 = ceil_nanos_to_secs(reset_ns).max(1);
        let observed_win: u64 = primary.load(Ordering::Acquire);
        if observed_win != current_win
            && primary
                .compare_exchange(
                    observed_win,
                    current_win,
                    Ordering::AcqRel,
                    Ordering::Acquire,
                )
                .is_ok()
        {
            counter.store(1, Ordering::Release);
            return RateLimitDecision {
                allowed: true,
                limit,
                remaining: limit.saturating_sub(1),
                reset_after_secs: reset_secs,
                retry_after_secs: 0,
            };
        }
        let mut cur: u64 = counter.load(Ordering::Relaxed);
        loop {
            if cur >= limit {
                return RateLimitDecision {
                    allowed: false,
                    limit,
                    remaining: 0,
                    reset_after_secs: reset_secs,
                    retry_after_secs: reset_secs,
                };
            }
            match counter.compare_exchange_weak(cur, cur + 1, Ordering::AcqRel, Ordering::Relaxed) {
                Ok(_) => {
                    return RateLimitDecision {
                        allowed: true,
                        limit,
                        remaining: limit.saturating_sub(cur + 1),
                        reset_after_secs: reset_secs,
                        retry_after_secs: 0,
                    };
                }
                Err(actual) => cur = actual,
            }
        }
    }
}

/// Inherent implementation of [`ShardedRateLimiter`].
impl ShardedRateLimiter {
    /// Builds a 64-way sharded lock-free [`ShardedRateLimiter`] governed by `policy`.
    ///
    /// # Arguments
    ///
    /// - `RateLimitPolicy` - Quota, window, burst, algorithm, and memory cap settings.
    ///
    /// # Returns
    ///
    /// - `Self` - Ready rate limiter.
    pub fn new(policy: RateLimitPolicy) -> Self {
        let window_ns: u64 = policy.get_window_ms().max(1).saturating_mul(NANOS_PER_MS);
        let rpw: u64 = policy.get_requests_per_window().max(1);
        let burst: u64 = policy.get_burst().max(1);
        let emission_interval_ns: u64 = (window_ns / rpw).max(1);
        let burst_tolerance_ns: u64 = emission_interval_ns.saturating_mul(burst);
        let shards: RateLimitShardArray =
            Box::new(from_fn(|_: usize| RwLock::new(hash_map_xx_hash3_64())));
        Self {
            inner: Arc::new(ShardedRateLimiterInner {
                policy,
                epoch: Instant::now(),
                emission_interval_ns,
                burst_tolerance_ns,
                window_ns,
                key_count: AtomicUsize::new(0),
                shards,
            }),
        }
    }

    /// Returns the number of currently tracked distinct keys in `O(1)` time without locking.
    ///
    /// # Returns
    ///
    /// - `usize` - Active tracked key count.
    pub fn tracked_keys(&self) -> usize {
        let Self { inner } = self;
        inner.key_count.load(Ordering::Relaxed)
    }

    /// Looks up or creates the [`BucketEntry`] for `key` with read-fast sharding and bounded capacity.
    ///
    /// # Arguments
    ///
    /// - `&str` - Bucket partition key.
    /// - `u64` - Current timestamp in nanoseconds since epoch.
    ///
    /// # Returns
    ///
    /// - `Option<Arc<BucketEntry>>` - Bucket entry, or `None` when table is full of active keys.
    fn get_or_insert_bucket(&self, key: &str, now_ns: u64) -> Option<Arc<BucketEntry>> {
        let Self { inner } = self;
        let idx: usize = rate_limit_shard_index(key);
        if let Ok(read_map) = inner.shards[idx].read()
            && let Some(existing) = read_map.get(key)
        {
            return Some(Arc::clone(existing));
        }
        let max_keys: usize = inner.policy.get_max_keys();
        if inner.key_count.load(Ordering::Relaxed) >= max_keys {
            let _: usize = self.purge_expired();
        }
        let mut write_map: RwLockWriteGuard<'_, HashMapXxHash3_64<String, Arc<BucketEntry>>> =
            inner.shards[idx].write().ok()?;
        if let Some(existing) = write_map.get(key) {
            return Some(Arc::clone(existing));
        }
        if inner.key_count.load(Ordering::Relaxed) >= max_keys {
            let stale_cutoff: u64 = now_ns.saturating_sub(inner.window_ns);
            let before: usize = write_map.len();
            write_map.retain(|_: &String, entry: &mut Arc<BucketEntry>| {
                entry.last_seen_ns.load(Ordering::Relaxed) > stale_cutoff
            });
            let removed: usize = before.saturating_sub(write_map.len());
            if removed > 0 {
                inner.key_count.fetch_sub(removed, Ordering::Relaxed);
            }
            if inner.key_count.load(Ordering::Relaxed) >= max_keys {
                return None;
            }
        }
        let created: Arc<BucketEntry> = Arc::new(BucketEntry::new(now_ns));
        write_map.insert(key.to_owned(), Arc::clone(&created));
        inner.key_count.fetch_add(1, Ordering::Relaxed);
        Some(created)
    }
}

/// Implements [`RateLimitStore`] for [`ShardedRateLimiter`].
impl RateLimitStore for ShardedRateLimiter {
    /// Evaluates one request against `key` using the configured lock-free algorithm.
    ///
    /// # Arguments
    ///
    /// - `&str` - Bucket partition key.
    ///
    /// # Returns
    ///
    /// - `RateLimitDecision` - Admission decision and remaining quota.
    fn check(&self, key: &str) -> RateLimitDecision {
        let Self { inner } = self;
        let now_ns: u64 = Instant::now()
            .duration_since(inner.epoch)
            .as_nanos()
            .min(u64::MAX as u128) as u64;
        let Some(entry) = self.get_or_insert_bucket(key, now_ns) else {
            let limit: u64 = inner.policy.get_requests_per_window();
            let reset_secs: u64 = ceil_nanos_to_secs(inner.window_ns).max(1);
            return RateLimitDecision {
                allowed: false,
                limit,
                remaining: 0,
                reset_after_secs: reset_secs,
                retry_after_secs: reset_secs,
            };
        };
        match inner.policy.get_algorithm() {
            RateLimitAlgorithm::Gcra => entry.check_gcra(
                now_ns,
                inner.emission_interval_ns,
                inner.burst_tolerance_ns,
                inner.policy.get_burst().max(1),
            ),
            RateLimitAlgorithm::FixedWindow => entry.check_fixed_window(
                now_ns,
                inner.window_ns,
                inner.policy.get_requests_per_window().max(1),
            ),
        }
    }

    /// Evicts entries whose idle age exceeds the configured window duration across all 64 shards.
    ///
    /// # Returns
    ///
    /// - `usize` - Total number of stale keys evicted.
    fn purge_expired(&self) -> usize {
        let Self { inner } = self;
        let now_ns: u64 = Instant::now()
            .duration_since(inner.epoch)
            .as_nanos()
            .min(u64::MAX as u128) as u64;
        let stale_cutoff: u64 =
            now_ns.saturating_sub(inner.window_ns.max(inner.burst_tolerance_ns));
        let mut evicted: usize = 0;
        for shard in inner.shards.iter() {
            let needs_evict: bool = shard
                .read()
                .map(
                    |map: RwLockReadGuard<'_, HashMapXxHash3_64<String, Arc<BucketEntry>>>| {
                        !map.is_empty()
                    },
                )
                .unwrap_or(false);
            if !needs_evict {
                continue;
            }
            if let Ok(mut map) = shard.write() {
                let before: usize = map.len();
                map.retain(|_: &String, entry: &mut Arc<BucketEntry>| {
                    entry.last_seen_ns.load(Ordering::Relaxed) > stale_cutoff
                });
                evicted += before.saturating_sub(map.len());
            }
        }
        if evicted > 0 {
            inner.key_count.fetch_sub(evicted, Ordering::Relaxed);
        }
        evicted
    }
}

/// Inherent constructors for built-in key extractors.
impl ClientIpKeyExtractor {
    /// Creates a [`ClientIpKeyExtractor`] choosing whether to trust `X-Forwarded-For` / `X-Real-Ip`.
    ///
    /// # Arguments
    ///
    /// - `bool` - `true` when behind a trusted reverse proxy.
    ///
    /// # Returns
    ///
    /// - `Self` - Constructed key extractor.
    pub fn new(trust_proxy_headers: bool) -> Self {
        Self {
            trust_proxy_headers,
        }
    }
}

/// Implements [`KeyExtractor`] for [`ClientIpKeyExtractor`].
impl KeyExtractor for ClientIpKeyExtractor {
    /// Extracts the client IP address as the bucket key.
    ///
    /// # Arguments
    ///
    /// - `&Stream` - Transport stream.
    /// - `&Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `String` - Client IP partition key.
    fn extract(&self, stream: &Stream, ctx: &Context) -> String {
        resolve_client_ip(stream, ctx, self.get_trust_proxy_headers())
    }
}

/// Inherent constructors for [`HeaderKeyExtractor`].
impl HeaderKeyExtractor {
    /// Creates a [`HeaderKeyExtractor`] reading `header_name` with optional client-IP fallback.
    ///
    /// # Arguments
    ///
    /// - `H` - Header name convertible into `String`.
    /// - `bool` - Whether to fall back to client IP when the header is absent.
    ///
    /// # Returns
    ///
    /// - `Self` - Constructed key extractor.
    pub fn new<H>(header_name: H, fallback_to_ip: bool) -> Self
    where
        H: Into<String>,
    {
        Self {
            header_name: header_name.into(),
            fallback_to_ip,
        }
    }
}

/// Implements [`KeyExtractor`] for [`HeaderKeyExtractor`].
impl KeyExtractor for HeaderKeyExtractor {
    /// Extracts the configured request header value (or client IP fallback) as the bucket key.
    ///
    /// # Arguments
    ///
    /// - `&Stream` - Transport stream.
    /// - `&Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `String` - Header-derived partition key.
    fn extract(&self, stream: &Stream, ctx: &Context) -> String {
        if let Some(val) = ctx
            .get_request()
            .try_get_header_back(self.get_header_name())
        {
            let trimmed: &str = val.trim();
            if !trimmed.is_empty() {
                return trimmed.to_owned();
            }
        }
        if self.get_fallback_to_ip() {
            resolve_client_ip(stream, ctx, false)
        } else {
            FALLBACK_CLIENT_KEY.to_owned()
        }
    }
}

/// Inherent constructors for [`PathAndIpKeyExtractor`].
impl PathAndIpKeyExtractor {
    /// Creates a [`PathAndIpKeyExtractor`] combining request path and client IP.
    ///
    /// # Arguments
    ///
    /// - `bool` - `true` when forwarding proxy headers are trusted.
    ///
    /// # Returns
    ///
    /// - `Self` - Constructed key extractor.
    pub fn new(trust_proxy_headers: bool) -> Self {
        Self {
            trust_proxy_headers,
        }
    }
}

/// Implements [`KeyExtractor`] for [`PathAndIpKeyExtractor`].
impl KeyExtractor for PathAndIpKeyExtractor {
    /// Extracts `"<path>|<client_ip>"` as the partition key.
    ///
    /// # Arguments
    ///
    /// - `&Stream` - Transport stream.
    /// - `&Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `String` - Composite path-and-IP partition key.
    fn extract(&self, stream: &Stream, ctx: &Context) -> String {
        let path: &str = ctx.get_request().get_path();
        let ip: String = resolve_client_ip(stream, ctx, self.get_trust_proxy_headers());
        let mut out: String = String::with_capacity(path.len() + ip.len() + 1);
        out.push_str(path);
        out.push('|');
        out.push_str(&ip);
        out
    }
}

/// Inherent implementation of [`RateLimitPlugin`].
impl RateLimitPlugin {
    /// Initializes the global [`RateLimitPlugin`] with the default 64-shard limiter and client-IP extractor.
    ///
    /// # Arguments
    ///
    /// - `RateLimitPolicy` - Quota and algorithm policy.
    ///
    /// # Returns
    ///
    /// - `&'static Self` - Reference to the initialized global plugin.
    pub fn init(policy: RateLimitPolicy) -> &'static Self {
        let store: SharedRateLimitStore = Arc::new(ShardedRateLimiter::new(policy.clone()));
        let key_extractor: SharedKeyExtractor = Arc::new(ClientIpKeyExtractor::new(false));
        Self::init_custom(policy, store, key_extractor)
    }

    /// Initializes the global [`RateLimitPlugin`] with custom [`RateLimitStore`] and [`KeyExtractor`].
    ///
    /// # Arguments
    ///
    /// - `RateLimitPolicy` - Quota header and exempt-path policy.
    /// - `SharedRateLimitStore` - Custom or distributed backend store.
    /// - `SharedKeyExtractor` - Custom partition key extractor.
    ///
    /// # Returns
    ///
    /// - `&'static Self` - Reference to the initialized global plugin.
    pub fn init_custom(
        policy: RateLimitPolicy,
        store: SharedRateLimitStore,
        key_extractor: SharedKeyExtractor,
    ) -> &'static Self {
        RATE_LIMIT_PLUGIN.get_or_init(|| Self {
            policy,
            store,
            key_extractor,
        })
    }

    /// Returns the initialized global [`RateLimitPlugin`], if initialized.
    ///
    /// # Returns
    ///
    /// - `Option<&'static Self>` - Reference to the global plugin, or `None`.
    pub fn get() -> Option<&'static Self> {
        RATE_LIMIT_PLUGIN.get()
    }

    /// Returns the active [`RateLimitPolicy`].
    ///
    /// # Returns
    ///
    /// - `&RateLimitPolicy` - Active policy.
    pub fn get_policy(&self) -> &RateLimitPolicy {
        &self.policy
    }

    /// Returns the active [`SharedRateLimitStore`].
    ///
    /// # Returns
    ///
    /// - `&SharedRateLimitStore` - Active backend store.
    pub fn get_store(&self) -> &SharedRateLimitStore {
        &self.store
    }

    /// Returns the active [`SharedKeyExtractor`].
    ///
    /// # Returns
    ///
    /// - `&SharedKeyExtractor` - Active key extractor.
    pub fn get_key_extractor(&self) -> &SharedKeyExtractor {
        &self.key_extractor
    }

    /// Evaluates one request against `key` and attaches standard `RateLimit-*` response headers.
    ///
    /// # Arguments
    ///
    /// - `&str` - Partition key.
    /// - `&mut Context` - Request and response context.
    ///
    /// # Returns
    ///
    /// - `RateLimitDecision` - Quota decision for this request.
    pub fn evaluate_key(&self, key: &str, ctx: &mut Context) -> RateLimitDecision {
        let decision: RateLimitDecision = self.get_store().check(key);
        let response: &mut Response = ctx.get_mut_response();
        if self.get_policy().get_include_headers() {
            response
                .set_header(HEADER_RATE_LIMIT_LIMIT, decision.get_limit().to_string())
                .set_header(
                    HEADER_RATE_LIMIT_REMAINING,
                    decision.get_remaining().to_string(),
                )
                .set_header(
                    HEADER_RATE_LIMIT_RESET,
                    decision.get_reset_after_secs().to_string(),
                );
        }
        if !decision.get_allowed() {
            response
                .set_status_code(HttpStatus::TooManyRequests.code())
                .set_header(RETRY_AFTER, decision.get_retry_after_secs().to_string())
                .set_body(Vec::new());
        }
        decision
    }
}

/// Implements [`ServerHook`] for [`RateLimitHook`].
impl ServerHook for RateLimitHook {
    /// Constructs a per-request [`RateLimitHook`].
    ///
    /// # Arguments
    ///
    /// - `&mut Stream` - Connection stream.
    /// - `&mut Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `Self` - Hook instance.
    async fn new(_: &mut Stream, _: &mut Context) -> Self {
        Self
    }

    /// Enforces the configured rate limit and rejects excess traffic with `429 Too Many Requests`.
    ///
    /// # Arguments
    ///
    /// - `Self` - Hook instance.
    /// - `&mut Stream` - Connection stream.
    /// - `&mut Context` - Request context.
    ///
    /// # Returns
    ///
    /// - `Status` - `Status::Continue` when unconfigured, exempt, or admitted; `Status::Reject` when throttled.
    async fn handle(self, stream: &mut Stream, ctx: &mut Context) -> Status {
        let Some(plugin) = RateLimitPlugin::get() else {
            return Status::Continue;
        };
        let path: &str = ctx.get_request().get_path();
        if plugin
            .get_policy()
            .get_exempt_paths()
            .iter()
            .any(|exempt: &String| exempt == path)
        {
            return Status::Continue;
        }
        let key: String = plugin.get_key_extractor().extract(stream, ctx);
        let decision: RateLimitDecision = plugin.evaluate_key(&key, ctx);
        if decision.get_allowed() {
            return Status::Continue;
        }
        let wire: ResponseData = ctx.get_mut_response().build();
        let _: Result<(), ResponseError> = stream.try_send(&wire).await;
        Status::Reject
    }
}