idempotency

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package idempotency stores and replays responses to unsafe requests keyed by a client-supplied Idempotency-Key, so retries do not duplicate side effects. The key is written in the same transaction as the state change it guards (see the widget module), which makes concurrent retries safe as well as sequential ones. This package owns lookup, hashing, and the time-to-live.

Index

Constants

This section is empty.

Variables

View Source
var ErrConflict = errors.New("idempotency key reused with a different request")

ErrConflict is returned when a key is reused with a different request body, which the caller should surface as HTTP 409.

Functions

func Hash

func Hash(parts ...[]byte) string

Hash derives the request fingerprint compared across retries of a key. Each part is length-prefixed so that part boundaries cannot alias.

Types

type Record

type Record struct {
	Status int
	Body   []byte
}

Record is a previously stored response.

type Store

type Store struct {
	// contains filtered or unexported fields
}

func NewStore

func NewStore(pool *pgxpool.Pool, ttl time.Duration) *Store

func (*Store) Lookup

func (s *Store) Lookup(ctx context.Context, key, requestHash string) (*Record, error)

Lookup returns the stored response for key. If the key exists but was stored for a different request fingerprint, it returns ErrConflict.

func (*Store) PurgeExpired

func (s *Store) PurgeExpired(ctx context.Context) (int64, error)

PurgeExpired deletes keys whose TTL has passed and returns how many were removed. Run it periodically so the table does not grow without bound.

func (*Store) TTL

func (s *Store) TTL() time.Duration

TTL is how long a stored key remains replayable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL