forked from robertotambunan/Jaen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
48 lines (39 loc) · 829 Bytes
/
handler.go
File metadata and controls
48 lines (39 loc) · 829 Bytes
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
package jaen
import (
"math"
"math/rand"
"time"
)
func (b *Backoff) Do() time.Duration {
b.Touch++
return b.getTime()
}
func (b *Backoff) getTime() time.Duration {
timeMin := b.MinTime
timeMax := b.MaxTime
if timeMin <= 0 {
timeMin = 500 * time.Millisecond
}
if timeMax <= 0 {
timeMax = 30 * time.Second
}
if timeMin >= timeMax {
timeMax = timeMin + (1 * time.Second)
}
factorTime := float64(timeMin) * math.Pow(2, float64(b.Touch))
duration := randInt64(int64(timeMin), int64(factorTime))
if duration > int64(timeMax) {
return timeMax
}
return time.Duration(duration)
}
func randInt64(min int64, max int64) int64 {
rand.Seed(time.Now().UTC().UnixNano())
return min + rand.Int63n(max-min)
}
func (b *Backoff) Reset() {
b.Touch = 0
}
func (b *Backoff) getTotalTouch() int {
return b.Touch
}