retry

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: MIT Imports: 3 Imported by: 0

README

retry

Retries using the exponential backoff algorithm can be easily realized.

%%{init:{'theme':'neutral'}}%%
sequenceDiagram
    participant User
    participant Retry Function
    participant Operation

    User->>Retry Function: Retry(op, opts)
    loop Until success or max retries
        Retry Function->>Operation: Execute operation (op)
        Operation-->>Retry Function: If error
        Retry Function->>Retry Function: Sleep for delay
    end
    Retry Function->>User: Return result or error

Usage

No return value

op := func() error {
    return errors.New("Error!!")
}

err := retry.Retry(op, retry.WithInitialDelay(2*time.Second), retry.WithMaxRetries(3))

One return value

op := func() (string, error) {
    return "", errors.New("Error!!")
}

result, err := retry.RetryOneResult(op, retry.WithInitialDelay(2*time.Second), retry.WithMaxRetries(3))

Two return values

	op := func() (string, bool, error) {
		return "", false, errors.New("Error!!")
	}

	result1, result2, err := retry.RetryTwoResult(op, retry.WithInitialDelay(2*time.Second), retry.WithMaxRetries(3))

License

MIT

Author

hoshiimo

Documentation

Overview

The package "retry" allows exponential backoff retries. The default behavior is as follows.

  • Jitter : ON
  • First retry delay : 100ms
  • Maximum delay : 10s
  • Maximum retry count : 5

※These can be changed with arguments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry

func Retry(op Operation, opts ...RetryOption) error

func RetryOneResult added in v0.2.0

func RetryOneResult[T any](op OperationOneResult[T], opts ...RetryOption) (*T, error)

func RetryTwoResults added in v0.2.1

func RetryTwoResults[T1 any, T2 any](op OperationTwoResult[T1, T2], opts ...RetryOption) (*T1, *T2, error)

Types

type Operation

type Operation func() error

type OperationOneResult added in v0.2.0

type OperationOneResult[T any] func() (T, error)

type OperationTwoResult added in v0.2.0

type OperationTwoResult[T1 any, T2 any] func() (T1, T2, error)

type RetryOption

type RetryOption func(*RetryOptions)

func WithInitialDelay

func WithInitialDelay(delay time.Duration) RetryOption

First retry delay

func WithMaxDelay

func WithMaxDelay(delay time.Duration) RetryOption

Maximum delay

func WithMaxRetries

func WithMaxRetries(maxRetries int) RetryOption

Maximum retry count

func WithoutJitter

func WithoutJitter() RetryOption

type RetryOptions

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

Jump to

Keyboard shortcuts

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