retrygo

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2024 License: MIT Imports: 3 Imported by: 1

README

RetryGO

Retrygo


codecov Go Reference

RetryGO is a simple and flexible library for retrying functions in Go.

Purpose

The purpose of this library is to provide a simple but flexible way to retry functions in Go.

Unlike other libraries, RetryGO does not provide a way to specify the number of retry attempts. Instead, it allows the user to specify a function that will be called after each retry attempt. This function will be responsible for deciding whether to continue retrying or not.

Main ideas:

  • Retry Policy: The user can specify a function that will be called after each retry attempt. This function will be responsible for deciding whether to continue retrying or not.
  • Backoff: The user can create its own retry strategy or use one of the predefined backoff strategies.

Features

  • Predefined Backoff Strategies: The library provides some predefined backoff strategies, such as constant, exponential, and linear backoff.
  • Combine Backoff Strategies: The user can combine multiple backoff strategies to create a custom backoff strategy.
  • Recover: The user can enable the recover feature, which will recover panics and behave as if the function returned an error.
  • Context: The user can use the context to cancel the retry process.

Installation

go get github.com/ic-it/retrygo

Usage

Examples

Simple Example With Predefined Backoff
package main

import (
	"context"
	"time"

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

func main() {
	type ReturnType struct{}
	retry, err := retrygo.New[ReturnType](
		retrygo.Combine(
			retrygo.Constant(1*time.Second),
			retrygo.LimitCount(5),
		),
	)

	if err != nil {
		// Handle error
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	val, err := retry.Do(ctx, func(context.Context) (ReturnType, error) {
		// Do something
		return ReturnType{}, nil
	})

    if err != nil {
        // Handle error
    }

    // Continue with val
}
Example with Custom Backoff
package main

import (
	"context"
	"time"

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

func main() {
	type ReturnType struct{}
	retry, err := retrygo.New[ReturnType](
		func(ri retrygo.RetryInfo) (continueRetry bool, sleep time.Duration) {
			// Custom logic
			return false, 0
		},
	)

	if err != nil {
		// Handle error
	}

	val, err := retry.Do(context.TODO(), func(context.Context) (ReturnType, error) {
		// Do something
		return ReturnType{}, nil
	})

	if err != nil {
		// Handle error
	}

	// Continue with val
}

Documentation

Doumentation is available in the docs folder. This documentation was generated using gomarkdoc.

Benchmarks

See benchmarks here.

Results gist.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrRecovered added in v0.2.0

type ErrRecovered struct{ V any }

ErrRecovered is returned when a panic is recovered.

func (ErrRecovered) Error added in v0.2.0

func (e ErrRecovered) Error() string

type Retry

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

Retry is the main type of this package.

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 added in v0.3.2

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 added in v0.3.2

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

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
}

RetryInfo contains information about the retry

type RetryOption added in v0.3.1

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

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

func WithRecovery added in v0.2.0

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

WithRecovery enables the recovery mode.

type RetryPolicy

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

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

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

Directories

Path Synopsis
benchmarks module

Jump to

Keyboard shortcuts

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