Skip to main content

ActionRateLimiter

Struct ActionRateLimiter 

Source
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

Source

pub fn new(default_limit: u32, window: Duration) -> Self

Create a new rate limiter with the given default limit and window duration.

Source

pub fn with_limits( default_limit: u32, window: Duration, limits: HashMap<String, u32>, ) -> Self

Create a rate limiter with per-action-type overrides.

Source

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.

Source

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.

Source

pub fn remaining(&self, action_type: &str) -> u32

Return the number of remaining calls allowed for an action type within the current window.

Source

pub fn reset(&self, action_type: &str)

Reset the sliding window for a specific action type.

Source

pub fn window_duration(&self) -> Duration

Return the configured window duration.

Source

pub fn default_limit(&self) -> u32

Return the default rate limit.

Trait Implementations§

Source§

impl Debug for ActionRateLimiter

Source§

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

Formats the value using the given formatter. 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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