A small HTTP handler receives a request to confirm an order, stores the execution result under an idempotency key, and must react correctly to context cancellation.
net/http handler and is invoked in a single process.Idempotency-Key header.r.Context() is already canceled before writing starts, state must not change.200 OK on successful write, 409 Conflict on a repeat with the same key, and 499 when the context is canceled before work starts.sync.Mutex; there are no additional goroutines.POST /orders/confirm with body order-17 and header Idempotency-Key: k-1.Example request:
curl -i -X POST http://localhost:8080/orders/confirm \
-H 'Idempotency-Key: k-1' \
--data 'order-17'
200 OK.409 Conflict and does not create a new result.499.go test ./... and go test -race ./...map is performed under one sync.MutexUse line comments to explain the problem, its impact, the fix and how to test it.