Skip to content

Key Strategies

A KeyStrategy is responsible for extracting or generating a unique identifier for a request.

The most common approach. The client is responsible for sending a unique key in a specific HTTP header.

import "github.com/fco-gt/gopotency/key"
// Default behavior: looks for "Idempotency-Key"
manager, err := idempotency.NewManager(idempotency.Config{
KeyStrategy: key.HeaderBased("Idempotency-Key"),
})

Useful when you want to enforce idempotency based on the request content itself, without requiring the client to manage keys.

KeyStrategy: key.BodyHash()

Note: This will generate a SHA-256 hash of the request body.

A hybrid approach. It first tries to find a key in the headers; if not found, it falls back to hashing the request body.

KeyStrategy: key.Composite("Idempotency-Key")

You can implement your own strategy by fulfilling the KeyStrategy interface:

type MyCustomStrategy struct{}
func (s *MyCustomStrategy) Generate(req *idempotency.Request) (string, error) {
// Your custom logic here (e.g., using a combination of UserID and OrderID)
return fmt.Sprintf("user-%d-order-%s", req.UserID, req.Body.OrderID), nil
}