Gin Middleware
The Gin middleware is the easiest way to integrate idempotency into your Gin application.
Installation
Section titled “Installation”go get github.com/fco-gt/gopotency/middleware/ginBasic Usage
Section titled “Basic Usage”import ( "github.com/fco-gt/gopotency" ginmw "github.com/fco-gt/gopotency/middleware/gin" "github.com/fco-gt/gopotency/storage/memory")
func main() { manager, _ := idempotency.NewManager(idempotency.Config{ Storage: memory.NewMemoryStorage(), })
r := gin.Default() r.Use(ginmw.Idempotency(manager))
r.POST("/payments", handlePayment) r.Run()}How it behaves
Section titled “How it behaves”- Automatic Detection: It automatically extracts the idempotency key based on your configuration.
- Replay Header: If a response is replayed from cache, it adds
X-Idempotent-Replayed: trueto the response headers. - Status Codes:
409 Conflict: If a request with the same key is already being processed.422 Unprocessable Entity: If the request body differs from the original request for that key.