@@ -16,6 +16,7 @@ type BackoffConfig struct {
16
16
type ExponentialBackoffConfig struct {
17
17
BackoffConfig
18
18
Multiplier float64 // Multiplier for exponential growth
19
+ JitterType JitterType
19
20
}
20
21
21
22
// LinearBackoffConfig contains configuration for linear backoff
@@ -36,25 +37,8 @@ const (
36
37
EqualJitter
37
38
)
38
39
39
- // ExponentialBackoffWithJitterConfig contains configuration for exponential backoff with jitter
40
- type ExponentialBackoffWithJitterConfig struct {
41
- ExponentialBackoffConfig
42
- JitterType JitterType
43
- }
44
-
45
40
// NewExponentialBackoff creates a new exponential backoff function
46
41
func NewExponentialBackoff (config ExponentialBackoffConfig ) BackoffDelayFunc {
47
- return func (attempt int ) time.Duration {
48
- delay := time .Duration (float64 (config .BaseDelay ) * math .Pow (config .Multiplier , float64 (attempt )))
49
- if delay > config .MaxDelay {
50
- delay = config .MaxDelay
51
- }
52
- return delay
53
- }
54
- }
55
-
56
- // NewExponentialBackoffWithJitter creates a new exponential backoff function with jitter
57
- func NewExponentialBackoffWithJitter (config ExponentialBackoffWithJitterConfig ) BackoffDelayFunc {
58
42
return func (attempt int ) time.Duration {
59
43
baseDelay := time .Duration (float64 (config .BaseDelay ) * math .Pow (config .Multiplier , float64 (attempt )))
60
44
if baseDelay > config .MaxDelay {
@@ -104,31 +88,28 @@ func DefaultExponentialBackoff() BackoffDelayFunc {
104
88
MaxDelay : 5 * time .Second ,
105
89
},
106
90
Multiplier : 2.0 ,
91
+ JitterType : NoJitter ,
107
92
})
108
93
}
109
94
110
95
func DefaultExponentialBackoffWithFullJitter () BackoffDelayFunc {
111
- return NewExponentialBackoffWithJitter (ExponentialBackoffWithJitterConfig {
112
- ExponentialBackoffConfig : ExponentialBackoffConfig {
113
- BackoffConfig : BackoffConfig {
114
- BaseDelay : 100 * time .Millisecond ,
115
- MaxDelay : 5 * time .Second ,
116
- },
117
- Multiplier : 2.0 ,
96
+ return NewExponentialBackoff (ExponentialBackoffConfig {
97
+ BackoffConfig : BackoffConfig {
98
+ BaseDelay : 100 * time .Millisecond ,
99
+ MaxDelay : 5 * time .Second ,
118
100
},
101
+ Multiplier : 2.0 ,
119
102
JitterType : FullJitter ,
120
103
})
121
104
}
122
105
123
106
func DefaultExponentialBackoffWithEqualJitter () BackoffDelayFunc {
124
- return NewExponentialBackoffWithJitter (ExponentialBackoffWithJitterConfig {
125
- ExponentialBackoffConfig : ExponentialBackoffConfig {
126
- BackoffConfig : BackoffConfig {
127
- BaseDelay : 100 * time .Millisecond ,
128
- MaxDelay : 5 * time .Second ,
129
- },
130
- Multiplier : 2.0 ,
107
+ return NewExponentialBackoff (ExponentialBackoffConfig {
108
+ BackoffConfig : BackoffConfig {
109
+ BaseDelay : 100 * time .Millisecond ,
110
+ MaxDelay : 5 * time .Second ,
131
111
},
112
+ Multiplier : 2.0 ,
132
113
JitterType : EqualJitter ,
133
114
})
134
115
}
0 commit comments