pub struct ActionRateLimiter { /* private fields */ }Expand description
Per-action-type sliding-window rate limiter.
Tracks timestamps of recent action invocations and enforces a maximum
number of calls within a configurable time window. Each action type
(e.g. "tool_call", "web_access") has its own independent window.
§Thread Safety
All methods acquire the internal RwLock and are safe to call from
multiple threads or async tasks.
Implementations§
Source§impl ActionRateLimiter
impl ActionRateLimiter
Sourcepub fn new(default_limit: u32, window: Duration) -> Self
pub fn new(default_limit: u32, window: Duration) -> Self
Create a new rate limiter with the given default limit and window duration.
Sourcepub fn with_limits(
default_limit: u32,
window: Duration,
limits: HashMap<String, u32>,
) -> Self
pub fn with_limits( default_limit: u32, window: Duration, limits: HashMap<String, u32>, ) -> Self
Create a rate limiter with per-action-type overrides.
Sourcepub fn check_rate_limit(
&self,
action_type: &str,
) -> Result<(), RateLimitExceeded>
pub fn check_rate_limit( &self, action_type: &str, ) -> Result<(), RateLimitExceeded>
Check the rate limit for an action type and record the action if allowed.
Returns Ok(()) if the action is within the limit, or
Err(RateLimitExceeded) if the limit has been reached.
Sourcepub fn record_action(&self, action_type: &str)
pub fn record_action(&self, action_type: &str)
Record an action without checking the rate limit.
Useful for tracking actions that have already been validated by other means.
Sourcepub fn remaining(&self, action_type: &str) -> u32
pub fn remaining(&self, action_type: &str) -> u32
Return the number of remaining calls allowed for an action type within the current window.
Sourcepub fn window_duration(&self) -> Duration
pub fn window_duration(&self) -> Duration
Return the configured window duration.
Sourcepub fn default_limit(&self) -> u32
pub fn default_limit(&self) -> u32
Return the default rate limit.