policy

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package policy provides shared retry and failure-classification primitives for evt-backed handlers.

Index

Constants

View Source
const (
	// DefaultMaxAttempts is the default number of retry attempts for commit paths.
	DefaultMaxAttempts = 5

	// DefaultBaseDelay is the default base delay between retries.
	DefaultBaseDelay = 100 * time.Millisecond

	// DefaultMaxDelay is the default maximum delay between retries.
	DefaultMaxDelay = 2 * time.Second
)

Variables

This section is empty.

Functions

func AllTransient

func AllTransient(errs []error) bool

AllTransient reports whether every provided error is transient.

func Execute

func Execute(ctx context.Context, cfg Config, op func() error, hooks Hooks) error

Execute runs op with bounded retries using the configured policy.

func ExecuteWithRetry

func ExecuteWithRetry(
	ctx context.Context,
	store evt.Store,
	factory evt.EntityFactory,
	entityID evt.EntityID,
	command evt.Command,
	metadata evt.Metadata,
	cfg Config,
	hooks Hooks,
) (evt.Entity, error)

ExecuteWithRetry runs Store.Execute inside a bounded retry loop. On transient commit failures the entity is reloaded from scratch via the factory before the next attempt. Errors returned on give-up are wrapped in ClassifiedError.

func ExponentialJitterDelay

func ExponentialJitterDelay(baseDelay, maxDelay time.Duration, attempt int, jitter func(time.Duration) time.Duration) time.Duration

ExponentialJitterDelay returns bounded exponential backoff with jitter in [base/2, base].

func IsClassifiedErr

func IsClassifiedErr(err error) bool

IsClassifiedErr returns true when err is a ClassifiedError.

func IsPermanent

func IsPermanent(err error) bool

IsPermanent reports whether an error should be dropped to DLQ without retries.

func IsTransient

func IsTransient(err error) bool

IsTransient reports whether an error should be retried.

func SleepWithContext

func SleepWithContext(ctx context.Context, delay time.Duration) error

SleepWithContext blocks for delay unless context is canceled.

Types

type Attempt

type Attempt struct {
	Attempt int
	Delay   time.Duration
	Class   Class
	Err     error
}

Attempt contains retry telemetry fields emitted by Execute.

type Class

type Class string

Class identifies whether an error should be retried.

const (
	// ClassUnknown is used when an error doesn't match a known policy rule.
	ClassUnknown Class = "unknown"
	// ClassTransient indicates retryable failures.
	ClassTransient Class = "transient"
	// ClassPermanent indicates non-retryable failures.
	ClassPermanent Class = "permanent"
)

func Classify

func Classify(err error) Class

Classify maps errors already wrapped with ClassifiedError into their stored class. Backend-specific packages can provide richer classifiers.

type ClassifiedError

type ClassifiedError struct {
	Class Class
	Err   error
}

ClassifiedError wraps an error with its failure class so callers can inspect the classification without re-calling Classify.

func WrapClassified

func WrapClassified(err error) *ClassifiedError

WrapClassified classifies err and wraps it in a ClassifiedError.

func (*ClassifiedError) Error

func (e *ClassifiedError) Error() string

Error returns the underlying error message.

func (*ClassifiedError) Unwrap

func (e *ClassifiedError) Unwrap() error

Unwrap returns the underlying error.

type Classifier

type Classifier func(error) Class

Classifier maps an error into a retry/failure class.

type Config

type Config struct {
	MaxAttempts int
	BaseDelay   time.Duration
	MaxDelay    time.Duration
	Jitter      func(time.Duration) time.Duration
	Sleep       func(context.Context, time.Duration) error
	IsRetryable func(error) bool
	Classify    Classifier
}

Config controls retry behavior for optimistic-concurrency commit paths.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults for optimistic-concurrency retry on evt Store commit paths.

type Hooks

type Hooks struct {
	OnRetry  func(Attempt)
	OnGiveUp func(Attempt)
}

Hooks emits retry lifecycle callbacks for telemetry.

Directories

Path Synopsis
Package dynamodb provides DynamoDB-specific retry classification for evt policy.
Package dynamodb provides DynamoDB-specific retry classification for evt policy.

Jump to

Keyboard shortcuts

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