modulepackage
0.0.0-20240207131029-bd953129507a
Repository: https://github.com/preciselyco/idempotency.git
Documentation: pkg.go.dev
# README
Idempotency - A middleware for idempotency
This is a middleware that implements an RFC draft for idempotency-key-header.
Example code:
idempotencyMiddleware := idempotency.New(idempotency.NewMemoryStorage(),
func(idempotencyKey string, w http.ResponseWriter, r *http.Request) {
req, err := _state.FindByKey(idempotencyKey)
if err != nil {
http.Error(w, "No such key", http.StatusBadRequest)
return
}
// Return content previously returned by myHandler.
})
http.Handle("/", idempotencyMiddleware.Verify(myHandler))
# Functions
FromContext returns the Idempotency Key value stored in ctx, if any.
New creates a new idempotency state.
NewContext returns a new Context that carries value idempotencyKey.
NewMemoryStorage creates a memory storage for Idempotency-Keys to be able to provide stateful functionality.
NewRedisStorage creates a Redis storage for Idempotency-Keys to be able to provide a distributed state of the keys.
WithErrorResponder configures a function that responds to the client whenever an error occurs.
No description provided by the author
WithRestorer configures the function that restores a previous payload from storage.
# Structs
RequestStatus keeps track of requests that are in process and what body sum they have, this is to check wether to return a Conflict or a Unprocessable Entity.
# Interfaces
Storage is a interface to implement storing and getting idempotency keys.
# Type aliases
Option is the functional option signature for configuring idempotency.
RedisStorageOption is the signature for functional options for the Redis storage.