You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/middleware/idempotency.md
+38-16Lines changed: 38 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,16 @@ id: idempotency
4
4
5
5
# Idempotency
6
6
7
-
Idempotency middleware for [Fiber](https://github.com/gofiber/fiber) allows for fault-tolerant APIs where duplicate requests — for example due to networking issues on the client-side — do not erroneously cause the same action performed multiple times on the server-side.
7
+
Idempotency middleware for [Fiber](https://github.com/gofiber/fiber) allows for fault-tolerant APIs where duplicate requests—for example due to networking issues on the client-side — do not erroneously cause the same action to be performed multiple times on the server-side.
8
8
9
-
Refer to [datatracker](https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-idempotency-key-header-02) for a better understanding.
9
+
Refer to [IETF RFC 7231 §4.2.2](https://tools.ietf.org/html/rfc7231#section-4.2.2) for definitions of safe and idempotent HTTP methods.
10
+
11
+
## HTTP Method Categories
12
+
13
+
***Safe Methods** (do not modify server state): `GET`, `HEAD`, `OPTIONS`, `TRACE`.
14
+
***Idempotent Methods** (multiple identical requests have the same effect as a single one): all safe methods **plus**`PUT` and `DELETE`.
15
+
16
+
> According to the RFC, safe methods are guaranteed not to change server state, while idempotent methods may change state but make identical requests safe to repeat.
10
17
11
18
## Signatures
12
19
@@ -27,14 +34,29 @@ import (
27
34
)
28
35
```
29
36
30
-
After you initiate your Fiber app, you can use the following possibilities:
37
+
After you initiate your Fiber app, you can configure the middleware:
38
+
39
+
### Default Config (Skip **Safe** Methods)
31
40
32
-
### Default Config
41
+
By default, the `Next` function skips middleware for safe methods only:
33
42
34
43
```go
35
44
app.Use(idempotency.New())
36
45
```
37
46
47
+
### Skip **Idempotent** Methods Instead
48
+
49
+
If you prefer to skip middleware on all idempotent methods (including `PUT`, `DELETE`), override `Next`:
50
+
51
+
```go
52
+
app.Use(idempotency.New(idempotency.Config{
53
+
Next: func(c fiber.Ctx) bool {
54
+
// Skip middleware for idempotent methods (safe + PUT, DELETE)
| Next |`func(fiber.Ctx) bool`|Next defines a function to skip this middleware when returned true. | A function for safe methods |
52
-
| Lifetime |`time.Duration`| Lifetime is the maximum lifetime of an idempotency key. |30 * time.Minute |
53
-
| KeyHeader |`string`| KeyHeader is the name of the header that contains the idempotency key.|"X-Idempotency-Key" |
54
-
| KeyHeaderValidate |`func(string) error`| KeyHeaderValidate defines a function to validate the syntax of the idempotency header.| A function for UUID validation|
55
-
| KeepResponseHeaders |`[]string`| KeepResponseHeaders is a list of headers that should be kept from the original response. |nil (keep all headers) |
56
-
| Lock |`Locker`| Lock locks an idempotency key. | An in-memory locker |
57
-
| Storage |`fiber.Storage`| Storage stores response data by idempotency key. | An in-memory storage |
| Next |`func(fiber.Ctx) bool`|Function to skip this middleware when returning `true`. Choose between `IsMethodSafe` or `IsMethodIdempotent` based on RFC definitions. |`func(c fiber.Ctx) bool { return fiber.IsMethodSafe(c.Method()) }`|
74
+
| Lifetime |`time.Duration`| Maximum lifetime of an idempotency key. |`30 * time.Minute`|
75
+
| KeyHeader |`string`| Header name containing the idempotency key. |`"X-Idempotency-Key"`|
76
+
| KeyHeaderValidate |`func(string) error`| Function to validate idempotency header syntax (e.g., UUID). |UUID length check (`36` characters) |
77
+
| KeepResponseHeaders |`[]string`| List of headers to preserve from original response. |`nil` (keep all headers)|
78
+
| Lock |`Locker`| Locks an idempotency key to prevent race conditions. | In-memory locker|
79
+
| Storage |`fiber.Storage`| Stores response data by idempotency key. | In-memory storage|
58
80
59
-
## Default Config
81
+
## Default Config Values
60
82
61
83
```go
62
84
varConfigDefault = Config{
63
85
Next: func(c fiber.Ctx) bool {
64
-
// Skip middleware if the request was done using a safe HTTP method
86
+
// Skip middleware for safe methods per RFC 7231 §4.2.2
0 commit comments