idempotency

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 16 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Next: func(c fiber.Ctx) bool {

		return c.IsSafe()
	},

	Lifetime: 30 * time.Minute,

	KeyHeader: "X-Idempotency-Key",
	KeyHeaderValidate: func(k string) error {
		if l, wl := len(k), 36; l != wl {
			return fmt.Errorf("%w: invalid length: %d != %d", ErrInvalidIdempotencyKey, l, wl)
		}

		return nil
	},

	KeyScope: nil,

	KeepResponseHeaders: nil,

	Lock: nil,

	Storage:               nil,
	DisableValueRedaction: false,
}

ConfigDefault is the default config

View Source
var ErrInvalidIdempotencyKey = errors.New("invalid idempotency key")

Functions

func IsFromCache

func IsFromCache(c fiber.Ctx) bool

IsFromCache reports whether the middleware served the response from the cache for the current request.

func New

func New(config ...Config) fiber.Handler

New creates idempotency middleware that caches responses keyed by the configured idempotency header.

func WasPutToCache

func WasPutToCache(c fiber.Ctx) bool

WasPutToCache reports whether the middleware stored the response produced by the current request in the cache.

Types

type Config

type Config struct {
	// Lock locks an idempotency key.
	//
	// Optional. Default: an in-memory locker for this process only.
	Lock Locker

	// Storage stores response data by idempotency key.
	//
	// Optional. Default: an in-memory storage for this process only.
	Storage fiber.Storage

	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: a function which skips the middleware on safe HTTP request method.
	Next func(c fiber.Ctx) bool

	// KeyHeaderValidate defines a function to validate the syntax of the idempotency header.
	//
	// Optional. Default: a function which ensures the header is 36 characters long (the size of an UUID).
	// It checks the length only, so it accepts a key that is easy to guess; see KeyScope.
	KeyHeaderValidate func(string) error

	// KeyScope returns the scope an idempotency key belongs to, such as the ID of the
	// authenticated user. A response is stored under the scope and the key together, and
	// replayed only to a request that carries the same key in the same scope, so a key that
	// another user sent or guessed returns nothing stored for you. Requests for which it
	// returns the same string share a scope, the empty string included.
	//
	// Optional. Default: nil (the key alone identifies a request)
	KeyScope func(c fiber.Ctx) string

	// KeyHeader is the name of the header that contains the idempotency key.
	//
	// Optional. Default: X-Idempotency-Key
	KeyHeader string

	// KeepResponseHeaders is a list of headers that should be kept from the original response.
	// Unchanged header lines already present before the downstream handler runs are not
	// stored, even when named here: earlier middleware sets them again for each request.
	// A response is replayed to every request that carries its key, so Set-Cookie and the
	// obsolete Set-Cookie2 are kept only when named here: a stored cookie would hand the
	// session of the first client to the others. Every other header is kept by default, so
	// a session or token carried in one, such as the session ID the session middleware
	// writes to the header it reads it from, needs a list that leaves it out, or KeyScope.
	//
	// Optional. Default: nil (to keep all headers but the cookies)
	KeepResponseHeaders []string

	// Lifetime is the maximum lifetime of an idempotency key.
	//
	// Optional. Default: 30 * time.Minute
	Lifetime time.Duration

	// DisableValueRedaction turns off masking idempotency keys in logs and errors when set to true.
	//
	// Optional. Default: false
	DisableValueRedaction bool
}

Config defines the config for middleware.

type Locker

type Locker interface {
	Lock(key string) error
	Unlock(key string) error
}

Locker implements a spinlock for a string key.

type MemoryLock

type MemoryLock struct {
	// contains filtered or unexported fields
}

MemoryLock coordinates access to idempotency keys using in-memory locks. MemoryLock is safe for concurrent use.

func NewMemoryLock

func NewMemoryLock() *MemoryLock

NewMemoryLock creates a MemoryLock ready for use.

func (*MemoryLock) Lock

func (l *MemoryLock) Lock(key string) error

Lock acquires the lock for the provided key, creating it when necessary.

func (*MemoryLock) Unlock

func (l *MemoryLock) Unlock(key string) error

Unlock releases the lock associated with the provided key.

Jump to

Keyboard shortcuts

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