Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cenkalti/backoff
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.2.2
Choose a base ref
...
head repository: cenkalti/backoff
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.0.0
Choose a head ref
  • 6 commits
  • 9 files changed
  • 2 contributors

Commits on Dec 12, 2019

  1. Copy the full SHA
    671f609 View commit details

Commits on Dec 26, 2019

  1. Copy the full SHA
    3832ace View commit details
  2. Merge branch 'venkat_custom_stop' of git://github.com/venkatraju/back…

    …off into venkatraju-venkat_custom_stop
    
    * 'venkat_custom_stop' of git://github.com/venkatraju/backoff:
      add support for custom Stop duration in ExponentialBackOff
    cenkalti committed Dec 26, 2019
    Copy the full SHA
    c11c1b5 View commit details
  3. Merge branch 'venkatraju-venkat_custom_stop' into v4

    * venkatraju-venkat_custom_stop:
      add support for custom Stop duration in ExponentialBackOff
    cenkalti committed Dec 26, 2019
    Copy the full SHA
    e6c7b7c View commit details
  4. v4

    cenkalti committed Dec 26, 2019
    Copy the full SHA
    6e2a5d4 View commit details
  5. require minimum go 1.12

    cenkalti committed Dec 26, 2019
    Copy the full SHA
    a9a4cec View commit details
Showing with 44 additions and 15 deletions.
  1. +1 −1 .travis.yml
  2. +2 −2 README.md
  3. +4 −2 exponential.go
  4. +11 −0 exponential_test.go
  5. +1 −1 go.mod
  6. +3 −1 retry_test.go
  7. +3 −1 timer.go
  8. +3 −0 tries.go
  9. +16 −7 tries_test.go
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.7
- 1.12
- 1.x
- tip
before_install:
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -9,10 +9,10 @@ The retries exponentially increase and stop increasing when a certain threshold

## Usage

Import path is `github.com/cenkalti/backoff/v3`. Please note the version part at the end.
Import path is `github.com/cenkalti/backoff/v4`. Please note the version part at the end.

godoc.org does not support modules yet,
so you can use https://godoc.org/gopkg.in/cenkalti/backoff.v3 to view the documentation.
so you can use https://godoc.org/gopkg.in/cenkalti/backoff.v4 to view the documentation.

## Contributing

6 changes: 4 additions & 2 deletions exponential.go
Original file line number Diff line number Diff line change
@@ -56,9 +56,10 @@ type ExponentialBackOff struct {
RandomizationFactor float64
Multiplier float64
MaxInterval time.Duration
// After MaxElapsedTime the ExponentialBackOff stops.
// After MaxElapsedTime the ExponentialBackOff returns Stop.
// It never stops if MaxElapsedTime == 0.
MaxElapsedTime time.Duration
Stop time.Duration
Clock Clock

currentInterval time.Duration
@@ -87,6 +88,7 @@ func NewExponentialBackOff() *ExponentialBackOff {
Multiplier: DefaultMultiplier,
MaxInterval: DefaultMaxInterval,
MaxElapsedTime: DefaultMaxElapsedTime,
Stop: Stop,
Clock: SystemClock,
}
b.Reset()
@@ -114,7 +116,7 @@ func (b *ExponentialBackOff) Reset() {
func (b *ExponentialBackOff) NextBackOff() time.Duration {
// Make sure we have not gone over the maximum elapsed time.
if b.MaxElapsedTime != 0 && b.GetElapsedTime() > b.MaxElapsedTime {
return Stop
return b.Stop
}
defer b.incrementCurrentInterval()
return getRandomValueFromInterval(b.RandomizationFactor, rand.Float64(), b.currentInterval)
11 changes: 11 additions & 0 deletions exponential_test.go
Original file line number Diff line number Diff line change
@@ -83,6 +83,17 @@ func TestMaxElapsedTime(t *testing.T) {
assertEquals(t, Stop, exp.NextBackOff())
}

func TestCustomStop(t *testing.T) {
var exp = NewExponentialBackOff()
customStop := time.Minute
exp.Stop = customStop
exp.Clock = &TestClock{start: time.Time{}.Add(10000 * time.Second)}
// Change the currentElapsedTime to be 0 ensuring that the elapsed time will be greater
// than the max elapsed time.
exp.startTime = time.Time{}
assertEquals(t, customStop, exp.NextBackOff())
}

func TestBackOffOverflow(t *testing.T) {
var (
testInitialInterval time.Duration = math.MaxInt64 / 2
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/cenkalti/backoff/v3
module github.com/cenkalti/backoff/v4

go 1.12
4 changes: 3 additions & 1 deletion retry_test.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,9 @@ func (t *testTimer) Start(duration time.Duration) {
}

func (t *testTimer) Stop() {
t.timer.Stop()
if t.timer != nil {
t.timer.Stop()
}
}

func (t *testTimer) C() <-chan time.Time {
4 changes: 3 additions & 1 deletion timer.go
Original file line number Diff line number Diff line change
@@ -29,5 +29,7 @@ func (t *defaultTimer) Start(duration time.Duration) {

// Stop is called when the timer is not used anymore and resources may be freed.
func (t *defaultTimer) Stop() {
t.timer.Stop()
if t.timer != nil {
t.timer.Stop()
}
}
3 changes: 3 additions & 0 deletions tries.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,9 @@ type backOffTries struct {
}

func (b *backOffTries) NextBackOff() time.Duration {
if b.maxTries == 0 {
return Stop
}
if b.maxTries > 0 {
if b.maxTries <= b.numTries {
return Stop
23 changes: 16 additions & 7 deletions tries_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backoff

import (
"errors"
"math/rand"
"testing"
"time"
@@ -42,13 +43,21 @@ func TestMaxTriesHappy(t *testing.T) {
}
}

// https://github.com/cenkalti/backoff/issues/80
func TestMaxTriesZero(t *testing.T) {
// It might not make sense, but its okay to send a zero
bo := WithMaxRetries(&ZeroBackOff{}, uint64(0))
for ix := 0; ix < 11; ix++ {
d := bo.NextBackOff()
if d == Stop {
t.Errorf("returned Stop on try %d", ix)
}
var called int

b := WithMaxRetries(&ZeroBackOff{}, 0)

err := Retry(func() error {
called++
return errors.New("err")
}, b)

if err == nil {
t.Errorf("error expected, nil founc")
}
if called != 1 {
t.Errorf("operation is called %d times", called)
}
}