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 HedgePolicy ¶
type HedgePolicy[R any] interface { failsafe.Policy[R] }
HedgePolicy is a policy that performes additional executions if the initial execution is slow to complete. This policy differs from RetryPolicy since multiple hedged execution 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.
This type is concurrency safe.
func WithDelay ¶
func WithDelay[R any](delay time.Duration) HedgePolicy[R]
WithDelay 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 WithDelayFunc ¶
func WithDelayFunc[R any](delayFunc failsafe.DelayFunc[R]) HedgePolicy[R]
WithDelayFunc 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.
type HedgePolicyBuilder ¶
type HedgePolicyBuilder[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) HedgePolicyBuilder[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) HedgePolicyBuilder[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) HedgePolicyBuilder[R] // CancelIf specifies that any outstanding hedges should be canceled if the predicate matches the result or error. CancelIf(predicate func(R, error) bool) HedgePolicyBuilder[R] // OnHedge registers the listener to be called when a hedge is about to be attempted. OnHedge(listener func(failsafe.ExecutionEvent[R])) HedgePolicyBuilder[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) HedgePolicyBuilder[R] // Build returns a new HedgePolicy using the builder's configuration. Build() HedgePolicy[R] }
HedgePolicyBuilder builds HedgePolicy instances.
This type is not concurrency safe.
func BuilderWithDelay ¶
func BuilderWithDelay[R any](delay time.Duration) HedgePolicyBuilder[R]
BuilderWithDelay returns a new HedgePolicyBuilder 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 BuilderWithDelayFunc ¶
func BuilderWithDelayFunc[R any](delayFunc failsafe.DelayFunc[R]) HedgePolicyBuilder[R]
BuilderWithDelayFunc returns a new HedgePolicyBuilder 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.