Files
weave-scope/vendor/github.com/weaveworks/common/backoff/backoff.go
Roland Schilter c817eccb9b Update github.com/weaveworks/common & deps
Bumped all packages that make the build fail:

    gvt update github.com/golang/protobuf/proto
    gvt fetch github.com/golang/protobuf/ptypes
    gvt fetch google.golang.org/genproto/googleapis/rpc/status
    gvt update google.golang.org/grpc/status
    gvt update google.golang.org/grpc/transport
    gvt update golang.org/x/net/http2
2017-06-21 12:19:17 +02:00

98 lines
2.0 KiB
Go

package backoff
import (
"time"
log "github.com/Sirupsen/logrus"
)
type backoff struct {
f func() (bool, error)
quit, done chan struct{}
msg string
initialBackoff, maxBackoff time.Duration
}
// Interface does f in a loop, sleeping for initialBackoff between
// each iterations. If it hits an error, it exponentially backs
// off to maxBackoff. Backoff will log when it backs off, but
// will stop logging when it reaches maxBackoff. It will also
// log on first success in the beginning and after errors.
type Interface interface {
Start()
Stop()
SetInitialBackoff(time.Duration)
SetMaxBackoff(time.Duration)
}
// New makes a new Interface
func New(f func() (bool, error), msg string) Interface {
return &backoff{
f: f,
quit: make(chan struct{}),
done: make(chan struct{}),
msg: msg,
initialBackoff: 10 * time.Second,
maxBackoff: 60 * time.Second,
}
}
func (b *backoff) SetInitialBackoff(d time.Duration) {
b.initialBackoff = d
}
func (b *backoff) SetMaxBackoff(d time.Duration) {
b.maxBackoff = d
}
// Stop the backoff, and waits for it to stop.
func (b *backoff) Stop() {
close(b.quit)
<-b.done
}
// Start the backoff. Can only be called once.
func (b *backoff) Start() {
defer close(b.done)
backoff := b.initialBackoff
shouldLog := true
for {
done, err := b.f()
if done {
return
}
if err != nil {
backoff *= 2
shouldLog = true
if backoff > b.maxBackoff {
backoff = b.maxBackoff
shouldLog = false
}
} else {
backoff = b.initialBackoff
}
if shouldLog {
if err != nil {
log.Warnf("Error %s, backing off %s: %s",
b.msg, backoff, err)
} else {
log.Infof("Success %s", b.msg)
}
}
// Re-enable logging if we came from an error (suppressed or not)
// since we want to log in case a success follows.
shouldLog = err != nil
select {
case <-time.After(backoff):
case <-b.quit:
return
}
}
}