RateLimitConfig

Struct RateLimitConfig 

Source
pub struct RateLimitConfig {
Show 13 fields pub enabled: bool, pub default_rpm: u32, pub burst_capacity: u32, pub method_limits: HashMap<String, MethodLimit>, pub client_limits: HashMap<String, ClientLimit>, pub cleanup_interval_secs: u64, pub adaptive: bool, pub threat_penalty_multiplier: f32, pub whitelist: HashSet<String>, pub blacklist: HashSet<String>, pub ip_limits: HashMap<String, IpLimit>, pub global_rpm: Option<u32>, pub track_by: TrackingMethod,
}
Expand description

Rate limiting configuration

§Security Implications

Rate limiting is essential for preventing abuse and DoS attacks:

  • Prevents brute force attacks - Limits authentication attempts
  • Protects against resource exhaustion - Controls request rates
  • Mitigates data harvesting - Slows down automated scraping
  • Adaptive penalties - Automatically restricts suspicious clients

§Example: Secure Production Configuration

[rate_limit]
enabled = true
default_rpm = 60           # 1 request per second average
burst_capacity = 10        # Allow short bursts
cleanup_interval_secs = 300
adaptive = true            # Auto-adjust based on threats
threat_penalty_multiplier = 0.5  # Halve limits for threats

[rate_limit.method_limits]
"tools/list" = { rpm = 120, burst = 20 }     # Read operations
"tools/call" = { rpm = 30, burst = 5 }       # Execution operations
"security/neutralize" = { rpm = 10, burst = 2 }  # Sensitive operations

[rate_limit.client_limits]
"trusted-app" = { rpm = 300, burst = 50, priority = "high" }
"public-api" = { rpm = 30, burst = 5, priority = "low" }

Fields§

§enabled: bool

Enable rate limiting

Default: false (for easier testing) Security: MUST be true in production to prevent abuse. Without rate limiting, attackers can overwhelm the service. Warning: Disabling exposes you to DoS and brute force attacks

§default_rpm: u32

Default requests per minute

Default: 60 (1 per second average) Security: Lower values are more secure but may impact usability. Consider your threat model and legitimate usage patterns. Range: 10-600 (recommend 30-120 for most APIs)

§burst_capacity: u32

Burst capacity (tokens available immediately)

Default: 10 Security: Allows legitimate burst traffic while preventing abuse. Too high enables rapid attacks; too low impacts user experience. Range: 1-50 (recommend 5-20, should be < default_rpm/6)

§method_limits: HashMap<String, MethodLimit>

Per-method rate limits (overrides default)

Default: Sensible limits for common operations Security: Set stricter limits on sensitive operations. Read operations can have higher limits than write operations. Best Practice: Order from least to most sensitive

§client_limits: HashMap<String, ClientLimit>

Per-client rate limits (by client ID)

Default: Empty (all clients use default limits) Security: Assign higher limits only to trusted clients. Use priority levels to ensure critical clients aren’t blocked. Warning: Overly generous limits can be exploited

§cleanup_interval_secs: u64

Clean up interval for expired buckets (seconds)

Default: 300 (5 minutes) Security: Regular cleanup prevents memory exhaustion. Shorter intervals use more CPU but free memory faster. Range: 60-3600 (recommend 300-900)

§adaptive: bool

Enable adaptive rate limiting based on load

Default: false Security: Automatically tightens limits under attack. Reduces false positives during traffic spikes. Trade-off: Adds complexity but improves resilience

§threat_penalty_multiplier: f32

Penalty for security threats (multiplier)

Default: 0.5 (halve the rate limit) Security: Clients triggering security alerts get reduced limits. Helps contain attacks while allowing recovery for false positives. Range: 0.1-1.0 (0.1 = 90% reduction, 1.0 = no penalty)

§whitelist: HashSet<String>

Whitelist of client IDs exempt from rate limiting

Default: Empty set Security: Only whitelist fully trusted internal clients. Whitelisted clients can still trigger other security measures. Warning: Use sparingly - prefer higher rate limits over exemption

§blacklist: HashSet<String>

Blacklist of client IDs to always block

Default: Empty set Security: Immediately reject requests from blacklisted clients. Useful for blocking known malicious actors or compromised credentials. Note: Blacklist takes precedence over whitelist

§ip_limits: HashMap<String, IpLimit>

IP-specific rate limits

Default: Empty map Security: Set stricter limits for suspicious IP ranges. Useful for geographic restrictions or known problematic networks. Format: IP address or CIDR notation as key

§global_rpm: Option<u32>

Global requests per minute limit across all clients

Default: None (no global limit) Security: Prevents total system overload regardless of client distribution. Individual client limits still apply within the global limit. Range: 100-10000 (recommend 10x expected peak traffic)

§track_by: TrackingMethod

Method for tracking clients

Default: ClientId Security: Determines how rate limits are applied. IP-based tracking is more strict but may affect legitimate shared IPs. Options: ClientId, IpAddress, Combined (both must pass)

Trait Implementations§

Source§

impl Clone for RateLimitConfig

Source§

fn clone(&self) -> RateLimitConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RateLimitConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RateLimitConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for RateLimitConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for RateLimitConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,