-
Notifications
You must be signed in to change notification settings - Fork 5
/
retry.go
88 lines (75 loc) · 2.77 KB
/
retry.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package types
import (
fmt "fmt"
math "math"
"time"
"github.com/celer-network/goutils/log"
)
const (
DEFAULT_INITIAL_BACKOFF_SECONDS = 30
DEFAULT_INTERVAL_SECONDS = 30
DEFAULT_SCALING_FACTOR = 1 // linear backoff
DEFAULT_MAX_ATTEMPTS = 15 // TODO remove types.MaxExecuteRetry and implement retry max attempts
)
type RetryConfig struct {
// the initial amount of seconds to wait before the first retry
InitialBackoffSeconds uint64 `mapstructure:"initial_backoff_seconds"`
// amount of seconds in between retries, affected by scaling factor
IntervalSeconds uint64 `mapstructure:"interval_seconds"`
// for any retry count n, x = scaling factor, s = interval seconds,
// actual retry interval = s * n^x
// e.g. set x = 0 for constant backoff, 1 for linear backoff, 2 for quadratic backoff, etc...
ScalingFactor float64 `mapstructure:"scaling_factor"`
// number of retries before giving up
MaxAttempts int `mapstructure:"max_attempts"`
}
func (c *RetryConfig) ApplyDefaults() {
if c.InitialBackoffSeconds == 0 {
c.InitialBackoffSeconds = DEFAULT_INITIAL_BACKOFF_SECONDS
}
if c.IntervalSeconds == 0 {
log.Infof("[executor] retry initial backoff not set or is 0, using default %d", DEFAULT_INTERVAL_SECONDS)
c.IntervalSeconds = DEFAULT_INTERVAL_SECONDS
}
if c.ScalingFactor == 0 {
log.Infof("[executor] retry backoff scaling factor not set or is 0, using default %d", DEFAULT_SCALING_FACTOR)
c.ScalingFactor = DEFAULT_SCALING_FACTOR
}
if c.MaxAttempts == 0 {
log.Infof("[executor] retry max attempts not set or is 0, using default %d", DEFAULT_MAX_ATTEMPTS)
c.MaxAttempts = DEFAULT_MAX_ATTEMPTS
}
}
func (c *RetryConfig) GetInitialBackoff() time.Duration {
return time.Duration(c.InitialBackoffSeconds) * time.Second
}
func (c *RetryConfig) GetIntervalSeconds() time.Duration {
return time.Duration(c.IntervalSeconds) * time.Second
}
func (c *RetryConfig) ShouldBackoff(attempts uint64, lastTryTime time.Time) bool {
now := time.Now()
if attempts == 1 && now.Before(lastTryTime.Add(c.GetInitialBackoff())) {
// backoff when it's the first retry and initial backoff window has not passed
return true
}
if attempts > 0 {
curAttempt := float64(attempts + 1)
// for any retry count n, x = scaling factor, s = interval seconds,
// actual retry interval = s * n^x
actualInterval := c.GetIntervalSeconds() * time.Duration(math.Pow(curAttempt, c.ScalingFactor))
if now.Before(lastTryTime.Add(actualInterval)) {
return true
}
}
return false
}
func (c *RetryConfig) String() string {
if c == nil {
return "retry config: nil"
}
return fmt.Sprintf("retry config: initial backoff %ds, interval %ds, scaling factor %f",
c.InitialBackoffSeconds, c.IntervalSeconds, c.ScalingFactor)
}
func (c *RetryConfig) PrettyLog() {
log.Infoln(c.String())
}