Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 Option
deprecated
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 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
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
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.