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>
impl MemoryStore<SystemClock>
Sourcepub fn new(config: MemoryStoreConfig) -> Self
pub fn new(config: MemoryStoreConfig) -> Self
Creates a store using the system monotonic clock.
Source§impl<C: Clock> MemoryStore<C>
impl<C: Clock> MemoryStore<C>
Sourcepub fn with_clock(config: MemoryStoreConfig, clock: C) -> Self
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.
Sourcepub fn with_observer(self, observer: Arc<dyn Observer>) -> Self
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.
Sourcepub fn check(&self, check: &Check<'_>) -> Result<Decision, MemoryStoreError>
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.
Sourcepub fn check_all(
&self,
checks: &[Check<'_>],
) -> Result<BatchDecision, MemoryStoreError>
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.
Sourcepub fn stats(&self) -> Result<MemoryStoreStats, MemoryStoreError>
pub fn stats(&self) -> Result<MemoryStoreStats, MemoryStoreError>
Sourcepub fn clear(&self) -> Result<(), MemoryStoreError>
pub fn clear(&self) -> Result<(), MemoryStoreError>
Sourcepub fn recover_poisoned(&self) -> usize
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.