Idempotency Key Replay Store
Track in-flight and completed requests by idempotency key, replay stored responses within a TTL, and release abandoned work.
Code
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::{Duration, Instant};
#[derive(Clone)]
struct CachedResponse {
status: u16,
body: String,
stored_at: Instant,
}
enum Entry {
InProgress,
Done(CachedResponse),
}
enum Outcome {
Fresh,
InProgress,
Replay(CachedResponse),
}
struct IdempotencyStore {
entries: Mutex<HashMap<String, Entry>>,
ttl: Duration,
}
impl IdempotencyStore {
fn new(ttl: Duration) -> Self {
Self {
entries: Mutex::new(HashMap::new()),
ttl,
}
}
fn begin(&self, key: &str) -> Outcome {
let mut entries = self.entries.lock().expect("store poisoned");
let replay = match entries.get(key) {
Some(Entry::InProgress) => return Outcome::InProgress,
Some(Entry::Done(cached)) if cached.stored_at.elapsed() < self.ttl => {
Some((*cached).clone())
}
_ => None,
};
if let Some(cached) = replay {
return Outcome::Replay(cached);
}
entries.insert(key.to_string(), Entry::InProgress);
Outcome::Fresh
}
fn complete(&self, key: &str, status: u16, body: &str) {
let mut entries = self.entries.lock().expect("store poisoned");
entries.insert(
key.to_string(),
Entry::Done(CachedResponse {
status,
body: body.to_string(),
stored_at: Instant::now(),
}),
);
}
fn abandon(&self, key: &str) {
self.entries
.lock()
.expect("store poisoned")
.remove(key);
}
}
fn main() {
let store = IdempotencyStore::new(Duration::from_secs(60));
match store.begin("order-1") {
Outcome::Fresh => println!("first request: fresh"),
_ => println!("unexpected first outcome"),
}
match store.begin("order-1") {
Outcome::InProgress => println!("duplicate while running: in progress"),
_ => println!("unexpected duplicate outcome"),
}
store.complete("order-1", 201, "{\"id\":1}");
match store.begin("order-1") {
Outcome::Replay(cached) => println!("replay: {} {}", cached.status, cached.body),
_ => println!("unexpected replay outcome"),
}
store.abandon("order-2");
}
How it works
Idempotency Key Replay Store keeps everything in the functions new, begin, complete and abandon.
Branching goes through match, whose exhaustiveness checking turns forgotten cases into compile errors.
Using it
The primary signature is fn new(ttl: Duration) -> Self
The run emits 6 labeled sections — first request: fresh, unexpected first outcome, duplicate while running: in progress, unexpected duplicate outcome, replay:, unexpected replay outcome — one per step.
Start the program and main runs new, begin, complete and abandon one after another.
Key statements
Some(Entry::Done(cached)) if cached.stored_at.elapsed() < self.ttl => {
Some((*cached).clone())
}
fn new(ttl: Duration) -> Self {
Self {
entries: Mutex::new(HashMap::new()),
ttl,
}
}
return Outcome::Replay(cached);
Interactive Tools for This Topic
Cache-Control BuilderCompose a valid Cache-Control header from directives such as no-store, private, max-age, and immutable.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.Accept Header ParserParse an Accept header into media ranges with their quality values and specificity, sorted by preference for content negotiation debugging.Basic & Bearer Auth Header ToolEncode and decode HTTP Authorization Basic and Bearer headers locally with UTF-8 Base64 validation and automatic JWT claim and expiry inspection.
FAQ
- What does Idempotency Key Replay Store print when run?
- Execute the whole file; each printed line is prefixed with a label like `first request: fresh`, `unexpected first outcome`, `duplicate while running: in progress`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.