idempotency

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderKey is the standard idempotency key header
	HeaderKey = "Idempotency-Key"

	// DefaultTTL is the default cache duration for idempotent responses (24 hours)
	DefaultTTL = 24 * time.Hour
)

Variables

This section is empty.

Functions

func Middleware

func Middleware(store Store, ttl time.Duration) func(http.Handler) http.Handler

Middleware creates idempotency middleware for payment endpoints

Types

type MemoryStore

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

MemoryStore is an in-memory implementation of Store with LRU eviction

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory idempotency store with a maximum of 10,000 entries

func NewMemoryStoreWithSize

func NewMemoryStoreWithSize(maxSize int) *MemoryStore

NewMemoryStoreWithSize creates a new in-memory idempotency store with custom max size

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(ctx context.Context, key string) error

Delete removes a cached response

func (*MemoryStore) Get

func (s *MemoryStore) Get(ctx context.Context, key string) (*Response, bool)

Get retrieves a cached response for the given key

func (*MemoryStore) Set

func (s *MemoryStore) Set(ctx context.Context, key string, response *Response, ttl time.Duration) error

Set stores a response for the given key with TTL

func (*MemoryStore) Stop

func (s *MemoryStore) Stop()

Stop gracefully shuts down the cleanup goroutine

type Response

type Response struct {
	StatusCode int
	Headers    map[string]string
	Body       []byte
	CachedAt   time.Time
}

Response represents a cached idempotent response

type Store

type Store interface {
	// Get retrieves a cached response for the given key
	Get(ctx context.Context, key string) (*Response, bool)

	// Set stores a response for the given key with TTL
	Set(ctx context.Context, key string, response *Response, ttl time.Duration) error

	// Delete removes a cached response
	Delete(ctx context.Context, key string) error
}

Store manages idempotency keys and cached responses

Jump to

Keyboard shortcuts

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