Kantoor. Provisioning. Idempotency
(Kantoor v1.0.0)
Idempotency-Key for the provisioning API, kept in Postgres so that it holds across
replicas.
A client may send Idempotency-Key: <1 to 255 printable characters> with a write.
The first request with a key (per operator) claims it, runs, and stores its answer
(status and JSON body). A repeat with the same key and the same request (method,
path, body) gets the stored answer again, without running anything, with the
header Idempotent-Replayed: true. The same key with a different request is
refused (idempotency_key_reuse, 422). A repeat that arrives while the first is
still running is refused with 409 and Retry-After (idempotency_in_progress); a
claim whose request died is taken over after 30 seconds.
Answers of 5xx and 429 are not kept (the repeat runs again); every other answer is, for 24 hours. Natural keys (the external reference of a domain or user, the address of a mailbox) make writes idempotent without this header; the header adds replay of the exact answer and protects a request whose body changed between tries.
The claim is one INSERT ... ON CONFLICT DO NOTHING on a unique index, so two
replicas racing for a key cannot both run.
Summary
Functions
A fingerprint of the request: method, path and parsed body.
The key from the request header: {:ok, nil} without one, {:error, :invalid} for a bad one.
Runs fun (returning {status, body}) once for key. Returns
{:ok, status, body}, {:replayed, status, body} or {:error, :reuse | :in_progress}.
Types
@type result() :: {pos_integer(), map() | nil}
Functions
@spec fingerprint(Plug.Conn.t()) :: binary()
A fingerprint of the request: method, path and parsed body.
@spec parse(Plug.Conn.t()) :: {:ok, String.t() | nil} | {:error, :invalid}
The key from the request header: {:ok, nil} without one, {:error, :invalid} for a bad one.
@spec run(String.t(), String.t(), binary(), (-> result())) :: {:ok, pos_integer(), map() | nil} | {:replayed, pos_integer(), map() | nil} | {:error, :reuse | :in_progress}
Runs fun (returning {status, body}) once for key. Returns
{:ok, status, body}, {:replayed, status, body} or {:error, :reuse | :in_progress}.