idempotency

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 9 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

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

		return fiber.IsMethodSafe(c.Method())
	},

	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
	},

	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).
	KeyHeaderValidate func(string) error

	// 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.
	//
	// Optional. Default: nil (to keep all headers)
	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.

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