-
Notifications
You must be signed in to change notification settings - Fork 0
/
polynomial.go
38 lines (32 loc) · 1.11 KB
/
polynomial.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
package goretry
import (
"time"
)
/* Polynomial performs retry function with backoff time calculated by Polynomial function.
It will wait for baseTime to do the first retry, and then increase the waiting time by time = attempt ^ degree * baseTime */
func Polynomial(baseTime time.Duration, degree int64, action func() error) {
std.Polynomial(baseTime, degree, action)
}
/* Polynomial performs retry function with backoff time calculated by Polynomial function.
It will wait for baseTime to do the first retry, and then increase the waiting time by time = attempt ^ degree * baseTime */
func (i *Instance) Polynomial(baseTime time.Duration, degree int64, 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((count+1), degree))
totalWaiting += backoff
}
}