cenkalti/backoff

retry is endless in v4 version

HobbyBear opened this issue · 1 comments

import (
	"errors"
	"fmt"
	"testing"
	"time"

	"github.com/cenkalti/backoff/v4"
	)

func Test01(t *testing.T)  {
	retryParam := backoff.ExponentialBackOff{
		InitialInterval:     1 * time.Second, 
		RandomizationFactor: backoff.DefaultRandomizationFactor,
		Multiplier:          1.1,
		MaxInterval:         3 * time.Second,  
		MaxElapsedTime:      10 * time.Second,
		Clock:               backoff.SystemClock,
	}

	err := backoff.Retry(func() error {
		fmt.Println("start",time.Now().Format("2006-01-02 03:04:05 PM"))
		time.Sleep(5 * time.Second + 20*time.Millisecond)
		fmt.Println("end",time.Now().Format("2006-01-02 03:04:05 PM"))
		return errors.New("233")
	},&retryParam)
	if err != nil{
		t.Fatal(err)
	}
	fmt.Println("success")
}

if i write the code like this ,the code is endless. the reason is lost the stop property,i think the best way is that give the lost property the default value,avoiding i must look throungh the backoff code to understand it.

Call the constructor and override desired fields afterwards.