Documentation
¶
Overview ¶
Package idempotent provides storage-backed duplicate suppression for operations.
Index ¶
- Variables
- func FingerprintBytes(data []byte) string
- func FingerprintJSON(value any) string
- func FingerprintString(value string) string
- func ValidateKey(key Key) error
- type AcquireRequest
- type AcquireResult
- type CallOption
- type CompleteRequest
- type ConflictError
- type ErrorClass
- type ErrorClassifier
- type Event
- type Executor
- func (e *Executor[T]) Do(ctx context.Context, key Key, operation func(context.Context) (T, error)) (Result[T], error)
- func (e *Executor[T]) DoWithOptions(ctx context.Context, key Key, operation func(context.Context) (T, error), ...) (Result[T], error)
- func (e *Executor[T]) Renew(ctx context.Context, key Key, token OwnerToken, fence Fence) error
- type FailRequest
- type FailurePolicy
- type Fence
- type FingerprintPolicy
- type Hooks
- type Key
- type KeyHasher
- type Option
- func WithCodec[T any](value codec.Codec[T]) Option[T]
- func WithErrorClassifier[T any](value ErrorClassifier) Option[T]
- func WithFailurePolicy[T any](value FailurePolicy) Option[T]
- func WithFailureTTL[T any](value time.Duration) Option[T]
- func WithFingerprintPolicy[T any](value FingerprintPolicy) Option[T]
- func WithHeartbeatInterval[T any](value time.Duration) Option[T]
- func WithHooks[T any](value Hooks[T]) Option[T]
- func WithKeyHasher[T any](value KeyHasher) Option[T]
- func WithLeaseDuration[T any](value time.Duration) Option[T]
- func WithMaxKeyLength[T any](value int) Option[T]
- func WithMaxResultSize[T any](value int) Option[T]
- func WithMinKeyLength[T any](value int) Option[T]
- func WithNamespace[T any](value string) Option[T]
- func WithPanicPolicy[T any](value PanicPolicy) Option[T]
- func WithPendingPolicy[T any](value PendingPolicy) Option[T]
- func WithStore[T any](store Store[T]) Option[T]
- func WithSuccessTTL[T any](value time.Duration) Option[T]
- func WithTTL[T any](value time.Duration) Option[T]
- func WithWaitTimeout[T any](value time.Duration) Option[T]
- type OwnerToken
- type Ownership
- type PanicPolicy
- type PendingPolicy
- type Record
- type RenewRequest
- type Result
- type State
- type Store
- type StoredError
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidKey = errors.New("invalid idempotency key") ErrFingerprintMismatch = errors.New("idempotency fingerprint mismatch") ErrInProgress = errors.New("operation already in progress") ErrWaitTimeout = errors.New("timed out waiting for operation") ErrOwnershipLost = errors.New("idempotency ownership lost") ErrRecordExpired = errors.New("idempotency record expired") ErrResultTooLarge = errors.New("idempotency result too large") ErrCodec = errors.New("idempotency codec failure") )
Functions ¶
func FingerprintBytes ¶
func FingerprintJSON ¶
FingerprintJSON creates a deterministic digest for JSON-compatible values.
func FingerprintString ¶
func ValidateKey ¶
ValidateKey rejects empty keys and keys containing control characters. Executors additionally enforce their configured length limits.
Types ¶
type AcquireRequest ¶
type AcquireResult ¶
type CallOption ¶
type CallOption func(*callConfig)
func WithFingerprint ¶
func WithFingerprint(value string) CallOption
func WithMetadata ¶
func WithMetadata(value map[string]string) CallOption
type CompleteRequest ¶
type ConflictError ¶
ConflictError describes reuse of a key with a different request fingerprint.
func (*ConflictError) Error ¶
func (e *ConflictError) Error() string
func (*ConflictError) Unwrap ¶
func (e *ConflictError) Unwrap() error
type ErrorClass ¶
type ErrorClass string
const ( ErrorRetryable ErrorClass = "retryable" ErrorPermanent ErrorClass = "permanent" ErrorUnknown ErrorClass = "unknown" )
type ErrorClassifier ¶
type ErrorClassifier interface{ Classify(error) ErrorClass }
type Event ¶
type Event struct {
KeyHash, Namespace string
State State
Owner, Replayed bool
Attempt int
Duration time.Duration
Store string
}
Event contains safe lifecycle metadata. KeyHash never includes the raw key.
type Executor ¶
type Executor[T any] struct { // contains filtered or unexported fields }
Executor runs typed operations using one Store.
func (*Executor[T]) DoWithOptions ¶
type FailRequest ¶
type FailurePolicy ¶
type FailurePolicy string
const ( NeverCacheFailures FailurePolicy = "never" CacheAllFailures FailurePolicy = "all" CacheSelectedFailures FailurePolicy = "selected" CachePermanentFailures FailurePolicy = "permanent" )
type FingerprintPolicy ¶
type FingerprintPolicy string
const ( FingerprintDisabled FingerprintPolicy = "disabled" FingerprintOptional FingerprintPolicy = "optional" FingerprintRequired FingerprintPolicy = "required" )
type Hooks ¶
type Hooks[T any] struct { OnAcquire func(Event) OnReplay func(Event) OnConflict func(Event) OnComplete func(Event, T) OnFailure func(Event, error) OnOwnershipLost func(Event) OnWait func(Event) OnStaleReclaimed func(Event) }
Hooks receives lifecycle events. Panics from callbacks are recovered.
type Option ¶
type Option[T any] func(*config[T])
func WithErrorClassifier ¶
func WithErrorClassifier[T any](value ErrorClassifier) Option[T]
func WithFailurePolicy ¶
func WithFailurePolicy[T any](value FailurePolicy) Option[T]
func WithFingerprintPolicy ¶
func WithFingerprintPolicy[T any](value FingerprintPolicy) Option[T]
func WithKeyHasher ¶
func WithMaxKeyLength ¶
func WithMaxResultSize ¶
func WithMinKeyLength ¶
WithMinKeyLength configures the shortest accepted persisted key.
func WithNamespace ¶
func WithPanicPolicy ¶
func WithPanicPolicy[T any](value PanicPolicy) Option[T]
func WithPendingPolicy ¶
func WithPendingPolicy[T any](value PendingPolicy) Option[T]
type OwnerToken ¶
type OwnerToken string
type Ownership ¶
type Ownership interface {
Lost() <-chan struct{}
}
Ownership exposes loss of the lease held by the running operation. A closed Lost channel means the operation must stop any critical side effects.
type PanicPolicy ¶
type PanicPolicy string
const ( PanicPropagate PanicPolicy = "propagate" PanicAsFailure PanicPolicy = "failure" )
type PendingPolicy ¶
type PendingPolicy string
const ( PendingWait PendingPolicy = "wait" PendingReject PendingPolicy = "reject" PendingPoll PendingPolicy = "poll" PendingReturn PendingPolicy = "return_pending" )
type Record ¶
type Record[T any] struct { Key Key Fingerprint string Metadata map[string]string State State OwnerToken OwnerToken Fence Fence Value []byte Error string CreatedAt, UpdatedAt, CompletedAt, LeaseExpiresAt, ExpiresAt time.Time Attempt int }
Record is the durable state maintained by a Store.
type RenewRequest ¶
type Result ¶
type Result[T any] struct { Value T State State Replayed, Owner bool CreatedAt, CompletedAt, ExpiresAt time.Time Attempt int }
Result reports both the value and how it was obtained.
type Store ¶
type Store[T any] interface { Acquire(ctx context.Context, request AcquireRequest) (AcquireResult[T], error) Complete(ctx context.Context, request CompleteRequest[T]) error Fail(ctx context.Context, request FailRequest) error Get(ctx context.Context, key Key) (Record[T], error) Renew(ctx context.Context, request RenewRequest) error Delete(ctx context.Context, key Key) error Cleanup(ctx context.Context, now time.Time, limit int) (int, error) }
Store atomically grants execution ownership and fences state transitions.
type StoredError ¶
type StoredError struct{ Message string }
StoredError is a replayable representation of an operation error. Its original Go type is deliberately not reconstructed.
func (*StoredError) Error ¶
func (e *StoredError) Error() string
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
http-api
command
|
|
|
worker
command
|
|
|
Package middleware contains net/http idempotency middleware.
|
Package middleware contains net/http idempotency middleware. |
|
store
|
|
|
contract
Package contract provides a conformance suite for Store implementations.
|
Package contract provides a conformance suite for Store implementations. |
|
memory
Package memory provides a process-local Store implementation.
|
Package memory provides a process-local Store implementation. |
|
sqlite
Package sqlite provides a SQLite Store for single-node and embedded services.
|
Package sqlite provides a SQLite Store for single-node and embedded services. |