idempotent

package
v3.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package idempotent standardizes HTTP contracts for idempotent API workflows.

The package is stable core API. It does not store idempotency keys or replay responses; middleware/idempotency or application code owns reservation, persistence, locking, TTLs, and response replay. idempotent provides the shared HTTP pieces that handlers, route contracts, and tests can agree on.

Start with RequireKey to extract the Idempotency-Key header and RequestHash to compare method, target, and body bytes. Use ConflictProblem, ReplayProblem, WriteConflict, WriteAcceptedReplay, and OperationExtensions to keep conflict, replay, accepted-operation, and OpenAPI behavior consistent across create, update, patch, and asynchronous workflows.

Keep keys scoped to tenant and principal in storage, reject reused keys with mismatched hashes, and avoid using this package alone as a replay store. For examples, see docs/cookbook.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConflictProblem

func ConflictProblem(detail string) httpx.Problem

ConflictProblem returns the standard idempotency conflict problem.

func OperationExtensions

func OperationExtensions(required bool) map[string]any

OperationExtensions returns OpenAPI extensions for idempotent operation contracts.

func ReplayProblem

func ReplayProblem(detail string) httpx.Problem

ReplayProblem returns the standard idempotency replay problem.

func RequestHash

func RequestHash(r *http.Request, body []byte) string

RequestHash returns a deterministic SHA-256 hash for method, target, and body bytes.

func RequireKey

func RequireKey(r *http.Request, headerName string) (string, error)

RequireKey extracts a required idempotency key from the request.

func WriteAcceptedReplay

func WriteAcceptedReplay(w http.ResponseWriter, config AsyncConfig)

WriteAcceptedReplay writes a replayed 202 Accepted operation response.

func WriteConflict

func WriteConflict(w http.ResponseWriter, detail string)

WriteConflict writes a 409 idempotency conflict Problem Details response.

Types

type AsyncConfig

type AsyncConfig struct {
	ID         string
	Location   string
	RetryAfter time.Duration
}

AsyncConfig describes an idempotent async replay response.

type CreateConfig

type CreateConfig struct {
	HeaderName string
	Required   bool
}

CreateConfig describes idempotency requirements for create workflows.

type UpdateConfig

type UpdateConfig struct {
	HeaderName string
	Required   bool
}

UpdateConfig describes idempotency requirements for update workflows.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL