retry

package module
v0.0.0-...-59863ce Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2019 License: Apache-2.0 Imports: 3 Imported by: 1 module, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

retry

retry is a simple golang retrying module aiming at simplifying the creation of generic retry behavior using a fluent interface

Features

  • Fluent interface
  • Specify stop condition
  • Specify timeout duration
  • Specify retry interval duration
  • Specify backoff strategy

Dependencies

retry uses Testify https://github.com/stretchr/testify

Examples

package retry_test

import (
	"testing"
	"time"

	"github.com/SebastienDorgan/retry"
	"github.com/stretchr/testify/assert"
)

var countHello = 0

func hello() interface{} {
	println("hello")
	time.Sleep(500 * time.Millisecond)
	return nil
}

func Counter(start, step int) retry.Action {
	value := start
	return func() interface{} {
		value = value + step
		return value
	}
}

func GreaterThan(v int) retry.Condition {
	return func(vi interface{}) bool {
		return vi.(int) >= v
	}
}

func Test(t *testing.T) {
	//run with go test -v -timeout 30s github.com/SebastienDorgan/retry -run ^Test$

	//Retry hello function every seconds for 10 seconds
	start := time.Now()

	res := retry.With(hello).Every(1 * time.Second).For(10 * time.Second).Go()

	elapse := time.Now().Sub(start)

	//The retry mechanism is under millis precise
	assert.Equal(t, 10*time.Second, elapse.Truncate(time.Millisecond))
	assert.True(t, res.Timeout)
	assert.Equal(t, uint64(10), res.Attempts)

	//If MaxAttempts is used the retry loop stops before the timeout
	res = retry.With(hello).Every(1 * time.Second).For(10 * time.Second).MaxAttempts(5).Go()
	assert.False(t, res.Timeout)
	assert.Equal(t, uint64(5), res.Attempts)

	//Retry the counter function every 10 seconds for 10 seconds or until condition GreaterThen(10) is satisfied
	res = retry.With(Counter(0, 2)).For(10 * time.Second).Every(1 * time.Second).Until(GreaterThan(10)).Go()
	assert.Equal(t, 10, res.LastReturnedValue.(int))
	assert.False(t, res.Timeout)

	//Retry hello function every seconds for 10 seconds
	start = time.Now()

	res = retry.With(Counter(0, 1)).Every(1 * time.Second).WithBackoff(retry.ExponentialStrategy(2.)).MaxAttempts(5).Go()

	elapse = time.Now().Sub(start)
	assert.Equal(t, 5, res.LastReturnedValue.(int))
	//31 = 1*2^0 + 1*2^1 + 1*2^2 + 1*2^3 + 1*2^4
	assert.Equal(t, 31*time.Second, elapse.Truncate(time.Second))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FalseCondition

func FalseCondition(v interface{}, e error) bool

FalseCondition utility function that always return false

func UniformStrategy

func UniformStrategy(attempt uint64, interval time.Duration) time.Duration

UniformStrategy keep same delay between attempts

Types

type Action

type Action func() (interface{}, error)

Action defines an action to retry against

func NoError

func NoError(action func() interface{}) Action

NoError NoError wraps a function that do not return error

type BackoffStrategy

type BackoffStrategy func(attempt uint64, interval time.Duration) time.Duration

BackoffStrategy returns the delay to apply between attempt and attempt + 1

func ExponentialStrategy

func ExponentialStrategy(f float64) BackoffStrategy

ExponentialStrategy return an exponential backoff strategy with a growing factor f

type Condition

type Condition func(interface{}, error) bool

Condition defines the retry loop stopping condition

type Result

type Result struct {
	Timeout   bool
	LastValue interface{}
	LastError error
	Attempts  uint64
}

Result stores the result of the retry loop

type Retry

type Retry struct {
	Action          Action
	Condition       Condition
	Duration        time.Duration
	Interval        time.Duration
	MaxAtt          uint64
	BackoffStrategy BackoffStrategy
	// contains filtered or unexported fields
}

Retry defines necessary attributes to retry an action

func With

func With(action Action) *Retry

With set the action to retry

func (*Retry) Every

func (r *Retry) Every(duration time.Duration) *Retry

Every set the retry interval

func (*Retry) For

func (r *Retry) For(duration time.Duration) *Retry

For set the maximum duration of the retry loop

func (*Retry) Go

func (r *Retry) Go() *Result

Go starts the retry loop

func (*Retry) MaxAttempts

func (r *Retry) MaxAttempts(max uint64) *Retry

MaxAttempts set the maximum attempts of the retry loop

func (*Retry) Until

func (r *Retry) Until(condition Condition) *Retry

Until set the stopping condition

func (*Retry) WithBackoff

func (r *Retry) WithBackoff(strategy BackoffStrategy) *Retry

WithBackoff set the backoff strategy to implement

Jump to

Keyboard shortcuts

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