-
Notifications
You must be signed in to change notification settings - Fork 15
/
officer.go
70 lines (62 loc) · 1.36 KB
/
officer.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
69
70
package retry
import (
"math"
"time"
)
// Officer sleeps or selects any amount of time for each try
type Officer struct {
Block func(Try)
}
// Constant sleeps for duration duration
func Constant(duration time.Duration) *Officer {
o := Officer{
Block: func(t Try) {
time.Sleep(duration)
},
}
return &o
}
// Incremental sleeps for duration + the number of tries
func Incremental(duration time.Duration) *Officer {
o := Officer{
Block: func(t Try) {
time.Sleep(duration + time.Duration(t.Count))
},
}
return &o
}
// Linear sleeps for duration * the number of tries
func Linear(duration time.Duration) *Officer {
o := Officer{
Block: func(t Try) {
time.Sleep(duration * time.Duration(t.Count))
},
}
return &o
}
// Exponential sleeps for duration base * 2^tries
func Exponential(base time.Duration) *Officer {
o := Officer{
Block: func(t Try) {
time.Sleep(time.Duration(float64(base) * math.Exp(float64(t.Count))))
},
}
return &o
}
// Fibonacci sleeps for duration * fib(tries)
//TODO:See if we can use a context to prevent the full calculation for each try...
func Fibonacci(duration time.Duration) *Officer {
o := Officer{
Block: func(t Try) {
var pre int64
var cur int64
var i uint
for pre, cur, i = 0, 1, 0; i < t.Count; i++ {
pre = cur
cur = pre + cur
}
time.Sleep(duration * time.Duration(pre))
},
}
return &o
}