Skip to main content

Module idempotency

Module idempotency 

Source
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:

  1. IdempotencyKey — validate the Idempotency-Key header (bounded length, visible-ASCII only), via the OptionalIdempotencyKey extractor.
  2. 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.
  3. 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 answers Mismatch when the same key arrives with a different fingerprint.
  4. mismatch_problem / in_progress_problem — map the non-Fresh outcomes 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§

IdempotencyKey
A validated Idempotency-Key: non-empty, at most MAX_KEY_LEN bytes, and visible ASCII only (0x21..=0x7E) so it cannot smuggle control characters or whitespace into a header or a storage key.
OptionalIdempotencyKey
Extractor for an optional idempotency key: Ok(None) when the header is absent (idempotency is opt-in per request, never forced on every POST), 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. body is serialised with serde_json (whose default Map is a BTreeMap, 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 409 problem for a key whose first request is still in flight: the caller retries after a short delay (it adds its own Retry-After if it wishes).
mismatch_problem
The 409 problem 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.