Updates Backoff

This commit is contained in:
Ludovic Fernandez 2019-02-04 16:38:08 +01:00 committed by Traefiker Bot
parent 0561a20c06
commit f19c497621
32 changed files with 47 additions and 46 deletions

11
Gopkg.lock generated
View file

@ -299,12 +299,12 @@
revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9" revision = "4c0e84591b9aa9e6dcfdf3e020114cd81f89d5f9"
[[projects]] [[projects]]
branch = "master" digest = "1:cdee563173093e5ae7ab2a19c298e0904129719e1919a3c532b7bb0c3398b818"
digest = "1:97b8422f271b83e760ce1279715d0f5d124f267459b56806306a64a0f82721c9" name = "github.com/cenkalti/backoff"
name = "github.com/cenk/backoff"
packages = ["."] packages = ["."]
pruneopts = "NUT" pruneopts = "NUT"
revision = "2ea60e5f094469f9e65adb9cd103795b73ae743e" revision = "1e4cf3da559842a91afcb6ea6141451e6c30c618"
version = "v2.1.1"
[[projects]] [[projects]]
digest = "1:03cfacdc6bfd46007c15786c1ece3fa074f89e5193a292f0f26d9e98c99c7cc2" digest = "1:03cfacdc6bfd46007c15786c1ece3fa074f89e5193a292f0f26d9e98c99c7cc2"
@ -2278,7 +2278,7 @@
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface", "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface",
"github.com/aws/aws-sdk-go/service/ec2", "github.com/aws/aws-sdk-go/service/ec2",
"github.com/aws/aws-sdk-go/service/ecs", "github.com/aws/aws-sdk-go/service/ecs",
"github.com/cenk/backoff", "github.com/cenkalti/backoff",
"github.com/containous/alice", "github.com/containous/alice",
"github.com/containous/flaeg", "github.com/containous/flaeg",
"github.com/containous/flaeg/parse", "github.com/containous/flaeg/parse",
@ -2364,6 +2364,7 @@
"github.com/vulcand/oxy/ratelimit", "github.com/vulcand/oxy/ratelimit",
"github.com/vulcand/oxy/roundrobin", "github.com/vulcand/oxy/roundrobin",
"github.com/vulcand/oxy/utils", "github.com/vulcand/oxy/utils",
"github.com/vulcand/predicate",
"github.com/xenolf/lego/certcrypto", "github.com/xenolf/lego/certcrypto",
"github.com/xenolf/lego/certificate", "github.com/xenolf/lego/certificate",
"github.com/xenolf/lego/challenge", "github.com/xenolf/lego/challenge",

View file

@ -54,8 +54,8 @@
version = "1.13.11" version = "1.13.11"
[[constraint]] [[constraint]]
branch = "master" name = "github.com/cenkalti/backoff"
name = "github.com/cenk/backoff" version = "2.1.1"
[[constraint]] [[constraint]]
name = "github.com/containous/flaeg" name = "github.com/containous/flaeg"

View file

@ -16,7 +16,7 @@ import (
"time" "time"
"github.com/BurntSushi/ty/fun" "github.com/BurntSushi/ty/fun"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/mux" "github.com/containous/mux"
"github.com/containous/staert" "github.com/containous/staert"
"github.com/containous/traefik/cluster" "github.com/containous/traefik/cluster"

View file

@ -5,7 +5,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/cluster" "github.com/containous/traefik/cluster"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"

View file

@ -7,7 +7,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/cluster" "github.com/containous/traefik/cluster"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"

View file

@ -8,7 +8,7 @@ import (
"time" "time"
"github.com/abronan/valkeyrie/store" "github.com/abronan/valkeyrie/store"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/staert" "github.com/containous/staert"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"

View file

@ -5,7 +5,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/mux" "github.com/containous/mux"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/old/types" "github.com/containous/traefik/old/types"

View file

@ -12,7 +12,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/flaeg" "github.com/containous/flaeg"
"github.com/containous/staert" "github.com/containous/staert"
"github.com/containous/traefik/autogen/genstatic" "github.com/containous/traefik/autogen/genstatic"

View file

@ -3,7 +3,7 @@ package job
import ( import (
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
) )
var ( var (

View file

@ -4,7 +4,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
) )
func TestJobBackOff(t *testing.T) { func TestJobBackOff(t *testing.T) {

View file

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/BurntSushi/ty/fun" "github.com/BurntSushi/ty/fun"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"

View file

@ -12,7 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"

View file

@ -13,7 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs" "github.com/aws/aws-sdk-go/service/ecs"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"

View file

@ -5,7 +5,7 @@ import (
"time" "time"
"github.com/ArthurHlt/go-eureka-client/eureka" "github.com/ArthurHlt/go-eureka-client/eureka"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/flaeg/parse" "github.com/containous/flaeg/parse"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"

View file

@ -15,7 +15,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"

View file

@ -8,7 +8,7 @@ import (
"github.com/abronan/valkeyrie" "github.com/abronan/valkeyrie"
"github.com/abronan/valkeyrie/store" "github.com/abronan/valkeyrie/store"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"

View file

@ -5,7 +5,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/provider" "github.com/containous/traefik/old/provider"

View file

@ -5,7 +5,7 @@ import (
"os" "os"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/types" "github.com/containous/traefik/old/types"

View file

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/old/log" "github.com/containous/traefik/old/log"
"github.com/containous/traefik/old/types" "github.com/containous/traefik/old/types"

View file

@ -6,7 +6,7 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/mux" "github.com/containous/mux"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
"github.com/containous/traefik/safe" "github.com/containous/traefik/safe"

View file

@ -13,7 +13,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/flaeg/parse" "github.com/containous/flaeg/parse"
"github.com/containous/traefik/config" "github.com/containous/traefik/config"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"

View file

@ -11,7 +11,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/config" "github.com/containous/traefik/config"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"

View file

@ -9,7 +9,7 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/flaeg/parse" "github.com/containous/flaeg/parse"
"github.com/containous/traefik/config" "github.com/containous/traefik/config"
"github.com/containous/traefik/job" "github.com/containous/traefik/job"

View file

@ -6,7 +6,7 @@ import (
"runtime/debug" "runtime/debug"
"sync" "sync"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
"github.com/containous/traefik/log" "github.com/containous/traefik/log"
) )

View file

@ -7,7 +7,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/cenk/backoff" "github.com/cenkalti/backoff"
) )
func TestNewPoolContext(t *testing.T) { func TestNewPoolContext(t *testing.T) {

View file

@ -1,9 +1,8 @@
package backoff package backoff
import ( import (
"context"
"time" "time"
"golang.org/x/net/context"
) )
// BackOffContext is a backoff policy that stops retrying after the context // BackOffContext is a backoff policy that stops retrying after the context
@ -52,9 +51,13 @@ func (b *backOffContext) Context() context.Context {
func (b *backOffContext) NextBackOff() time.Duration { func (b *backOffContext) NextBackOff() time.Duration {
select { select {
case <-b.Context().Done(): case <-b.ctx.Done():
return Stop return Stop
default: default:
return b.BackOff.NextBackOff()
} }
next := b.BackOff.NextBackOff()
if deadline, ok := b.ctx.Deadline(); ok && deadline.Sub(time.Now()) < next {
return Stop
}
return next
} }

View file

@ -63,7 +63,6 @@ type ExponentialBackOff struct {
currentInterval time.Duration currentInterval time.Duration
startTime time.Time startTime time.Time
random *rand.Rand
} }
// Clock is an interface that returns current time for BackOff. // Clock is an interface that returns current time for BackOff.
@ -89,7 +88,6 @@ func NewExponentialBackOff() *ExponentialBackOff {
MaxInterval: DefaultMaxInterval, MaxInterval: DefaultMaxInterval,
MaxElapsedTime: DefaultMaxElapsedTime, MaxElapsedTime: DefaultMaxElapsedTime,
Clock: SystemClock, Clock: SystemClock,
random: rand.New(rand.NewSource(time.Now().UnixNano())),
} }
b.Reset() b.Reset()
return b return b
@ -118,10 +116,7 @@ func (b *ExponentialBackOff) NextBackOff() time.Duration {
return Stop return Stop
} }
defer b.incrementCurrentInterval() defer b.incrementCurrentInterval()
if b.random == nil { return getRandomValueFromInterval(b.RandomizationFactor, rand.Float64(), b.currentInterval)
b.random = rand.New(rand.NewSource(time.Now().UnixNano()))
}
return getRandomValueFromInterval(b.RandomizationFactor, b.random.Float64(), b.currentInterval)
} }
// GetElapsedTime returns the elapsed time since an ExponentialBackOff instance // GetElapsedTime returns the elapsed time since an ExponentialBackOff instance

View file

@ -15,7 +15,6 @@ type Notify func(error, time.Duration)
// Retry the operation o until it does not return error or BackOff stops. // Retry the operation o until it does not return error or BackOff stops.
// o is guaranteed to be run at least once. // o is guaranteed to be run at least once.
// It is the caller's responsibility to reset b after Retry returns.
// //
// If o returns a *PermanentError, the operation is not retried, and the // If o returns a *PermanentError, the operation is not retried, and the
// wrapped error is returned. // wrapped error is returned.
@ -29,6 +28,7 @@ func Retry(o Operation, b BackOff) error { return RetryNotify(o, b, nil) }
func RetryNotify(operation Operation, b BackOff, notify Notify) error { func RetryNotify(operation Operation, b BackOff, notify Notify) error {
var err error var err error
var next time.Duration var next time.Duration
var t *time.Timer
cb := ensureContext(b) cb := ensureContext(b)
@ -42,7 +42,7 @@ func RetryNotify(operation Operation, b BackOff, notify Notify) error {
return permanent.Err return permanent.Err
} }
if next = b.NextBackOff(); next == Stop { if next = cb.NextBackOff(); next == Stop {
return err return err
} }
@ -50,11 +50,15 @@ func RetryNotify(operation Operation, b BackOff, notify Notify) error {
notify(err, next) notify(err, next)
} }
t := time.NewTimer(next) if t == nil {
t = time.NewTimer(next)
defer t.Stop()
} else {
t.Reset(next)
}
select { select {
case <-cb.Context().Done(): case <-cb.Context().Done():
t.Stop()
return err return err
case <-t.C: case <-t.C:
} }

View file

@ -1,7 +1,6 @@
package backoff package backoff
import ( import (
"runtime"
"sync" "sync"
"time" "time"
) )
@ -34,7 +33,6 @@ func NewTicker(b BackOff) *Ticker {
} }
t.b.Reset() t.b.Reset()
go t.run() go t.run()
runtime.SetFinalizer(t, (*Ticker).Stop)
return t return t
} }