idempotency

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package idempotency provides command deduplication for the dashboard contract: a Store interface plus an in-memory implementation. Wrappers around dispatcher.Dispatch consult the store before invoking command handlers and return cached envelopes when the (key, identity) tuple matches a recent invocation.

Index

Constants

View Source
const DefaultMaxEntries = 10000

DefaultMaxEntries is the default LRU cap for an in-memory store.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cached

type Cached struct {
	// Status is the HTTP status the original handler returned.
	Status int
	// WireBody is the JSON envelope the original handler produced, ready to
	// write back verbatim.
	WireBody json.RawMessage
	// StoredAt is when this entry landed in the store.
	StoredAt time.Time
	// TTL is how long the entry is considered fresh.
	TTL time.Duration
}

Cached is one cached command response.

func (Cached) Expired

func (c Cached) Expired(now time.Time) bool

Expired reports whether c is past its TTL relative to now.

type InMemoryStore

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

InMemoryStore is a process-local Store with TTL and LRU eviction. Safe for concurrent use.

func NewInMemoryStore

func NewInMemoryStore(opts ...Option) *InMemoryStore

NewInMemoryStore returns an in-memory Store with the given options.

func (*InMemoryStore) Lookup

func (s *InMemoryStore) Lookup(_ context.Context, key, identity string) (*Cached, bool)

Lookup implements Store.

func (*InMemoryStore) Store

func (s *InMemoryStore) Store(_ context.Context, key, identity string, c Cached) error

Store implements Store. Returns nil; signature reserves error for future backends (e.g., Redis).

type Option

type Option func(*InMemoryStore)

Option configures an InMemoryStore.

func WithMaxEntries

func WithMaxEntries(n int) Option

WithMaxEntries caps the number of cached entries; oldest are evicted first.

type Store

type Store interface {
	Lookup(ctx context.Context, key, identity string) (*Cached, bool)
	Store(ctx context.Context, key, identity string, c Cached) error
}

Store deduplicates command invocations by (key, identity) tuple. Lookup returns a cached envelope if one is present and unexpired; the dispatcher writes back the cached envelope verbatim when found. Implementations MUST be safe for concurrent use.

Jump to

Keyboard shortcuts

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