Skip to main content

KeyedRateLimiter

Struct KeyedRateLimiter 

Source
pub struct KeyedRateLimiter<K> { /* private fields */ }
Expand description

64-way cache-line-aligned sharded, bounded, lock-free-CAS keyed rate limiter.

Implementations§

Source§

impl<K> KeyedRateLimiter<K>
where K: Eq + Hash,

Core 64-way sharded bounded rate-limiting operations for KeyedRateLimiter.

Source

pub fn new( policy: RateLimitPolicy, max_entries: usize, ) -> Result<Self, RateLimitError>

Creates a 64-way sharded bounded KeyedRateLimiter for the given policy and max_entries.

§Arguments
  • RateLimitPolicy - Rate-limiting policy applied per key.
  • usize - Target maximum key capacity (>= 1).
§Returns
  • Result<Self, RateLimitError> - Constructed limiter or ZeroCapacity.
Source

pub fn with_clock( policy: RateLimitPolicy, max_entries: usize, clock: MonotonicClock, ) -> Result<Self, RateLimitError>

Creates a 64-way sharded bounded KeyedRateLimiter anchored at clock.

§Arguments
  • RateLimitPolicy - Rate-limiting policy applied per key.
  • usize - Target maximum key capacity (>= 1).
  • MonotonicClock - Monotonic clock origin.
§Returns
  • Result<Self, RateLimitError> - Constructed limiter or ZeroCapacity.
Source

pub fn get_policy(&self) -> RateLimitPolicy

Returns the configured rate-limiting policy.

§Returns
  • RateLimitPolicy - Copy of the active policy.
Source

pub fn get_clock(&self) -> &MonotonicClock

Returns the embedded monotonic clock.

§Returns
  • &MonotonicClock - Reference to the clock.
Source

pub fn get_max_entries(&self) -> usize

Returns the caller-requested target entry capacity.

§Returns
  • usize - Requested maximum entries.
Source

pub fn get_per_shard_capacity(&self) -> usize

Returns the hard entry capacity enforced per shard.

§Returns
  • usize - Maximum entries per shard.
Source

pub fn effective_capacity(&self) -> usize

Returns the effective upper bound across all 64 shards (64 * per_shard_capacity).

§Returns
  • usize - Total hard bound across all shards.
Source

pub fn check<Q>(&self, key: &Q) -> RateLimitDecision
where K: Borrow<Q>, Q: Eq + Hash + ToOwned<Owned = K> + ?Sized,

Checks and consumes 1 unit for key using the embedded monotonic clock.

§Arguments
  • &Q - Borrowed key reference (zero allocation on existing keys).
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn check_n<Q>(&self, key: &Q, cost: u64) -> RateLimitDecision
where K: Borrow<Q>, Q: Eq + Hash + ToOwned<Owned = K> + ?Sized,

Checks and consumes cost units for key using the embedded monotonic clock.

§Arguments
  • &Q - Borrowed key reference (zero allocation on existing keys).
  • u64 - Number of quota units to consume.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn check_at_ns<Q>(&self, key: &Q, now_ns: u64) -> RateLimitDecision
where K: Borrow<Q>, Q: Eq + Hash + ToOwned<Owned = K> + ?Sized,

Checks and consumes 1 unit for key at an explicit monotonic timestamp.

§Arguments
  • &Q - Borrowed key reference.
  • u64 - Monotonic timestamp in nanoseconds.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn check_n_at_ns<Q>( &self, key: &Q, now_ns: u64, cost: u64, ) -> RateLimitDecision
where K: Borrow<Q>, Q: Eq + Hash + ToOwned<Owned = K> + ?Sized,

Checks and consumes cost units for key at an explicit monotonic timestamp.

§Arguments
  • &Q - Borrowed key reference (zero allocation on existing keys).
  • u64 - Monotonic timestamp in nanoseconds.
  • u64 - Number of quota units to consume.
§Returns
  • RateLimitDecision - Admission decision and nanosecond timings.
Source

pub fn peek<Q>(&self, key: &Q) -> RateLimitDecision
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Inspects availability for key using the embedded clock without mutating or inserting state.

§Arguments
  • &Q - Borrowed key reference.
§Returns
  • RateLimitDecision - Non-mutating availability snapshot.
Source

pub fn peek_at_ns<Q>(&self, key: &Q, now_ns: u64) -> RateLimitDecision
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Inspects availability for key at now_ns without mutating or inserting state.

§Arguments
  • &Q - Borrowed key reference.
  • u64 - Monotonic timestamp in nanoseconds.
§Returns
  • RateLimitDecision - Non-mutating availability snapshot.
Source

pub fn remove<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Removes key from its shard if present.

§Arguments
  • &Q - Borrowed key reference.
§Returns
  • bool - true if the key existed and was removed.
Source

pub fn evict_idle(&self) -> usize

Evicts all currently idle entries across all 64 shards using the embedded monotonic clock.

§Returns
  • usize - Total number of evicted keys.
Source

pub fn evict_idle_at_ns(&self, now_ns: u64) -> usize

Evicts all idle entries across all 64 shards at an explicit monotonic timestamp.

§Arguments
  • u64 - Monotonic timestamp in nanoseconds.
§Returns
  • usize - Total number of evicted keys.
Source

pub fn clear(&self)

Clears all tracked keys across all 64 shards.

Source

pub fn len(&self) -> usize

Returns the total number of tracked keys currently stored across all 64 shards.

§Returns
  • usize - Approximate total entry count across shards.
Source

pub fn is_empty(&self) -> bool

Returns true if no keys are currently tracked across any shard.

§Returns
  • bool - true when every shard is empty.

Trait Implementations§

Source§

impl<K: Debug> Debug for KeyedRateLimiter<K>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K> !Freeze for KeyedRateLimiter<K>

§

impl<K> !RefUnwindSafe for KeyedRateLimiter<K>

§

impl<K> Forget for KeyedRateLimiter<K>

§

impl<K> Move for KeyedRateLimiter<K>

§

impl<K> Send for KeyedRateLimiter<K>
where Atomic<usize>: Send, BoxRaw<[ReaderSlot]>: Send, BoxRaw<[KeyedShard<K>]>: Send,

§

impl<K> Sync for KeyedRateLimiter<K>
where Atomic<usize>: Sync, BoxRaw<[ReaderSlot]>: Sync, BoxRaw<[KeyedShard<K>]>: Sync,

§

impl<K> Unpin for KeyedRateLimiter<K>
where Align8<usize>: Unpin, Box<[ReaderSlot]>: Unpin, Box<[KeyedShard<K>]>: Unpin,

§

impl<K> UnsafeUnpin for KeyedRateLimiter<K>
where Align8<usize>: UnsafeUnpin, *const [ReaderSlot] is !null: UnsafeUnpin, PhantomData<[ReaderSlot]>: UnsafeUnpin, *const [KeyedShard<K>] is !null: UnsafeUnpin, PhantomData<[KeyedShard<K>]>: UnsafeUnpin,

§

impl<K> UnwindSafe for KeyedRateLimiter<K>
where Align8<usize>: UnwindSafe, BoxRaw<[ReaderSlot]>: UnwindSafe, BoxRaw<[KeyedShard<K>]>: UnwindSafe,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.