Skip to content

Latest commit

 

History

History

docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

retrygo

import "github.com/ic-it/retrygo"

Index

type ErrRecovered

ErrRecovered is returned when a panic is recovered.

type ErrRecovered struct{ V any }

func (ErrRecovered) Error

func (e ErrRecovered) Error() string

type Retry

Retry is the main type of this package.

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

func New

func New[T any](policy RetryPolicy, options ...RetryOption[T]) (Retry[T], error)

New creates a new Retry instance with the given RetryPolicy and RetryOptions.

func NewZero

func NewZero(policy RetryPolicy, options ...RetryOption[zero]) (Retry[zero], error)

NewZero creates a new Retry instance with no return value.

func (Retry[T]) Do

func (r Retry[T]) Do(ctx context.Context, f func(context.Context) (T, error)) (T, error)

Do calls the given function f until it returns nil error or the context is done.

func (Retry[T]) DoZero

func (r Retry[T]) DoZero(ctx context.Context, f func(context.Context) error) error

DoZero calls the given function f until it returns nil error or the context is done.

type RetryInfo

RetryInfo contains information about the retry

type RetryInfo struct {
    Fails int       // Fails is the number of retries
    Err   error     // Err is the error returned by the function
    Since time.Time // Since is the time when the retry started
}

type RetryOption

type RetryOption[T any] func(*Retry[T])

type RetryOption[T any] interface {
    // contains filtered or unexported methods
}

func WithRecovery

func WithRecovery[T any]() RetryOption[T]

WithRecovery enables the recovery mode.

type RetryPolicy

RetryPolicy is a function that returns a retry strategy based on the RetryInfo

type RetryPolicy func(RetryInfo) (continueRetry bool, sleep time.Duration)

func Combine

func Combine(policies ...RetryPolicy) RetryPolicy

Combine returns a RetryPolicy that combines multiple RetryPolicies. The function will return true if all the policies return true. (logical AND) If all the policies return true, the function will return the sum of the sleep durations.

Sleep formula: sleep1 + sleep2 + ...

func Constant

func Constant(interval time.Duration) RetryPolicy

Constant returns a RetryPolicy that always returns the same interval between retries.

Sleep formula: interval

func Exponential

func Exponential(interval time.Duration) RetryPolicy

Exponential returns a RetryPolicy that increases the interval between retries exponentially.

Sleep formula: interval * 2^fails

func Jitter

func Jitter(interval time.Duration) RetryPolicy

Jitter returns a RetryPolicy that adds a random non-negative jitter to the interval between retries.

Sleep formula: interval + rand.Int63n(interval)

func LimitCount

func LimitCount(count int) RetryPolicy

LimitCount returns a RetryPolicy that limits the number of retries.

Sleep formula: 0

func LimitTime

func LimitTime(limit time.Duration) RetryPolicy

LimitTime returns a RetryPolicy that limits the total time spent on retries.

Sleep formula: 0

WARNING: Use context.WithTimeout instead of this function if you can!

func Linear

func Linear(interval time.Duration) RetryPolicy

Linear returns a RetryPolicy that increases the interval between retries linearly.

Sleep formula: interval * fails

Generated by gomarkdoc