-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathconfig.go
68 lines (60 loc) · 1.68 KB
/
config.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
package retry
import (
"time"
)
// Config defines the config for addon.
type Config struct {
// InitialInterval defines the initial time interval for backoff algorithm.
//
// Optional. Default: 1 * time.Second
InitialInterval time.Duration
// MaxBackoffTime defines maximum time duration for backoff algorithm. When
// the algorithm is reached this time, rest of the retries will be maximum
// 32 seconds.
//
// Optional. Default: 32 * time.Second
MaxBackoffTime time.Duration
// Multiplier defines multiplier number of the backoff algorithm.
//
// Optional. Default: 2.0
Multiplier float64
// MaxRetryCount defines maximum retry count for the backoff algorithm.
//
// Optional. Default: 10
MaxRetryCount int
// currentInterval tracks the current waiting time.
//
// Optional. Default: 1 * time.Second
currentInterval time.Duration
}
// DefaultConfig is the default config for retry.
var DefaultConfig = Config{
InitialInterval: 1 * time.Second,
MaxBackoffTime: 32 * time.Second,
Multiplier: 2.0,
MaxRetryCount: 10,
currentInterval: 1 * time.Second,
}
// configDefault sets the config values if they are not set.
func configDefault(config ...Config) Config {
if len(config) == 0 {
return DefaultConfig
}
cfg := config[0]
if cfg.InitialInterval == 0 {
cfg.InitialInterval = DefaultConfig.InitialInterval
}
if cfg.MaxBackoffTime == 0 {
cfg.MaxBackoffTime = DefaultConfig.MaxBackoffTime
}
if cfg.Multiplier <= 0 {
cfg.Multiplier = DefaultConfig.Multiplier
}
if cfg.MaxRetryCount <= 0 {
cfg.MaxRetryCount = DefaultConfig.MaxRetryCount
}
if cfg.currentInterval != cfg.InitialInterval {
cfg.currentInterval = DefaultConfig.currentInterval
}
return cfg
}