Skip to content

Request Hashing

RequestHasher is used to detect if two requests with the same idempotency key have different content. This prevents a classic edge case where a client reuses a key for a completely different operation.

The simplest approach. It hashes exclusively the request body.

import "github.com/fco-gt/gopotency/hash"
manager, err := idempotency.NewManager(idempotency.Config{
Storage: store,
RequestHasher: hash.BodyHasher(),
})

A stricter approach that combines the HTTP Method, Path, and Body.

RequestHasher: hash.FullHasher()

The data used for the hash follows the pattern: METHOD:PATH:BODY.

You can implement your own hashing logic by fulfilling the RequestHasher interface:

type MyHasher struct{}
func (h *MyHasher) Hash(req *idempotency.Request) (string, error) {
// Custom logic: e.g., hash specific headers + body
data := fmt.Sprintf("%s:%s", req.Headers["X-Custom-Context"], string(req.Body))
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:]), nil
}