-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.go
41 lines (33 loc) · 1.05 KB
/
fibonacci.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
package goretry
import "time"
/* Fibonacci performs retry function with the waiting duration calculated using Fibonacci sequence.
It will wait for firstWaiting to do the first retry*/
func Fibonacci(startWaiting time.Duration, action func() error) {
std.Fibonacci(startWaiting, action)
}
/* Fibonacci performs retry function with the waiting duration calculated using Fibonacci sequence.
It will wait for firstWaiting to do the first retry*/
func (i *Instance) Fibonacci(startWaiting time.Duration, action func() error) {
var count int64
var totalWaiting time.Duration
lastWaiting := time.Duration(0)
secondLastWaiting := startWaiting
var backoff time.Duration
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
}
backoff = i.sleep(lastWaiting + secondLastWaiting)
totalWaiting += backoff
secondLastWaiting = lastWaiting
lastWaiting = backoff
}
}