pub trait Algorithm:
Send
+ Sync
+ 'static {
// Required methods
fn name(&self) -> &'static str;
fn check_and_record<S: Storage>(
&self,
storage: &S,
key: &str,
quota: &Quota,
) -> impl Future<Output = Result<Decision>> + Send;
fn check<S: Storage>(
&self,
storage: &S,
key: &str,
quota: &Quota,
) -> impl Future<Output = Result<Decision>> + Send;
// Provided method
fn reset<S: Storage>(
&self,
storage: &S,
key: &str,
) -> impl Future<Output = Result<()>> + Send { ... }
}Expand description
Rate limiting algorithm trait.
Each algorithm provides different trade-offs between accuracy, memory usage, and burst handling. All implementations must be thread-safe.
§Algorithm Comparison
| Algorithm | Accuracy | Memory | Burst | Best For |
|---|---|---|---|---|
| GCRA | Highest | Low (1 timestamp) | Controlled | Precise rate control |
| Token Bucket | High | Low | Excellent | Bursty traffic |
| Leaky Bucket | High | Medium | None | Smooth output |
| Sliding Log | Highest | High | Good | Precision critical |
| Sliding Window | Medium | Low | Good | General purpose |
| Fixed Window | Low | Low | Poor | Simple use cases |
| Concurrent | N/A | Low | N/A | Limit parallelism |
Required Methods§
Sourcefn check_and_record<S: Storage>(
&self,
storage: &S,
key: &str,
quota: &Quota,
) -> impl Future<Output = Result<Decision>> + Send
fn check_and_record<S: Storage>( &self, storage: &S, key: &str, quota: &Quota, ) -> impl Future<Output = Result<Decision>> + Send
Check if a request is allowed AND record it atomically.
This is the primary method for rate limiting. It checks whether the request should be allowed and, if so, records it against the quota.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.