hyperlane_plugin_rate_limit/trait.rs
1use super::*;
2
3/// Extracts the rate-limiting partition key (for example client IP, API key, or user + route).
4pub trait KeyExtractor: Send + Sync + 'static {
5 /// Computes the bucket key for an incoming request.
6 ///
7 /// # Arguments
8 ///
9 /// - `&Stream` - Underlying transport stream (for peer socket address inspection).
10 /// - `&Context` - Request and response context.
11 ///
12 /// # Returns
13 ///
14 /// - `String` - Partition key identifying the rate-limit bucket.
15 fn extract(&self, stream: &Stream, ctx: &Context) -> String;
16}
17
18/// Pluggable rate-limit decision backend (in-memory sharded or external distributed store).
19pub trait RateLimitStore: Send + Sync + 'static {
20 /// Evaluates and records one request against `key`.
21 ///
22 /// # Arguments
23 ///
24 /// - `&str` - Bucket partition key.
25 ///
26 /// # Returns
27 ///
28 /// - `RateLimitDecision` - Admission decision and remaining quota metadata.
29 fn check(&self, key: &str) -> RateLimitDecision;
30
31 /// Evicts stale expired bucket entries to bound memory usage.
32 ///
33 /// # Returns
34 ///
35 /// - `usize` - Number of evicted entries.
36 fn purge_expired(&self) -> usize {
37 0
38 }
39}