Skip to main content

MemoryStore

Struct MemoryStore 

Source
pub struct MemoryStore<C = SystemClock> { /* private fields */ }
Expand description

A sharded, hard-bounded, process-local fixed-window store.

The store never evicts an active key to admit a new one. Every check touches only the shards required by that check and performs a bounded amount of expiry cleanup. An atomic batch locks its shards in deterministic order.

If application code panics while a shard is locked, that shard remains poisoned and every later operation touching it fails closed with MemoryStoreError::PoisonedShard. MemoryStore::recover_poisoned is an explicit availability tradeoff: it restores poisoned shards by discarding their counters rather than trusting state whose consistency is unknown.

Implementations§

Source§

impl MemoryStore<SystemClock>

Source

pub fn new(config: MemoryStoreConfig) -> Self

Creates a store using the system monotonic clock.

Source§

impl<C: Clock> MemoryStore<C>

Source

pub fn with_clock(config: MemoryStoreConfig, clock: C) -> Self

Creates a store using a caller-supplied monotonic clock.

Supplying a clock is primarily useful for deterministic tests.

Source

pub fn with_observer(self, observer: Arc<dyn Observer>) -> Self

Returns this store with an operational observer.

Observer callbacks run after internal locks are released. A callback panic is isolated and cannot change the admission result.

Source

pub fn check(&self, check: &Check<'_>) -> Result<Decision, MemoryStoreError>

Evaluates and, when allowed, consumes one check.

§Errors

Returns an error without allowing the request when the target shard is poisoned.

Source

pub fn check_all( &self, checks: &[Check<'_>], ) -> Result<BatchDecision, MemoryStoreError>

Evaluates a batch atomically.

If any check is denied, none of the checks consumes quota. Returned allowed decisions preserve input order.

§Errors

Returns an error before quota consumption when the batch exceeds its configured bound, contains a duplicate key, targets more distinct keys at one shard than that shard can ever store, or a touched shard is poisoned.

Source

pub fn stats(&self) -> Result<MemoryStoreStats, MemoryStoreError>

Returns current storage usage.

§Errors

Returns an error if a shard is poisoned.

Source

pub fn clear(&self) -> Result<(), MemoryStoreError>

Removes every stored counter.

§Errors

Returns an error if a shard is poisoned.

Source

pub fn recover_poisoned(&self) -> usize

Rebuilds every poisoned shard and returns the number recovered.

A poisoned shard’s internal consistency is unknown, so recovery discards all counters in that shard before clearing its mutex poison flag. This can admit requests that the discarded counters would have denied. Healthy shards and their active counters are left unchanged.

Until this method is called, operations touching a poisoned shard continue to fail closed with MemoryStoreError::PoisonedShard. Recovery is safe to invoke through an std::sync::Arc. It locks every shard in index order before changing any of them, so concurrent checks observe either the pre-recovery poisoned store or the complete recovered state, never a partially reopened multi-shard store.

Trait Implementations§

Source§

impl<C> Debug for MemoryStore<C>

Source§

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

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

impl<C: Clock> Limiter for MemoryStore<C>

Source§

type Policy = FixedWindowPolicy

Policy algorithm supported by this backend.
Source§

type Error = MemoryStoreError

Backend-specific operational failure.
Source§

async fn check(&self, check: &Check<'_>) -> Result<Decision, Self::Error>

Evaluates and, when allowed, consumes one check.
Source§

async fn check_all( &self, checks: &[Check<'_>], ) -> Result<BatchDecision, Self::Error>

Evaluates a batch atomically. Read more

Auto Trait Implementations§

§

impl<C = SystemClock> !RefUnwindSafe for MemoryStore<C>

§

impl<C = SystemClock> !UnwindSafe for MemoryStore<C>

§

impl<C> Freeze for MemoryStore<C>
where C: Freeze,

§

impl<C> Send for MemoryStore<C>
where C: Send,

§

impl<C> Sync for MemoryStore<C>
where C: Sync,

§

impl<C> Unpin for MemoryStore<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for MemoryStore<C>
where C: UnsafeUnpin,

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> Same for T

Source§

type Output = T

Should always be Self
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.