pub struct IdempotencyStore { /* private fields */ }Expand description
In-memory Idempotency-Key store. Bounded by capacity (default 10_000 entries — generous for the high-traffic banking POST case); once full, the oldest entry is evicted on insert.
Implementations§
Source§impl IdempotencyStore
impl IdempotencyStore
pub fn new(capacity: usize, retention: Duration) -> Self
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn hash_prefix_hex(hash: &[u8; 32]) -> String
pub fn hash_prefix_hex(hash: &[u8; 32]) -> String
Hex-encode the first 8 bytes of a SHA-256 digest. Enough entropy for adopter-side correlation, doesn’t leak the full hash (defense-in-depth).
Sourcepub fn hash_body(body: &[u8]) -> [u8; 32]
pub fn hash_body(body: &[u8]) -> [u8; 32]
Compute the canonical body hash. We hash the raw bytes the client sent — adopters submitting JSON with whitespace differences will hash DIFFERENTLY, which is the safer default (the client must canonicalize on its side if it wants semantic equality). Matches Stripe’s behavior.
Sourcepub fn lookup(
&mut self,
key: &IdempotencyCacheKey,
request_body_hash: &[u8; 32],
) -> IdempotencyVerdict
pub fn lookup( &mut self, key: &IdempotencyCacheKey, request_body_hash: &[u8; 32], ) -> IdempotencyVerdict
Look up a cached entry. Three-way verdict:
- Miss: no entry for this key.
- Hit(entry): entry found, body hash matches — replay.
- Conflict: entry found, body hash MISMATCH — return 422. Expired entries are evicted lazily and reported as Miss.
Sourcepub fn insert(&mut self, key: IdempotencyCacheKey, entry: IdempotencyEntry)
pub fn insert(&mut self, key: IdempotencyCacheKey, entry: IdempotencyEntry)
Insert (or overwrite) an entry. Caller is responsible for
only caching successful responses (the gate in
dynamic_endpoint_handler only caches 2xx — preserving the
semantic that retries genuinely retry execution on failure).
Sourcepub fn reap_expired(&mut self) -> usize
pub fn reap_expired(&mut self) -> usize
Sweep expired entries. Returns the number reaped. Intended to be called periodically by a server task to bound memory.
Sourcepub fn set_retention(&mut self, retention: Duration)
pub fn set_retention(&mut self, retention: Duration)
Reconfigure retention (for tests + per-endpoint future tuning).
Trait Implementations§
Source§impl Debug for IdempotencyStore
impl Debug for IdempotencyStore
Auto Trait Implementations§
impl Freeze for IdempotencyStore
impl RefUnwindSafe for IdempotencyStore
impl Send for IdempotencyStore
impl Sync for IdempotencyStore
impl Unpin for IdempotencyStore
impl UnsafeUnpin for IdempotencyStore
impl UnwindSafe for IdempotencyStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more