exec

package
v0.54.3 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

View Source
const RequestCanceledError = requestCanceledError("RequestCanceled")

Variables

This section is empty.

Functions

func AddRequestCancelCheck

func AddRequestCancelCheck(check RequestCanceledCheck)

func IsClientAwaitHeadersTimeoutError

func IsClientAwaitHeadersTimeoutError(err error) bool

func IsConnectionError

func IsConnectionError(err error) bool

func IsContextDone added in v0.48.0

func IsContextDone(ctx context.Context) (bool, error)

func IsErrMaxAttemptsExceeded

func IsErrMaxAttemptsExceeded(err error) bool

func IsErrMaxElapsedTimeExceeded

func IsErrMaxElapsedTimeExceeded(err error) bool

func IsHttp2ClientConnectionForceClosedError added in v0.41.3

func IsHttp2ClientConnectionForceClosedError(err error) bool

func IsIoTimeoutError added in v0.41.3

func IsIoTimeoutError(err error) bool

func IsOperationWasCanceledError added in v0.41.3

func IsOperationWasCanceledError(err error) bool

func IsRequestCanceled

func IsRequestCanceled(err error) bool

IsRequestCanceled checks if the given error was (only) caused by a canceled context - if there is any other error contained in it, we return false. Thus, if IsRequestCanceled returns true, you can (and should) ignore the error and stop processing instead.

func IsTimeoutError

func IsTimeoutError(err error) bool

func IsTlsHandshakeTimeoutError

func IsTlsHandshakeTimeoutError(err error) bool

func IsUsedClosedConnectionError

func IsUsedClosedConnectionError(err error) bool

func NewExponentialBackOff

func NewExponentialBackOff(settings *BackoffSettings) *backoff.ExponentialBackOff

func NewTestHttpClient

func NewTestHttpClient(timeout time.Duration, trips Trips) http.Client

func WithManualCancelContext

func WithManualCancelContext(parentCtx context.Context) (context.Context, context.CancelFunc)

WithManualCancelContext is similar to context.WithCancel, but it only cancels the returned context once the cancel function has been called. Cancellation of the parent context is not automatically propagated to the child context.

Types

type BackoffExecutor

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

func NewBackoffExecutor

func NewBackoffExecutor(logger log.Logger, res *ExecutableResource, settings *BackoffSettings, checks []ErrorChecker, notifier ...Notify) *BackoffExecutor

func NewBackoffExecutorWithOptions added in v0.54.3

func NewBackoffExecutorWithOptions(
	logger log.Logger,
	res *ExecutableResource,
	settings *BackoffSettings,
	checks []ErrorChecker,
	notifier []Notify,
	opts ...BackoffExecutorOption,
) *BackoffExecutor

NewBackoffExecutorWithOptions creates a BackoffExecutor with functional options. Use this when you need to customize behavior like elapsed time tracking.

func (*BackoffExecutor) Execute

func (e *BackoffExecutor) Execute(ctx context.Context, f Executable, notifier ...Notify) (any, error)

type BackoffExecutorOption added in v0.54.3

type BackoffExecutorOption func(*BackoffExecutor)

BackoffExecutorOption is a functional option for configuring a BackoffExecutor.

func WithElapsedTimeTrackerFactory added in v0.54.3

func WithElapsedTimeTrackerFactory(factory func() ElapsedTimeTracker) BackoffExecutorOption

WithElapsedTimeTrackerFactory sets a factory function for creating elapsed time trackers. Each Execute call will use a new tracker instance, making it safe for concurrent use. Use this to change how the MaxElapsedTime budget is measured.

type BackoffSettings

type BackoffSettings struct {
	CancelDelay     time.Duration `cfg:"cancel_delay"`
	InitialInterval time.Duration `cfg:"initial_interval" default:"50ms"`
	MaxAttempts     int           `cfg:"max_attempts" default:"10"`
	MaxElapsedTime  time.Duration `cfg:"max_elapsed_time" default:"10m"`
	MaxInterval     time.Duration `cfg:"max_interval" default:"10s"`
}

func ReadBackoffSettings

func ReadBackoffSettings(config cfg.Config, paths ...string) (BackoffSettings, error)

type DefaultElapsedTimeTracker added in v0.54.3

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

DefaultElapsedTimeTracker measures time from when execution starts. This is the default behavior that preserves backward compatibility.

func NewDefaultElapsedTimeTracker added in v0.54.3

func NewDefaultElapsedTimeTracker() *DefaultElapsedTimeTracker

NewDefaultElapsedTimeTracker creates a tracker that measures elapsed time from the start of execution.

func NewDefaultElapsedTimeTrackerWithInterfaces added in v0.54.3

func NewDefaultElapsedTimeTrackerWithInterfaces(clock clock.Clock) *DefaultElapsedTimeTracker

NewDefaultElapsedTimeTrackerWithInterfaces creates a DefaultElapsedTimeTracker with injected dependencies for testing.

func (*DefaultElapsedTimeTracker) Elapsed added in v0.54.3

func (*DefaultElapsedTimeTracker) OnError added in v0.54.3

func (t *DefaultElapsedTimeTracker) OnError(_ error)

func (*DefaultElapsedTimeTracker) OnSuccess added in v0.54.3

func (t *DefaultElapsedTimeTracker) OnSuccess()

func (*DefaultElapsedTimeTracker) Start added in v0.54.3

func (t *DefaultElapsedTimeTracker) Start()

type DefaultExecutor

type DefaultExecutor struct{}

func (DefaultExecutor) Execute

func (e DefaultExecutor) Execute(ctx context.Context, f Executable, notifier ...Notify) (any, error)

type ElapsedTimeTracker added in v0.54.3

type ElapsedTimeTracker interface {
	// Start is called at the beginning of execution.
	Start()
	// OnError is called when a retryable error occurs.
	OnError(err error)
	// OnSuccess is called when execution succeeds.
	OnSuccess()
	// Elapsed returns the current elapsed duration based on the tracking strategy.
	Elapsed() time.Duration
}

ElapsedTimeTracker defines the strategy for tracking elapsed time during backoff execution. Different implementations can measure time from different starting points.

type ErrAttemptsExceeded

type ErrAttemptsExceeded struct {
	Resource     *ExecutableResource
	Attempts     int
	DurationTook time.Duration
	Err          error
}

func NewErrAttemptsExceeded

func NewErrAttemptsExceeded(resource *ExecutableResource, attempts int, durationTook time.Duration, err error) *ErrAttemptsExceeded

func (*ErrAttemptsExceeded) Error

func (e *ErrAttemptsExceeded) Error() string

func (*ErrAttemptsExceeded) Unwrap

func (e *ErrAttemptsExceeded) Unwrap() error

type ErrMaxElapsedTimeExceeded

type ErrMaxElapsedTimeExceeded struct {
	Resource     *ExecutableResource
	Attempts     int
	DurationTook time.Duration
	DurationMax  time.Duration
	Err          error
}

func NewErrMaxElapsedTimeExceeded

func NewErrMaxElapsedTimeExceeded(resource *ExecutableResource, attempts int, durationTook time.Duration, durationMax time.Duration, err error) *ErrMaxElapsedTimeExceeded

func (ErrMaxElapsedTimeExceeded) Error

func (ErrMaxElapsedTimeExceeded) Unwrap

func (e ErrMaxElapsedTimeExceeded) Unwrap() error

type ErrorChecker

type ErrorChecker func(result any, err error) ErrorType

type ErrorTriggeredElapsedTimeTracker added in v0.54.3

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

ErrorTriggeredElapsedTimeTracker measures time from when the first error occurs. This is useful for long-blocking operations (like Kafka poll) where you want the MaxElapsedTime budget to only start being consumed when actual errors occur, not during normal blocking waits for data.

On success, the error timer is reset so subsequent errors get a fresh budget.

func NewErrorTriggeredElapsedTimeTracker added in v0.54.3

func NewErrorTriggeredElapsedTimeTracker() *ErrorTriggeredElapsedTimeTracker

NewErrorTriggeredElapsedTimeTracker creates a tracker that measures elapsed time from the first error.

func NewErrorTriggeredElapsedTimeTrackerWithInterfaces added in v0.54.3

func NewErrorTriggeredElapsedTimeTrackerWithInterfaces(clock clock.Clock) *ErrorTriggeredElapsedTimeTracker

NewErrorTriggeredElapsedTimeTrackerWithInterfaces creates an ErrorTriggeredElapsedTimeTracker with injected dependencies for testing.

func (*ErrorTriggeredElapsedTimeTracker) Elapsed added in v0.54.3

func (*ErrorTriggeredElapsedTimeTracker) OnError added in v0.54.3

func (*ErrorTriggeredElapsedTimeTracker) OnSuccess added in v0.54.3

func (t *ErrorTriggeredElapsedTimeTracker) OnSuccess()

func (*ErrorTriggeredElapsedTimeTracker) Start added in v0.54.3

type ErrorType

type ErrorType int
const (
	// We don't know yet, let the other error checkers decide about this error. If the error is
	// not marked retryable by another checker, we will not retry it.
	ErrorTypeUnknown ErrorType = iota
	// Stop retrying, the error was actually a "success" and needs to be propagated to the caller
	// ("success" meaning something e.g. was not found, but will not magically appear just because
	// we retry a few more times)
	ErrorTypeOk
	// Immediately stop retrying and return this error to the caller
	ErrorTypePermanent
	// Retry the execution of the action
	ErrorTypeRetryable
)

func CheckClientAwaitHeaderTimeoutError

func CheckClientAwaitHeaderTimeoutError(_ any, err error) ErrorType

func CheckConnectionError

func CheckConnectionError(_ any, err error) ErrorType

func CheckHttp2ClientConnectionForceClosedError added in v0.41.3

func CheckHttp2ClientConnectionForceClosedError(_ any, err error) ErrorType

func CheckOperationWasCanceledError added in v0.41.3

func CheckOperationWasCanceledError(_ any, err error) ErrorType

func CheckRequestCanceled

func CheckRequestCanceled(_ any, err error) ErrorType

func CheckTimeoutError

func CheckTimeoutError(_ any, err error) ErrorType

func CheckTlsHandshakeTimeoutError

func CheckTlsHandshakeTimeoutError(_ any, err error) ErrorType

func CheckUsedClosedConnectionError

func CheckUsedClosedConnectionError(_ any, err error) ErrorType

type Executable

type Executable func(ctx context.Context) (any, error)

type ExecutableResource

type ExecutableResource struct {
	Type string
	Name string
}

func (ExecutableResource) String

func (r ExecutableResource) String() string

type Executor

type Executor interface {
	Execute(ctx context.Context, f Executable, notifier ...Notify) (any, error)
}

func NewDefaultExecutor

func NewDefaultExecutor() Executor

func NewExecutor

func NewExecutor(logger log.Logger, res *ExecutableResource, settings *BackoffSettings, checks []ErrorChecker, notifier ...Notify) Executor

type Notify added in v0.20.0

type Notify func(error, time.Duration)

type RequestCanceledCheck

type RequestCanceledCheck func(err error) bool

type StopFunc

type StopFunc func()

func WithDelayedCancelContext

func WithDelayedCancelContext(parentCtx context.Context, delay time.Duration) (context.Context, StopFunc)

WithDelayedCancelContext creates a context which propagates the cancellation of the parent context after a fixed delay to the returned context. Call the returned StopFunc function to release resources associated with the returned context once you no longer need it. Calling stop never returns before all resources have been released, so after Stop returns, the context will not experience a delayed cancel anymore (however, if the parent context was already canceled the moment you called stop, the child context will immediately get canceled).

func WithStoppableDeadlineContext

func WithStoppableDeadlineContext(parentCtx context.Context, deadline time.Time) (context.Context, StopFunc)

WithStoppableDeadlineContext is similar to context.WithDeadline. However, while context.WithDeadline cancels the context when you call the returned context.CancelFunc, WithStoppableDeadlineContext does not cancel the context if it is not yet canceled once you stop it.

type TestRoundTripper

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

func NewTestRoundTripper

func NewTestRoundTripper(trips ...Trip) *TestRoundTripper

func (*TestRoundTripper) RoundTrip

func (t *TestRoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

type TrackedBackOff added in v0.54.3

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

TrackedBackOff wraps an ExponentialBackOff and uses an ElapsedTimeTracker to determine when MaxElapsedTime has been exceeded instead of the internal clock. This allows the elapsed time measurement strategy to be customized (e.g., measure from when the first error occurs rather than from when the backoff was created).

func NewTrackedBackOff added in v0.54.3

func NewTrackedBackOff(settings *BackoffSettings, tracker ElapsedTimeTracker) *TrackedBackOff

NewTrackedBackOff creates a backoff that delegates elapsed time checking to the provided tracker. The underlying ExponentialBackOff is configured with MaxElapsedTime=0 to disable its internal elapsed time check, allowing the TrackedBackOff to handle it via the tracker.

func (*TrackedBackOff) NextBackOff added in v0.54.3

func (b *TrackedBackOff) NextBackOff() time.Duration

NextBackOff returns the next backoff interval, or backoff.Stop if MaxElapsedTime (as measured by the tracker) has been exceeded.

func (*TrackedBackOff) Reset added in v0.54.3

func (b *TrackedBackOff) Reset()

Reset resets the backoff interval back to the initial value.

type Trip

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

func DoTrip

func DoTrip(duration time.Duration, err error) Trip

type Trips

type Trips []Trip

Jump to

Keyboard shortcuts

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