Documentation
¶
Overview ¶
Package idempotency provides safe-retry middleware for Echo v5.
It makes unsafe requests (POST, PUT, PATCH, DELETE) safe to retry by remembering the response to a request that carries an idempotency key and replaying it when the same key is seen again. This is the behaviour behind the "Idempotency-Key" header in the IETF draft and in payment APIs such as Stripe's.
The middleware is built on store.Store and its owned locks (see the store package), so the "only one request runs at a time per key" guarantee holds across a single instance (MemoryStore) or a whole cluster (RedisStore).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrKeyRequired is passed to ErrorHandler when a request carries no // idempotency key but Config.Required is set. ErrKeyRequired = errors.New("idempotency: an Idempotency-Key header is required") // ErrKeyInvalid is passed to ErrorHandler when the key is empty, too // long, or contains control characters. ErrKeyInvalid = errors.New("idempotency: the Idempotency-Key is invalid") // ErrFingerprintMismatch is passed to ErrorHandler when a key is reused // with a different request (method, path, or body) than the one that was // stored. The stored response is never replayed in this case. ErrFingerprintMismatch = errors.New("idempotency: the key was already used with a different request") // ErrRequestInFlight is passed to ErrorHandler when a duplicate arrives // while the first request with the same key is still running and the // policy is InFlightReject. ErrRequestInFlight = errors.New("idempotency: a request with this key is already being processed") )
var DefaultConfig = Config{ Header: "Idempotency-Key", TTL: 24 * time.Hour, MaxBodySize: 1024 * 1024, InFlight: InFlightReject, WaitTimeout: 10 * time.Second, RetryDelay: 20 * time.Millisecond, MaxRetries: 10, LockTTL: 30 * time.Second, StoreStatusCodes: []int{ http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent, http.StatusBadRequest, http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound, http.StatusConflict, http.StatusUnprocessableEntity, http.StatusTooManyRequests, }, ErrorHandler: func(c *echo.Context, err error) error { return err }, }
DefaultConfig provides the standard settings for idempotency middleware.
Functions ¶
func New ¶
func New(config Config) echo.MiddlewareFunc
New returns an Echo middleware that makes unsafe requests idempotent.
Types ¶
type Config ¶
type Config struct {
// Store persists the fingerprint and the stored response. It must be a
// store.Store because idempotency needs both Get/Save and owned locks.
Store store.Store
// TTL is how long a completed response is remembered so a later retry
// with the same key replays it instead of running the handler again.
// Default is 24h.
TTL time.Duration
// Skipper defines a function to skip the middleware. Defaults to
// middleware.DefaultSkipper.
Skipper middleware.Skipper
// Header is the request header that carries the idempotency key.
// Default is "Idempotency-Key".
Header string
// Required rejects requests that carry no key (400) instead of passing
// them through. Default is false.
Required bool
// KeyFunc derives the idempotency key. When set it overrides Header; use
// it to scope keys per route or tenant (for example by prefixing the
// authenticated API key).
KeyFunc func(c *echo.Context) string
// MaxBodySize caps how many response bytes are stored. A larger response
// is streamed to the client and not stored, so a retry re-runs the
// handler. Default is 1MB.
MaxBodySize int
// InFlight selects the duplicate-request policy. Default is InFlightReject.
InFlight InFlightPolicy
// WaitTimeout bounds how long an InFlightWait duplicate waits for the
// first request before giving up. Default is 10s.
WaitTimeout time.Duration
// RetryDelay and MaxRetries control the poll cadence an InFlightWait
// duplicate uses while waiting for the first request to finish.
// Defaults are 20ms and 10 retries.
RetryDelay time.Duration
MaxRetries int
// LockTTL bounds how long the in-flight lock is held before another
// request may take it over. Default is 30s. Set it longer than your
// slowest idempotency-protected handler.
LockTTL time.Duration
// StoreStatusCodes is the allow-list of response statuses that are stored.
// 5xx responses are never stored by default so a retry can succeed after a
// transient failure. Default is []int{200, 201, 202, 204, 400, 401, 403,
// 404, 409, 422, 429}.
StoreStatusCodes []int
// ErrorHandler renders the error response for rejected requests (missing
// key when Required, a fingerprint mismatch, or a concurrent duplicate
// under InFlightReject). Default renders 400/409/422 with a short message.
ErrorHandler func(c *echo.Context, err error) error
// FailClosed reports whether a store failure (not a lock conflict) should
// reject the request instead of failing open. Default is false (fail open:
// run the handler without idempotency).
FailClosed bool
}
Config defines the settings for the idempotency middleware.
type InFlightPolicy ¶
type InFlightPolicy int
InFlightPolicy controls what happens when a second request arrives with an idempotency key whose first request is still running.
const ( // InFlightReject (default) answers the duplicate immediately with // 409 Conflict. It is the cheapest policy and matches the common // "the first request is still being processed, retry later" behaviour. InFlightReject InFlightPolicy = iota // InFlightWait parks the duplicate until the first request finishes and // then replays its stored response. It is friendlier to clients but // holds a goroutine (and connection) for the duration of the wait. InFlightWait )