Skip to main content

RateLimiterBackend

Trait RateLimiterBackend 

Source
pub trait RateLimiterBackend:
    Send
    + Sync
    + 'static {
    // Required methods
    fn check<'a>(
        &'a self,
        bucket_key: &'a str,
        config: &'a RateLimitConfig,
    ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult>> + Send + 'a>>;
    fn build_key(
        &self,
        key_type: RateLimitKey,
        action_name: &str,
        auth: &AuthContext,
        request: &RequestMetadata,
    ) -> String;

    // Provided method
    fn enforce<'a>(
        &'a self,
        bucket_key: &'a str,
        config: &'a RateLimitConfig,
    ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult>> + Send + 'a>> { ... }
}
Expand description

Pluggable rate-limiter implementation.

The runtime exposes two implementations:

  • HybridRateLimiter: per-node DashMap fast path with PG fallback for Global keys. Approximate under multi-node deployments — user/IP limits multiply by the node count. Right for DDoS protection.
  • StrictRateLimiter: every check round-trips to PostgreSQL. Cluster-wide correct. Right for billing-grade or quota enforcement.

Both ship with the framework. Users implement this trait themselves only when their backing store sits outside the runtime’s PG-only contract.

Required Methods§

Source

fn check<'a>( &'a self, bucket_key: &'a str, config: &'a RateLimitConfig, ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult>> + Send + 'a>>

Check whether a single token is available for the given bucket.

Source

fn build_key( &self, key_type: RateLimitKey, action_name: &str, auth: &AuthContext, request: &RequestMetadata, ) -> String

Build the bucket key string for a (key kind, action, auth, request) tuple.

Provided Methods§

Source

fn enforce<'a>( &'a self, bucket_key: &'a str, config: &'a RateLimitConfig, ) -> Pin<Box<dyn Future<Output = Result<RateLimitResult>> + Send + 'a>>

Check and convert a denial into a ForgeError::RateLimitExceeded.

Implementors§