idempotency

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package idempotency provides a contrib in-memory idempotency store.

The memory store is useful for local development, tests, and examples. It is not a durable production store; production APIs should use storage with shared process visibility and retention aligned with retry windows.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LegacyInFlightRecoveryEvent added in v2.1.0

type LegacyInFlightRecoveryEvent struct {
	// Key is hashed by default. It contains the raw key only when
	// MemoryStoreOptions.LegacyInFlightRecoveryRawKey is explicitly enabled.
	Key string
	// KeyHash always contains the hashed key value when a key is available.
	KeyHash string
	// RawKey is populated only when LegacyInFlightRecoveryRawKey is explicitly enabled.
	RawKey  string
	Outcome LegacyInFlightRecoveryOutcome
}

LegacyInFlightRecoveryEvent carries structured context for legacy token recovery.

type LegacyInFlightRecoveryHandler added in v2.1.0

type LegacyInFlightRecoveryHandler func(context.Context, LegacyInFlightRecoveryEvent)

LegacyInFlightRecoveryHandler is invoked when a tokenless legacy recovery branch is encountered. Keep handlers fast and non-blocking.

type LegacyInFlightRecoveryOutcome added in v2.1.0

type LegacyInFlightRecoveryOutcome string

LegacyInFlightRecoveryOutcome captures whether a legacy tokenless recovery path was exercised in the adapter.

const (
	// LegacyInFlightRecoveryRecovered indicates tokenless legacy in-flight state
	// was intentionally migrated away.
	LegacyInFlightRecoveryRecovered LegacyInFlightRecoveryOutcome = "legacy_in_flight_recovered"
	// LegacyInFlightRecoveryTokenMismatch indicates a token was supplied for a
	// tokenless legacy in-flight record and release was rejected.
	LegacyInFlightRecoveryTokenMismatch LegacyInFlightRecoveryOutcome = "legacy_in_flight_token_mismatch"
)

type MemoryStore

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

MemoryStore is an in-memory idempotency store intended for development/testing.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore constructs an in-memory idempotency store.

func NewMemoryStoreWithOptions added in v2.1.0

func NewMemoryStoreWithOptions(opts MemoryStoreOptions) *MemoryStore

NewMemoryStoreWithOptions constructs an in-memory idempotency store with optional observability hooks.

func (*MemoryStore) Get

Get returns an idempotency record if present and not expired.

func (*MemoryStore) Release added in v2.1.0

func (m *MemoryStore) Release(ctx context.Context, key string) error

Release removes an in-flight idempotency reservation by key.

This method preserves the v2 compatibility contract. New middleware uses ReleaseReservation when available so tokened reservations are not released by unrelated requests.

func (*MemoryStore) ReleaseReservation added in v2.1.0

func (m *MemoryStore) ReleaseReservation(ctx context.Context, key, token string) error

ReleaseReservation removes a stored idempotency reservation when token matches.

func (*MemoryStore) Save

func (m *MemoryStore) Save(ctx context.Context, key string, record ports.IdempotencyRecord, ttl time.Duration) error

Save persists a completed idempotency record.

func (*MemoryStore) TryBegin

func (m *MemoryStore) TryBegin(ctx context.Context, key string, record ports.IdempotencyRecord, ttl time.Duration) (bool, error)

TryBegin reserves a key for an in-flight request.

type MemoryStoreOptions added in v2.1.0

type MemoryStoreOptions struct {
	OnLegacyInFlightRecovery LegacyInFlightRecoveryHandler
	// LegacyInFlightRecoveryRawKey exposes raw idempotency keys in recovery events.
	// Defaults to false so adapter telemetry receives hashed keys.
	LegacyInFlightRecoveryRawKey bool
}

MemoryStoreOptions configures legacy recovery telemetry for memory store usage.

Jump to

Keyboard shortcuts

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