Expand description
HTTP-facing building blocks for durable idempotency (ADR 0011).
The durable guarantee itself lives in the application layer, inside the
UnitOfWork transaction, so the reservation commits atomically with the
domain mutation and the outbox write (seiva_ports::IdempotencyStore, proven
against a real database by seiva_data_dorm::DormIdempotencyStore). A generic
outer middleware could not keep that atomicity — it runs above any
transaction — so this module gives the transport only its half:
IdempotencyKey— validate theIdempotency-Keyheader (bounded length, visible-ASCII only), via theOptionalIdempotencyKeyextractor.scoped_key— fold the caller’s tenant + subject + the operation (method + path, which carries the API version segment) into the key the store sees, so two tenants (or two identities) can never collide or replay one another’s result.fingerprint— a canonical SHA-256 of the request’s semantically relevant arguments (method, path, query, and the body as canonical JSON —serde_json’s default sorts object keys). The store answersMismatchwhen the same key arrives with a different fingerprint.mismatch_problem/in_progress_problem— map the non-Freshoutcomes to RFC 9457 responses.
The application threads the key + fingerprint into IdempotencyStore::begin
inside its transaction; on Fresh it performs the effect and finishes, on
Replay it returns the stored result, on Mismatch/InProgress it returns
the problem below. Re-authorization holds because the scoped key is bound to
the caller’s identity: a different caller gets a different key and never reads
a prior caller’s response.
Structs§
- Idempotency
Key - A validated
Idempotency-Key: non-empty, at mostMAX_KEY_LENbytes, and visible ASCII only (0x21..=0x7E) so it cannot smuggle control characters or whitespace into a header or a storage key. - Optional
Idempotency Key - Extractor for an optional idempotency key:
Ok(None)when the header is absent (idempotency is opt-in per request, never forced on everyPOST),Err(400)when it is present but malformed.
Constants§
- IDEMPOTENCY_
HEADER - The header carrying the client’s idempotency key.
- MAX_
KEY_ LEN - The maximum accepted key length (bytes). Longer keys are rejected
400, so a client cannot store unbounded data under a key.
Functions§
- fingerprint
- A canonical SHA-256 fingerprint of the request’s semantically relevant
arguments.
bodyis serialised withserde_json(whose defaultMapis aBTreeMap, so object keys are sorted) — the same body reorders to the same bytes, so a retry with logically-equal content matches. - in_
progress_ problem - The
409problem for a key whose first request is still in flight: the caller retries after a short delay (it adds its ownRetry-Afterif it wishes). - mismatch_
problem - The
409problem for a key reused with a different request fingerprint: the same key must mean the same operation, so the server does not execute. - scoped_
key - Folds the caller’s identity and the operation into the key the store sees, so keys are isolated per tenant and per subject (and per method+path, which carries the API version). Returns a fixed-length hex digest: bounded, and free of any delimiter a component could spoof.