/
backoff.go
55 lines (43 loc) · 1.58 KB
/
backoff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2017 ScyllaDB
package retry
import (
"time"
"github.com/cenkalti/backoff"
)
// Backoff specifies a policy for how long to wait between retries.
// It is called after a failing request, to determine the amount of time
// that should pass before trying again.
type Backoff = backoff.BackOff
// Stop indicates that no more retries should be made.
const Stop time.Duration = -1
// NewExponentialBackoff returns Backoff implementation that increases each
// wait period exponentially.
// Multiplier controls how fast each wait period grows, and randomizationFactor
// allows to inject some jitter between periods.
func NewExponentialBackoff(initialInterval, maxElapsedTime, maxInterval time.Duration, multiplier, randomizationFactor float64) Backoff {
b := backoff.NewExponentialBackOff()
b.InitialInterval = initialInterval
b.MaxElapsedTime = maxElapsedTime
b.MaxInterval = maxInterval
b.Multiplier = multiplier
b.RandomizationFactor = randomizationFactor
b.Reset()
return b
}
// WithMaxRetries allows to set maximum number of retries for given backoff strategy.
func WithMaxRetries(b Backoff, maxRetries uint64) Backoff {
return backoff.WithMaxRetries(b, maxRetries)
}
// BackoffFunc type is an adapter to allow the use of ordinary
// functions as Backoff.
type BackoffFunc func() time.Duration
// NextBackOff returns the duration to wait before retrying the operation.
func (b BackoffFunc) NextBackOff() time.Duration {
return b()
}
// Reset to initial state.
func (b BackoffFunc) Reset() {}
// Clone returns a copy of BackoffFunc.
func (b BackoffFunc) Clone() Backoff {
return b
}