idempotency

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package idempotency replays client-token creates: a retry with the same token and parameters returns the resource the first call made.

Index

Constants

This section is empty.

Variables

View Source
var ErrParamsMismatch = errors.New("client token was already used with different parameters")

ErrParamsMismatch is returned when a token is reused with different parameters.

Functions

func Create

func Create[T any](
	m *Memo, op, token, fingerprint string,
	idOf func(*T) string, get func(string) (*T, error), create func() (*T, error),
) (*T, error)

Create returns the live resource recorded for (op, token) or runs create and records it. An empty token always runs create; a reused token with a different fingerprint yields ErrParamsMismatch.

func Fingerprint

func Fingerprint(v any) string

Fingerprint renders request parameters for comparison across retries.

func Replay

func Replay[T any](m *Memo, op, token, fingerprint string, do func() (*T, error)) (*T, error)

Replay returns the response recorded for (op, token) or runs do and records its response. An empty token always runs do; a reused token with a different fingerprint yields ErrParamsMismatch.

Types

type Memo

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

Memo remembers which resource a (op, token) created; bounded by TTL and entry cap.

func New

func New(name string, opts ...Option) *Memo

New returns a Memo whose lock metrics are labelled name.

func (*Memo) Clear

func (m *Memo) Clear()

Clear forgets every remembered token.

func (*Memo) Len

func (m *Memo) Len() int

Len reports the number of remembered tokens.

func (*Memo) Lookup

func (m *Memo) Lookup(op, token, fingerprint string) (string, bool, error)

Lookup returns the ID recorded for a live (op, token); ErrParamsMismatch when fingerprints differ.

func (*Memo) Record

func (m *Memo) Record(op, token, fingerprint, id string)

Record remembers id as the resource (op, token) created.

type Option

type Option func(*Memo)

Option configures a Memo.

func WithClock

func WithClock(now func() time.Time) Option

WithClock overrides the time source.

func WithLimits

func WithLimits(ttl time.Duration, maxEntries int) Option

WithLimits overrides the entry lifetime and cap.

Jump to

Keyboard shortcuts

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