retries

package
v0.54.0 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 7 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Poll

func Poll[T any](ctx context.Context, timeout time.Duration, fn func() (*T, *Err)) (*T, error)

func Wait

func Wait(ctx context.Context, timeout time.Duration, fn func() *Err) error

Types

type Err

type Err struct {
	Err  error
	Halt bool
}

func Continue

func Continue(err error) *Err

func Continuef

func Continuef(format string, err error, args ...interface{}) *Err

func Continues

func Continues(msg string) *Err

func Halt

func Halt(err error) *Err

func (*Err) Error added in v0.29.0

func (e *Err) Error() string

func (*Err) Unwrap added in v0.29.0

func (e *Err) Unwrap() error

type ErrTimedOut added in v0.26.0

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

func (*ErrTimedOut) Error added in v0.26.0

func (et *ErrTimedOut) Error() string

func (*ErrTimedOut) Unwrap added in v0.26.0

func (et *ErrTimedOut) Unwrap() error

type Info deprecated

type Info[T any] struct {
	Info    *T
	Timeout time.Duration
}

Deprecated: use return types from non-*AndWait methods

type Option deprecated

type Option[T any] func(*Info[T])

Deprecated: use return types from non-*AndWait methods

func OnPoll deprecated added in v0.2.0

func OnPoll[T any](callback func(*T)) Option[T]

Deprecated: use return types from non-*AndWait methods

func Timeout deprecated

func Timeout[T any](dur time.Duration) Option[T]

Deprecated: use return types from non-*AndWait methods

type Retrier added in v0.29.0

type Retrier[T any] struct {
	// contains filtered or unexported fields
}

Retrier is a struct that can retry an operation until it succeeds or the timeout is reached. The empty struct indicates that the retrier should run for 20 minutes and retry on any non-nil error. The type parameter is the return type of the Run() method. When using the Wait() method, this can be struct{}.

Example:

r := retries.New[struct{}](retries.WithTimeout(5 * time.Minute), retries.OnErrors(apierr.ErrResourceConflict))
err := r.Wait(ctx, func(ctx context.Context) error {
	return a.Workspaces.Delete(ctx, provisioning.DeleteWorkspaceRequest{
		WorkspaceId: workspace.WorkspaceId,
	})
})

func New added in v0.29.0

func New[T any](configOpts ...RetryOption) Retrier[T]

New creates a new retrier with the given configuration. If no timeout is specified, the default is 20 minutes. If the timeout is negative, the retrier will run indefinitely. If no retry function is specified, the default is to retry on all errors.

Example
// Default retries for 20 minutes on any error
New[int]()

// Override the timeout with NewTimeout
New[int](WithTimeout(5 * time.Minute))

// Retry on specific errors only with OnErrors
New[int](OnErrors(apierr.ErrResourceConflict))

// Customize retry logic with WithRetryFunc
retryCount := 3
New[int](WithRetryFunc(func(err error) bool {
	if retryCount > 0 {
		retryCount--
		return true
	}
	return false
}))
Output:

func (Retrier[T]) Run added in v0.29.0

func (r Retrier[T]) Run(ctx context.Context, fn func(context.Context) (*T, error)) (*T, error)

Run runs the given function until it succeeds or the timeout is reached, returning the result. On timeout, it returns an error wrapping the last error returned by the function.

Example
hasRun := false
e := errors.New("an error")
res, _ := New[string](WithTimeout(5*time.Minute), OnErrors(e)).Run(
	context.Background(),
	func(ctx context.Context) (*string, error) {
		if !hasRun {
			hasRun = true
			fmt.Println("failed, retrying")
			return nil, e
		}
		fmt.Println("succeeded")
		res := "hello!"
		return &res, nil
	},
)
fmt.Println(*res)
Output:

failed, retrying
succeeded
hello!

func (Retrier[T]) Wait added in v0.29.0

func (r Retrier[T]) Wait(ctx context.Context, fn func(ctx context.Context) error) error

Wait runs the given function until it succeeds or the timeout is reached. On success, it returns nil. On timeout, it returns an error wrapping the last error returned by the function.

Example
hasRun := false
e := errors.New("an error")
err := New[string](WithTimeout(5*time.Minute), OnErrors(e)).Wait(
	context.Background(),
	func(ctx context.Context) error {
		if !hasRun {
			hasRun = true
			fmt.Println("failed, retrying")
			return e
		}
		fmt.Println("succeeded")
		return nil
	},
)
fmt.Println(err)
Output:

failed, retrying
succeeded
<nil>

type RetryConfig added in v0.29.0

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

RetryConfig is the configuration for a retrier.

func (RetryConfig) Backoff added in v0.29.0

func (r RetryConfig) Backoff(attempt int) time.Duration

func (RetryConfig) ShouldRetry added in v0.29.0

func (r RetryConfig) ShouldRetry(err error) bool

func (RetryConfig) Timeout added in v0.29.0

func (r RetryConfig) Timeout() time.Duration

type RetryOption added in v0.29.0

type RetryOption func(*RetryConfig)

RetryOption is a function that sets part of the retry configuration for a retrier.

func OnErrors added in v0.29.0

func OnErrors(on ...error) RetryOption

OnErrors sets the errors that should be retried.

func WithRetryFunc added in v0.29.0

func WithRetryFunc(halt func(error) bool) RetryOption

WithRetryFunc sets the function that determines whether an error should halt the retrier. If the function returns true, the retrier will continue. If it returns false, the retrier will halt.

func WithTimeout added in v0.29.0

func WithTimeout(timeout time.Duration) RetryOption

WithTimeout sets the timeout for the retrier.

Jump to

Keyboard shortcuts

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