idempotency

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package idempotency coordinates request attempts using expiring ownership leases.

Index

Constants

This section is empty.

Variables

View Source
var ErrBusy = errors.New("idempotency: request processing")
View Source
var ErrCompleted = errors.New("idempotency: request already completed")
View Source
var ErrMismatch = errors.New("idempotency: request fingerprint mismatch")
View Source
var ErrOwnership = errors.New("idempotency: ownership lost")

Functions

func Middleware

func Middleware(c HTTPConfig) (func(http.Handler) http.Handler, error)

Middleware returns context-aware HTTP admission or coordination middleware.

Types

type HTTPConfig

type HTTPConfig struct {
	Store                            Store
	Scope                            func(*http.Request) string
	Lease, Retention, RequestTimeout time.Duration
	MaxBodyBytes                     int64
	OnError                          func(context.Context, error)
}

HTTPConfig requires a server-defined authorization/tenant scope. Bodies are fingerprinted, never persisted. Completed duplicates receive 409 and Idempotency-Status: completed; response bodies are not replayed. RequestTimeout must be shorter than Lease. A handler must honor cancellation; use durable business keys for irreversible side effects.

type Memory

type Memory struct {
	MaxKeys int
	Now     func() time.Time
	// contains filtered or unexported fields
}

Memory is bounded. Expired processing leases may be reclaimed; operations must stop when renewal fails.

func NewMemory

func NewMemory(maxKeys int) (*Memory, error)

NewMemory creates a bounded process-local idempotency store.

func (*Memory) Begin

func (m *Memory) Begin(ctx context.Context, key, fingerprint string, lease time.Duration) (Record, error)

Begin atomically attempts to acquire a fingerprint-bound processing lease.

func (*Memory) Complete

func (m *Memory) Complete(ctx context.Context, key, token string, status int, ttl time.Duration) error

Complete records successful processing only for the current ownership token.

func (*Memory) Fail

func (m *Memory) Fail(ctx context.Context, key, token string, ttl time.Duration) error

Fail records failed processing only for the current ownership token.

func (*Memory) Renew

func (m *Memory) Renew(ctx context.Context, key, token string, ttl time.Duration) error

Renew extends a current processing lease.

type Record

type Record struct {
	Fingerprint string    `json:"fingerprint"`
	Token       string    `json:"token"`
	State       State     `json:"state"`
	Status      int       `json:"status"`
	Expires     time.Time `json:"expires"`
}

Record contains persisted processing or event metadata.

type State

type State string

State identifies a processing or circuit-breaker state.

const (
	Processing State = "processing"
	Completed  State = "completed"
	Failed     State = "failed"
)

type Store

Store defines atomic lease-based idempotency transitions.

Jump to

Keyboard shortcuts

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