/
backoff.go
127 lines (98 loc) · 2.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018-2019 Authors of Cilium
package backoff
import (
"context"
"fmt"
"math"
"time"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/rand"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "backoff")
randGen = rand.NewSafeRand(time.Now().UnixNano())
)
// NodeManager is the interface required to implement cluster size dependent
// intervals
type NodeManager interface {
ClusterSizeDependantInterval(baseInterval time.Duration) time.Duration
}
// Exponential implements an exponential backoff
type Exponential struct {
// Min is the minimal backoff time, if unspecified, 1 second will be
// used
Min time.Duration
// Max is the maximum backoff time, if unspecified, no maximum time is
// applied
Max time.Duration
// Factor is the factor the backoff time grows exponentially, if
// unspecified, a factor of 2.0 will be used
Factor float64
// Jitter, when enabled, adds random jitter to the interval
Jitter bool
// NodeManager enables the use of cluster size dependent backoff
// intervals, i.e. the larger the cluster, the longer the backoff
// interval
NodeManager NodeManager
// Name is a free form string describing the operation subject to the
// backoff, if unspecified, a UUID is generated. This string is used
// for logging purposes.
Name string
attempt int
}
// CalculateDuration calculates the backoff duration based on minimum base
// interval, exponential factor, jitter and number of failures.
func CalculateDuration(min, max time.Duration, factor float64, jitter bool, failures int) time.Duration {
minFloat := float64(min)
maxFloat := float64(max)
t := minFloat * math.Pow(factor, float64(failures))
if max != time.Duration(0) && t > maxFloat {
t = maxFloat
}
if jitter {
t = randGen.Float64()*(t-minFloat) + minFloat
}
return time.Duration(t)
}
// Wait waits for the required time using an exponential backoff
func (b *Exponential) Wait(ctx context.Context) error {
b.attempt++
t := b.Duration(b.attempt)
log.WithFields(logrus.Fields{
"time": t,
"attempt": b.attempt,
"name": b.Name,
}).Debug("Sleeping with exponential backoff")
select {
case <-ctx.Done():
return fmt.Errorf("exponential backoff cancelled via context: %s", ctx.Err())
case <-time.After(t):
}
return nil
}
// Duration returns the wait duration for the nth attempt
func (b *Exponential) Duration(attempt int) time.Duration {
if b.Name == "" {
b.Name = uuid.New().String()
}
min := time.Duration(1) * time.Second
if b.Min != time.Duration(0) {
min = b.Min
}
factor := float64(2)
if b.Factor != float64(0) {
factor = b.Factor
}
t := CalculateDuration(min, b.Max, factor, b.Jitter, attempt)
if b.NodeManager != nil {
t = b.NodeManager.ClusterSizeDependantInterval(t)
}
if b.Max != time.Duration(0) && t > b.Max {
t = b.Max
}
return t
}