MCPcopy
hub / github.com/gofiber/fiber / New

Function New

middleware/idempotency/idempotency.go:45–182  ·  view source on GitHub ↗

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

(config ...Config)

Source from the content-addressed store, hash-verified

43// New creates idempotency middleware that caches responses keyed by the
44// configured idempotency header.
45func New(config ...Config) fiber.Handler {
46 // Set default config
47 cfg := configDefault(config...)
48
49 redactKeys := !cfg.DisableValueRedaction
50
51 maskKey := func(key string) string {
52 if redactKeys {
53 return redactedKey
54 }
55 return key
56 }
57
58 keepResponseHeadersMap := make(map[string]struct{}, len(cfg.KeepResponseHeaders))
59 for _, h := range cfg.KeepResponseHeaders {
60 // CopyString is needed because utils.ToLower uses UnsafeString
61 // and map keys must be immutable
62 keepResponseHeadersMap[utilsstrings.ToLower(h)] = struct{}{}
63 }
64
65 maybeWriteCachedResponse := func(c fiber.Ctx, key string) (bool, error) {
66 if val, err := cfg.Storage.GetWithContext(c, key); err != nil {
67 return false, fmt.Errorf("failed to read response: %w", err)
68 } else if val != nil {
69 var res response
70 if _, err := res.UnmarshalMsg(val); err != nil {
71 return false, fmt.Errorf("failed to unmarshal response: %w", err)
72 }
73
74 _ = c.Status(res.StatusCode)
75
76 for header, vals := range res.Headers {
77 for _, val := range vals {
78 c.RequestCtx().Response.Header.Add(header, val)
79 }
80 }
81
82 if len(res.Body) != 0 {
83 if err := c.Send(res.Body); err != nil {
84 return true, err
85 }
86 }
87
88 _ = c.Locals(localsKeyIsFromCache, true)
89
90 return true, nil
91 }
92
93 return false, nil
94 }
95
96 return func(c fiber.Ctx) error {
97 // Don't execute middleware if Next returns true
98 if cfg.Next != nil && cfg.Next(c) {
99 return c.Next()
100 }
101
102 // Don't execute middleware if the idempotency key is empty

Callers 13

Test_IdempotencyFunction · 0.70
Benchmark_IdempotencyFunction · 0.70
Test_New_NextSkipFunction · 0.70
Test_New_InvalidKeyFunction · 0.70
Test_New_StorageGetErrorFunction · 0.70
Test_New_UnmarshalErrorFunction · 0.70
Test_New_HandlerErrorFunction · 0.70
Test_New_LockErrorFunction · 0.70
Test_New_StorageSetErrorFunction · 0.70
Test_New_UnlockErrorFunction · 0.70

Calls 15

UnmarshalMsgMethod · 0.95
MarshalMsgMethod · 0.95
ErrorfFunction · 0.92
RespHeaderMethod · 0.80
configDefaultFunction · 0.70
GetWithContextMethod · 0.65
ErrorfMethod · 0.65
StatusMethod · 0.65
AddMethod · 0.65
RequestCtxMethod · 0.65
SendMethod · 0.65
LocalsMethod · 0.65

Tested by 13

Test_IdempotencyFunction · 0.56
Benchmark_IdempotencyFunction · 0.56
Test_New_NextSkipFunction · 0.56
Test_New_InvalidKeyFunction · 0.56
Test_New_StorageGetErrorFunction · 0.56
Test_New_UnmarshalErrorFunction · 0.56
Test_New_HandlerErrorFunction · 0.56
Test_New_LockErrorFunction · 0.56
Test_New_StorageSetErrorFunction · 0.56
Test_New_UnlockErrorFunction · 0.56