pub struct InMemoryIdempotencyStore<T> { /* private fields */ }Expand description
A process-local store of completed results, one Mutex wide.
§Example
use std::num::NonZeroUsize;
use protocol_droid::adapters::secondary::idempotency::InMemoryIdempotencyStore;
use protocol_droid::application::idempotency::IdempotencyStore;
use protocol_droid::domain::idempotency::{Claim, IdempotencyKey};
let store = InMemoryIdempotencyStore::with_capacity(NonZeroUsize::MIN);
let key = IdempotencyKey::new("req-1");
let first = store.claim(&key)?;
assert!(first.is_execution_mine());
let token = first.token().ok_or("claimed without a token")?;
store.complete(token, "charged once")?;
// The client retries because the connection dropped. It gets the recorded
// result and no second charge.
let replay = store.claim(&key)?;
// `matches!` on a `&replay` binds `result` as `&&str`, so the recorded
// value is two derefs in.
assert!(matches!(&replay, Claim::Completed(result) if **result == *"charged once"));Implementations§
Source§impl<T> InMemoryIdempotencyStore<T>
impl<T> InMemoryIdempotencyStore<T>
Sourcepub fn with_capacity(capacity: NonZeroUsize) -> Self
pub fn with_capacity(capacity: NonZeroUsize) -> Self
A store holding at most capacity keys.
Bounded because the keys are peer-chosen; see
CompletedResults::with_capacity.
Sourcepub fn capacity(&self) -> Result<usize, IdempotencyError>
pub fn capacity(&self) -> Result<usize, IdempotencyError>
The ceiling, in keys.
Takes the lock, so a poisoned store reports the error rather than a made-up number.
§Errors
IdempotencyError::Unavailable when the lock is poisoned.
Sourcepub fn len(&self) -> Result<usize, IdempotencyError>
pub fn len(&self) -> Result<usize, IdempotencyError>
How many keys are held, including claims that are still in flight.
§Errors
IdempotencyError::Unavailable when the lock is poisoned.
Sourcepub fn is_empty(&self) -> Result<bool, IdempotencyError>
pub fn is_empty(&self) -> Result<bool, IdempotencyError>
Trait Implementations§
Source§impl<T: Debug> Debug for InMemoryIdempotencyStore<T>
impl<T: Debug> Debug for InMemoryIdempotencyStore<T>
Source§impl<T> Default for InMemoryIdempotencyStore<T>
impl<T> Default for InMemoryIdempotencyStore<T>
Source§fn default() -> Self
fn default() -> Self
A store of default_capacity keys.
Source§impl<T: Clone + Send + Sync> IdempotencyStore for InMemoryIdempotencyStore<T>
impl<T: Clone + Send + Sync> IdempotencyStore for InMemoryIdempotencyStore<T>
Source§type Result = T
type Result = T
What a completed side effect produced. The adapter’s own result type —
an
IrResult for a tool call, a transport payload for a bridge forward.Source§fn claim(&self, key: &IdempotencyKey) -> Result<Claim<T>, IdempotencyError>
fn claim(&self, key: &IdempotencyKey) -> Result<Claim<T>, IdempotencyError>
Takes ownership of
key, or reports that it is already spoken for. Read moreSource§fn complete(
&self,
token: &ClaimToken,
result: T,
) -> Result<(), IdempotencyError>
fn complete( &self, token: &ClaimToken, result: T, ) -> Result<(), IdempotencyError>
Records the result of a claim the caller owns. Read more
Source§fn abandon(&self, token: &ClaimToken) -> Result<(), IdempotencyError>
fn abandon(&self, token: &ClaimToken) -> Result<(), IdempotencyError>
Releases a claim whose execution did not produce a result. Read more
Auto Trait Implementations§
impl<T> !Freeze for InMemoryIdempotencyStore<T>
impl<T> RefUnwindSafe for InMemoryIdempotencyStore<T>
impl<T> Send for InMemoryIdempotencyStore<T>
impl<T> Sync for InMemoryIdempotencyStore<T>
impl<T> Unpin for InMemoryIdempotencyStore<T>
impl<T> UnsafeUnpin for InMemoryIdempotencyStore<T>
impl<T> UnwindSafe for InMemoryIdempotencyStore<T>
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
Mutably borrows from an owned value. Read more