Skip to content

Commit ab7f949

Browse files
authored
📒 docs: Idempotency add more detailed description for next method (#3443)
* add more detailed description for next method
1 parent 4321dfe commit ab7f949

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

docs/middleware/idempotency.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ id: idempotency
44

55
# Idempotency
66

7-
Idempotency middleware for [Fiber](https://github.com/gofiber/fiber) allows for fault-tolerant APIs where duplicate requestsfor 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 requestsfor 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.
88

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.
1017
1118
## Signatures
1219

@@ -27,14 +34,29 @@ import (
2734
)
2835
```
2936

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)
3140

32-
### Default Config
41+
By default, the `Next` function skips middleware for safe methods only:
3342

3443
```go
3544
app.Use(idempotency.New())
3645
```
3746

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)
55+
return fiber.IsMethodIdempotent(c.Method())
56+
},
57+
}))
58+
```
59+
3860
### Custom Config
3961

4062
```go
@@ -44,24 +66,24 @@ app.Use(idempotency.New(idempotency.Config{
4466
}))
4567
```
4668

47-
### Config
69+
## Config
4870

49-
| Property | Type | Description | Default |
50-
|:--------------------|:------------------------|:-----------------------------------------------------------------------------------------|:-------------------------------|
51-
| 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 |
71+
| Property | Type | Description | Default |
72+
|:--------------------|:-----------------------|:----------------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------|
73+
| 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 |
5880

59-
## Default Config
81+
## Default Config Values
6082

6183
```go
6284
var ConfigDefault = Config{
6385
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
6587
return fiber.IsMethodSafe(c.Method())
6688
},
6789

0 commit comments

Comments
 (0)