-
Notifications
You must be signed in to change notification settings - Fork 0
/
exponential.go
36 lines (30 loc) · 1.06 KB
/
exponential.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
package goretry
import "time"
/* Exponential performs retry function with backoff time calculated by exponential function.
It will wait for baseTime to do the first retry, and then increase the waiting time by time = 2 ^ attempt * baseTime */
func Exponential(baseTime time.Duration, action func() error) {
std.Exponential(baseTime, action)
}
/* Exponential performs retry function with backoff time calculated by exponential function.
It will wait for baseTime to do the first retry, and then increase the waiting time by time = 2 ^ attempt * baseTime */
func (i *Instance) Exponential(baseTime time.Duration, action func() error) {
var count int64
var totalWaiting time.Duration
backoff := baseTime
for {
i.log("do action()")
if err := action(); err == nil {
return
}
count++
if i.MaxStopRetries != NoLimit && count >= i.MaxStopRetries {
break
}
if i.MaxStopTotalWaiting != NoDuration && totalWaiting >= i.MaxStopTotalWaiting {
break
}
i.sleep(backoff)
backoff = baseTime * time.Duration(intPow(2, (count+1)))
totalWaiting += backoff
}
}