Giter VIP home page Giter VIP logo

retry's Introduction

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))
}

retry's People

Contributors

sdorgancs avatar sebastiendorgan avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.