ADR 0006: the HTTP adapter, Idempotency-Key, problem types and readiness
Status
Accepted (2026-10-23).
Context
apis/openapi.yaml fixes the fifteen operations; the model conventions fix the
four shared problem types; contracts-delta fixes Idempotency-Key on every
state-changing route (table idempotency_keys, 24 h, replay of the first
response). The fleet requires a dependency-aware /readyz, a NUL guard and a
graceful shutdown.
Decision
- Router.
internal/adapters/inbound/httpis a chi router over the use cases (Serverholds one field per operation;NewRouterrefuses a server withoutIdempotency). Middleware, outermost first: access log, recoverer (a panic is a 500internal-errorthe log still sees),rejectNUL, then the route. The adapter imports only the application layer and the domain;cmd/apiinjects the use cases, the readiness gate and the transaction binder (pgtx.With), and never imports the memory or repocontract packages. - Operations.
POST /nodes,GET /nodes,GET /nodes/{nodeId},PUT /nodes/{nodeId}/operational-profile,POST /nodes/{nodeId}/activations,/suspensions,/retirements,POST /lanes,GET /lanes,POST /lanes/{laneId}/closures,PUT /service-areas/{postalPrefix},GET /service-areas/{postalPrefix},POST /regions,GET /regions,GET /regions/{regionId}; plus the probesGET /healthzandGET /readyz(not part of the business contract). Creates answer 201, everything else 200. The three lists answer{"items":[...],"nextCursor":"..."}(ADR 0012); an empty result is{"items":[]}, never null. The response bodies are theviewstypes. - Problems (RFC 9457,
application/problem+json).typeis the slug (the OpenAPIProblemschema),title,status,detail(the error text; an unmapped error is an opaque 500 whose detail is fixed and whose cause is logged) andinstance(the path). Shared:not-found404,illegal-transition409,concurrent-update409,validation-failed422. One slug per model Err, the kebab-case of the name withoutErr: 409vendor-node-code-taken,stale-profile-revision,incomplete-operational-profile,node-retired,lane-endpoint-not-active,duplicate-lane,not-a-delivery-station,stale-assignment-version,region-code-taken; 422operator-not-allowed-for-node-type,subtype-not-allowed,cutoff-outside-operating-hours,sort-dwell-not-allowed,lane-loop,lane-type-pair-not-allowed,unknown-region. Adapter problems: 400malformed-request-body(bad JSON, unknown field, wrong type, empty body, trailing data, NUL), 400malformed-request-query(NUL in the query), 413request-body-too-large(64 KiB), 404not-foundfor an unknown route or a NUL in the path, 405method-not-allowed, 400idempotency-key-required, 400idempotency-key-invalid(not 1-128 printable ASCII characters), 409idempotency-key-reused, 400invalid-limit/invalid-cursor/unknown-query-parameter, 503dependency-unavailable. - NUL. A NUL (U+0000) cannot be stored in Postgres text and would abort the
transaction.
rejectNULanswers before the Idempotency middleware records anything: 404 for the path, 400 for the query and the body (raw byte or the\u0000escape; an escaped backslash beforeu0000is not a NUL). - Idempotency-Key. Required on all eight state-changing routes. The key is global
(ADR 0012;
routeis stored for diagnosis only); the request fingerprint issha256(method, path, body), so the same key on another node is a reuse. The same key and fingerprint replays the first status and body (headerIdempotency-Replayed: true, content type derived from the status); the same key with another fingerprint is 409idempotency-key-reused(ADR 0012; it was 422). The key row and the handler's writes share one transaction: the handler runs in a SAVEPOINT bound into its context, a 2xx keeps its writes, a 4xx rolls them back to the savepoint but is stored (a repeat answers the same), a 5xx or a panic rolls everything back and is never stored. Concurrent requests with one key race onINSERT ... ON CONFLICT DO NOTHING; the loser blocks until the winner resolves and then replays. Keys live 24 h: every keyed write first deletes the expired rows (index oncreated_at), so an expired key is a new key and the table does not grow without bound. No migration was needed. - Probes.
/healthzis liveness: 200{"status":"ok"}while the process serves, never flipped./readyzis 200{"status":"ready"}only while the drain gate is open AND Postgres answers a ping AND a Kafka broker answers a metadata request. The Kafka request names NO topics (an empty, non-nil topic list), so it never creates a topic and does not require the output topic to exist (the broker creates it on the first write); it dials a fresh connection on every check, because kafka-go's cached metadata would keep answering ready after the broker is gone. Verdicts are cached 3 s and bounded 2 s, on a context detached from the caller. While draining: 503{"status":"not_ready"}; while a dependency fails: 503dependency-unavailablenaming the dependencies. - Composition root.
cmd/apirun(ctx)validates the environment before anything opens (DATABASE_URL,KAFKA_BROKERS: a boot error names the variable;MIGRATIONS_DATABASE_URLis an optional direct DSN that falls back toDATABASE_URL), runs the embedded migrations and pings Postgres with backoff (internal/bootretry), starts the relay and serves. Shutdown order:/readyzflips to 503, waitSHUTDOWN_DRAIN_DELAY(default 5 s), drain HTTP (10 s), stop and await the relay, close the pool. If the relay returns on its own the process exits non-zero.
Consequences
- A client retrying a timed-out write with the same key never duplicates a node, lane, region, assignment or event.
- A 4xx is replayed too: after fixing the cause the client sends a new key. Pointer 2026-11-19: superseded in part by ADR 0020: only a refusal whose class is FINAL is stored and replayed; a RETRYABLE refusal releases the key and a retry under it runs again.
Addendum 2026-11-13: current state
apis/openapi.yaml now has sixteen operations, not fifteen, and ten routes take an
Idempotency-Key (each registered through keyed in
internal/adapters/inbound/http/handlers_nodes.go and handlers_other.go), not eight.
The addition is POST /lanes/{laneId}/cost-declarations (declareLaneCost, ADR 0014).