hedgepolicy

package
v0.9.5 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package hedgepolicy provides a HedgePolicy.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder added in v0.7.0

type Builder[R any] interface {
	// CancelOnResult specifies that any outstanding hedges should be canceled if the execution result matches the result using
	// reflect.DeepEqual.
	CancelOnResult(result R) Builder[R]

	// CancelOnErrors specifies that any outstanding hedges should be canceled if the execution error matches any of the errors
	// using errors.Is.
	CancelOnErrors(errs ...error) Builder[R]

	// CancelOnErrorTypes specifies the errors whose types should cause any outstanding hedges to be canceled. Any execution
	// errors or their Unwrapped parents whose type matches any of the errs' types will cause outstanding hedges to be
	// canceled. This is similar to the check that errors.As performs.
	CancelOnErrorTypes(errs ...any) Builder[R]

	// CancelIf specifies that any outstanding hedges should be canceled if the predicate matches the result or error.
	CancelIf(predicate func(R, error) bool) Builder[R]

	// WithMaxHedges sets the max number of hedges to perform when an execution attempt doesn't complete in time, which is 1
	// by default.
	WithMaxHedges(maxHedges int) Builder[R]

	// WithBudget configures a hedge budget. When the hedgeBudget is exceeded, hedges will stop with budget.ErrExceeded.
	WithBudget(hedgeBudget budget.Budget) Builder[R]

	// OnHedge registers the listener to be called when a hedge is about to be attempted.
	OnHedge(listener func(failsafe.ExecutionEvent[R])) Builder[R]

	// Build returns a new HedgePolicy using the builder's configuration.
	Build() HedgePolicy[R]
}

Builder builds HedgePolicy instances.

R is the execution result type. This type is not concurrency safe.

func NewBuilderWithDelay added in v0.7.0

func NewBuilderWithDelay[R any](delay time.Duration) Builder[R]

NewBuilderWithDelay returns a new Builder for execution result type R and the delay, which by default will allow a single hedged execution to be performed, after the delay is elapsed, if the original execution is not done yet. Additional hedged executions will be performed, with delay, up to the max configured hedges.

If the execution is configured with a Context, a child context will be created for the execution and canceled when the HedgePolicy is exceeded.

func NewBuilderWithDelayFunc added in v0.7.0

func NewBuilderWithDelayFunc[R any](delayFunc failsafe.DelayFunc[R]) Builder[R]

NewBuilderWithDelayFunc returns a new Builder for execution result type R and the delayFunc, which by default will allow a single hedged execution to be performed, after the delayFunc result is elapsed, if the original execution is not done yet. Additional hedged executions will be performed, after additional delays, up to the max configured hedges.

If the execution is configured with a Context, a child context will be created for the execution and canceled when the HedgePolicy is exceeded.

type HedgePolicy

type HedgePolicy[R any] interface {
	failsafe.Policy[R]
}

HedgePolicy is a policy that performes additional execution attempts if the initial execution is slow to complete, with a delay between attempts. This policy differs from RetryPolicy since multiple hedged executions may be in progress at the same time. By default, any outstanding hedges are canceled after the first execution result or error returns. The CancelOn and CancelIf methods can be used to configure a hedge policy to cancel after different results, errors, or conditions. Once the max hedges have been started, they are left to run until a cancellable result is returned, then the remaining hedges are canceled.

If the execution is configured with a Context, a child context will be created for each attempt and outstanding contexts are canceled when the HedgePolicy is exceeded.

R is the execution result type. This type is concurrency safe.

func NewWithDelay added in v0.7.0

func NewWithDelay[R any](delay time.Duration) HedgePolicy[R]

NewWithDelay returns a new HedgePolicy for execution result type R and the delay, which by default will allow a single hedged execution to be performed, after the delay is elapsed, if the original execution is not done yet. Additional hedged executions will be performed, with delay, up to the max configured hedges.

If the execution is configured with a Context, a child context will be created for the execution and canceled when the HedgePolicy is exceeded.

func NewWithDelayFunc added in v0.7.0

func NewWithDelayFunc[R any](delayFunc failsafe.DelayFunc[R]) HedgePolicy[R]

NewWithDelayFunc returns a new HedgePolicy for execution result type R and the delayFunc, which by default will allow a single hedged execution to be performed, after the delayFunc result is elapsed, if the original execution is not done yet. Additional hedged executions will be performed, with delay, up to the max configured hedges.

If the execution is configured with a Context, a child context will be created for the execution and canceled when the HedgePolicy is exceeded.

Jump to

Keyboard shortcuts

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