middleware/idempotency
import "github.com/gofiber/fiber/v3/middleware/idempotency"
Variables
ConfigDefault is the default config
Source: middleware/idempotency/config.go:75
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,
}Source: middleware/idempotency/config.go:12
var ErrInvalidIdempotencyKey = errors.New("invalid idempotency key")ErrTooManyWaiters is returned by MemoryLock when maxLockWaiters requests already wait for the key.
Source: middleware/idempotency/locker.go:13
var ErrTooManyWaiters = fiber.NewError(fiber.StatusConflict, "too many requests are waiting for this idempotency key")Functions
func IsFromCache(c fiber.Ctx) bool
Source: middleware/idempotency/idempotency.go:76
IsFromCache reports whether the middleware served the response from the cache for the current request.
func New(config ...Config) fiber.Handler
Source: middleware/idempotency/idempotency.go:92
New creates idempotency middleware that caches responses keyed by the configured idempotency header.
func NewMemoryLock() *MemoryLock
Source: middleware/idempotency/locker.go:82
NewMemoryLock creates a MemoryLock ready for use.
func WasPutToCache(c fiber.Ctx) bool
Source: middleware/idempotency/idempotency.go:82
WasPutToCache reports whether the middleware stored the response produced by the current request in the cache.
Types
type Config
Source: middleware/idempotency/config.go:15
Config defines the config for middleware.
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, without a size limit.
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
}Fields
Lock LockerLock locks an idempotency key.
Optional. Default: an in-memory locker for this process only.
Storage fiber.StorageStorage stores response data by idempotency key.
Optional. Default: an in-memory storage for this process only, without a size limit.
Next func(c fiber.Ctx) boolNext defines a function to skip this middleware when returned true.
Optional. Default: a function which skips the middleware on safe HTTP request method.
KeyHeaderValidate func(string) errorKeyHeaderValidate 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.
KeyScope func(c fiber.Ctx) stringKeyScope 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)
KeyHeader stringKeyHeader is the name of the header that contains the idempotency key.
Optional. Default: X-Idempotency-Key
KeepResponseHeaders []stringKeepResponseHeaders 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)
Lifetime time.DurationLifetime is the maximum lifetime of an idempotency key.
Optional. Default: 30 * time.Minute
DisableValueRedaction boolDisableValueRedaction turns off masking idempotency keys in logs and errors when set to true.
Optional. Default: false
type Locker
Source: middleware/idempotency/locker.go:16
Locker implements a spinlock for a string key.
type Locker interface {
Lock(key string) error
Unlock(key string) error
}Methods
Lock func(key string) errorUnlock func(key string) error
type MemoryLock
Source: middleware/idempotency/locker.go:28
MemoryLock coordinates access to idempotency keys using in-memory locks. MemoryLock is safe for concurrent use.
type MemoryLock struct {
// contains filtered or unexported fields
}func (l *MemoryLock) Lock(key string) error
Source: middleware/idempotency/locker.go:34
Lock acquires the lock for the provided key, creating it when necessary.
func (l *MemoryLock) Unlock(key string) error
Source: middleware/idempotency/locker.go:57
Unlock releases the lock associated with the provided key.