Update vendored library github.com/cenkalti/backoff

This commit is contained in:
Alexander Neumann 2018-03-30 11:45:13 +02:00
parent 5a77b2ab49
commit 673f0bbd6c
9 changed files with 23 additions and 13 deletions

4
Gopkg.lock generated
View File

@ -28,8 +28,8 @@
[[projects]] [[projects]]
name = "github.com/cenkalti/backoff" name = "github.com/cenkalti/backoff"
packages = ["."] packages = ["."]
revision = "61153c768f31ee5f130071d08fc82b85208528de" revision = "2ea60e5f094469f9e65adb9cd103795b73ae743e"
version = "v1.1.0" version = "v2.0.0"
[[projects]] [[projects]]
name = "github.com/cpuguy83/go-md2man" name = "github.com/cpuguy83/go-md2man"

View File

@ -34,7 +34,7 @@ func NewRetryBackend(be restic.Backend, maxTries int, report func(string, error,
func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) error { func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) error {
err := backoff.RetryNotify(f, err := backoff.RetryNotify(f,
backoff.WithContext(backoff.WithMaxTries(backoff.NewExponentialBackOff(), uint64(be.MaxTries)), ctx), backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), uint64(be.MaxTries)), ctx),
func(err error, d time.Duration) { func(err error, d time.Duration) {
if be.Report != nil { if be.Report != nil {
be.Report(msg, err, d) be.Report(msg, err, d)

View File

@ -1,6 +1,7 @@
language: go language: go
go: go:
- 1.3.3 - 1.3.3
- 1.x
- tip - tip
before_install: before_install:
- go get github.com/mattn/goveralls - go get github.com/mattn/goveralls

View File

@ -15,7 +15,7 @@ import "time"
// BackOff is a backoff policy for retrying an operation. // BackOff is a backoff policy for retrying an operation.
type BackOff interface { type BackOff interface {
// NextBackOff returns the duration to wait before retrying the operation, // NextBackOff returns the duration to wait before retrying the operation,
// or backoff.Stop to indicate that no more retries should be made. // or backoff. Stop to indicate that no more retries should be made.
// //
// Example usage: // Example usage:
// //

View File

@ -127,7 +127,9 @@ func (b *ExponentialBackOff) NextBackOff() time.Duration {
// GetElapsedTime returns the elapsed time since an ExponentialBackOff instance // GetElapsedTime returns the elapsed time since an ExponentialBackOff instance
// is created and is reset when Reset() is called. // is created and is reset when Reset() is called.
// //
// The elapsed time is computed using time.Now().UnixNano(). // The elapsed time is computed using time.Now().UnixNano(). It is
// safe to call even while the backoff policy is used by a running
// ticker.
func (b *ExponentialBackOff) GetElapsedTime() time.Duration { func (b *ExponentialBackOff) GetElapsedTime() time.Duration {
return b.Clock.Now().Sub(b.startTime) return b.Clock.Now().Sub(b.startTime)
} }

View File

@ -18,9 +18,12 @@ type Ticker struct {
stopOnce sync.Once stopOnce sync.Once
} }
// NewTicker returns a new Ticker containing a channel that will send the time at times // NewTicker returns a new Ticker containing a channel that will send
// specified by the BackOff argument. Ticker is guaranteed to tick at least once. // the time at times specified by the BackOff argument. Ticker is
// The channel is closed when Stop method is called or BackOff stops. // guaranteed to tick at least once. The channel is closed when Stop
// method is called or BackOff stops. It is not safe to manipulate the
// provided backoff policy (notably calling NextBackOff or Reset)
// while the ticker is running.
func NewTicker(b BackOff) *Ticker { func NewTicker(b BackOff) *Ticker {
c := make(chan time.Time) c := make(chan time.Time)
t := &Ticker{ t := &Ticker{
@ -29,6 +32,7 @@ func NewTicker(b BackOff) *Ticker {
b: ensureContext(b), b: ensureContext(b),
stop: make(chan struct{}), stop: make(chan struct{}),
} }
t.b.Reset()
go t.run() go t.run()
runtime.SetFinalizer(t, (*Ticker).Stop) runtime.SetFinalizer(t, (*Ticker).Stop)
return t return t
@ -42,7 +46,6 @@ func (t *Ticker) Stop() {
func (t *Ticker) run() { func (t *Ticker) run() {
c := t.c c := t.c
defer close(c) defer close(c)
t.b.Reset()
// Ticker is guaranteed to tick at least once. // Ticker is guaranteed to tick at least once.
afterC := t.send(time.Now()) afterC := t.send(time.Now())

View File

@ -30,6 +30,10 @@ func TestTicker(t *testing.T) {
b := NewExponentialBackOff() b := NewExponentialBackOff()
ticker := NewTicker(b) ticker := NewTicker(b)
elapsed := b.GetElapsedTime()
if elapsed > time.Second {
t.Errorf("elapsed time too large: %v", elapsed)
}
var err error var err error
for _ = range ticker.C { for _ = range ticker.C {

View File

@ -3,13 +3,13 @@ package backoff
import "time" import "time"
/* /*
WithMaxTries creates a wrapper around another BackOff, which will WithMaxRetries creates a wrapper around another BackOff, which will
return Stop if NextBackOff() has been called too many times since return Stop if NextBackOff() has been called too many times since
the last time Reset() was called the last time Reset() was called
Note: Implementation is not thread-safe. Note: Implementation is not thread-safe.
*/ */
func WithMaxTries(b BackOff, max uint64) BackOff { func WithMaxRetries(b BackOff, max uint64) BackOff {
return &backOffTries{delegate: b, maxTries: max} return &backOffTries{delegate: b, maxTries: max}
} }

View File

@ -9,7 +9,7 @@ import (
func TestMaxTriesHappy(t *testing.T) { func TestMaxTriesHappy(t *testing.T) {
r := rand.New(rand.NewSource(time.Now().UnixNano())) r := rand.New(rand.NewSource(time.Now().UnixNano()))
max := 17 + r.Intn(13) max := 17 + r.Intn(13)
bo := WithMaxTries(&ZeroBackOff{}, uint64(max)) bo := WithMaxRetries(&ZeroBackOff{}, uint64(max))
// Load up the tries count, but reset should clear the record // Load up the tries count, but reset should clear the record
for ix := 0; ix < max/2; ix++ { for ix := 0; ix < max/2; ix++ {
@ -45,7 +45,7 @@ func TestMaxTriesHappy(t *testing.T) {
func TestMaxTriesZero(t *testing.T) { func TestMaxTriesZero(t *testing.T) {
// It might not make sense, but its okay to send a zero // It might not make sense, but its okay to send a zero
bo := WithMaxTries(&ZeroBackOff{}, uint64(0)) bo := WithMaxRetries(&ZeroBackOff{}, uint64(0))
for ix := 0; ix < 11; ix++ { for ix := 0; ix < 11; ix++ {
d := bo.NextBackOff() d := bo.NextBackOff()
if d == Stop { if d == Stop {