Home / File/ idempotency.go — fiber Source File

idempotency.go — fiber Source File

Architecture documentation for idempotency.go, a go file in the fiber codebase. 1 imports, 0 dependents.

File go FiberCore Adapters 1 imports 3 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  315a9fa0_19cb_7602_60d2_16dbe20cf673["idempotency.go"]
  adf3d4e8_4d86_86c1_e6cc_281d7b4104af["fmt"]
  315a9fa0_19cb_7602_60d2_16dbe20cf673 --> adf3d4e8_4d86_86c1_e6cc_281d7b4104af
  style 315a9fa0_19cb_7602_60d2_16dbe20cf673 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

package idempotency

import (
	"fmt"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/log"
	"github.com/gofiber/utils/v2"
)

// Inspired by https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header-02
// and https://github.com/penguin-statistics/backend-next/blob/f2f7d5ba54fc8a58f168d153baa17b2ad4a14e45/internal/pkg/middlewares/idempotency.go

// The contextKey type is unexported to prevent collisions with context keys defined in
// other packages.
type contextKey int

const (
	localsKeyIsFromCache contextKey = iota //
	localsKeyWasPutToCache
)

const redactedKey = "[redacted]"

// IsFromCache reports whether the middleware served the response from the
// cache for the current request.
func IsFromCache(c fiber.Ctx) bool {
	return c.Locals(localsKeyIsFromCache) != nil
}

// WasPutToCache reports whether the middleware stored the response produced by
// the current request in the cache.
func WasPutToCache(c fiber.Ctx) bool {
	val := c.Locals(localsKeyWasPutToCache)
	if wasPut, ok := val.(bool); ok {
		return wasPut
	}
	return val != nil
}

// New creates idempotency middleware that caches responses keyed by the
// configured idempotency header.
func New(config ...Config) fiber.Handler {
	// Set default config
	cfg := configDefault(config...)

	redactKeys := !cfg.DisableValueRedaction

	maskKey := func(key string) string {
		if redactKeys {
			return redactedKey
		}
		return key
	}

	keepResponseHeadersMap := make(map[string]struct{}, len(cfg.KeepResponseHeaders))
	for _, h := range cfg.KeepResponseHeaders {
		// CopyString is needed because utils.ToLower uses UnsafeString
		// and map keys must be immutable
		keepResponseHeadersMap[utils.CopyString(utils.ToLower(h))] = struct{}{}
// ... (127 more lines)

Domain

Subdomains

Classes

Types

Dependencies

  • fmt

Frequently Asked Questions

What does idempotency.go do?
idempotency.go is a source file in the fiber codebase, written in go. It belongs to the FiberCore domain, Adapters subdomain.
What functions are defined in idempotency.go?
idempotency.go defines 3 function(s): IsFromCache, New, WasPutToCache.
What does idempotency.go depend on?
idempotency.go imports 1 module(s): fmt.
Where is idempotency.go in the architecture?
idempotency.go is located at middleware/idempotency/idempotency.go (domain: FiberCore, subdomain: Adapters, directory: middleware/idempotency).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free