forked from googlearchive/go-gcm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backoff.go
62 lines (52 loc) · 1.41 KB
/
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
57
58
59
60
61
62
package gcm
import (
"time"
"github.com/jpillora/backoff"
)
const (
// DefaultMinBackoff is a default min delay for backoff.
DefaultMinBackoff = 1 * time.Second
// DefaultMaxBackoff is the default max delay for backoff.
DefaultMaxBackoff = 10 * time.Second
)
// backoffProvider defines an interface for backoff.
type backoffProvider interface {
sendAnother() bool
getMin() time.Duration
setMin(min time.Duration)
wait()
}
// Implementation of backoff provider using exponential backoff.
type exponentialBackoff struct {
b backoff.Backoff
currentDelay time.Duration
}
// Factory method for exponential backoff, uses default values for Min and Max and
// adds Jitter.
func newExponentialBackoff() backoffProvider {
b := &backoff.Backoff{
Min: DefaultMinBackoff,
Max: DefaultMaxBackoff,
Jitter: true,
}
return &exponentialBackoff{b: *b, currentDelay: b.Duration()}
}
// Returns true if not over the retries limit.
func (eb *exponentialBackoff) sendAnother() bool {
return eb.currentDelay <= eb.b.Max
}
func (eb *exponentialBackoff) getMin() time.Duration {
return eb.b.Min
}
// Set the minumim delay for backoff.
func (eb *exponentialBackoff) setMin(min time.Duration) {
eb.b.Min = min
if (eb.currentDelay) < min {
eb.currentDelay = min
}
}
// Wait for the current value of backoff.
func (eb *exponentialBackoff) wait() {
time.Sleep(eb.currentDelay)
eb.currentDelay = eb.b.Duration()
}