-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff.go
56 lines (43 loc) · 912 Bytes
/
backoff.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
package config
import (
"time"
"github.com/tj/backoff"
)
// Backoff config.
type Backoff struct {
// Min time in milliseconds.
Min int `json:"min"`
// Max time in milliseconds.
Max int `json:"max"`
// Factor applied for every attempt.
Factor float64 `json:"factor"`
// Attempts performed before failing.
Attempts int `json:"attempts"`
// Jitter is applied when true.
Jitter bool `json:"jitter"`
}
// Default implementation.
func (b *Backoff) Default() error {
if b.Min == 0 {
b.Min = 100
}
if b.Max == 0 {
b.Max = 500
}
if b.Factor == 0 {
b.Factor = 2
}
if b.Attempts == 0 {
b.Attempts = 3
}
return nil
}
// Backoff returns the backoff from config.
func (b *Backoff) Backoff() *backoff.Backoff {
return &backoff.Backoff{
Min: time.Duration(b.Min) * time.Millisecond,
Max: time.Duration(b.Max) * time.Millisecond,
Factor: b.Factor,
Jitter: b.Jitter,
}
}