Debian Codegraph @

backoff.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "math"
9 "math/rand"
10 "sync"
11 "time"
12 )
13
14 // BackoffFunc specifies the signature of a function that returns the
15 // time to wait before the next call to a resource. To stop retrying
16 // return false in the 2nd return value.
17 type BackoffFunc func(retry int) (time.Duration, bool)
18
19 // Backoff allows callers to implement their own Backoff strategy.
20 type Backoff interface {
21 // Next implements a BackoffFunc.
22 Next(retry int) (time.Duration, bool)
23 }
24
25 // -- ZeroBackoff --
26
27 // ZeroBackoff is a fixed backoff policy whose backoff time is always zero,
28 // meaning that the operation is retried immediately without waiting,
29 // indefinitely.
30 type ZeroBackoff struct{}
31
32 // Next implements BackoffFunc for ZeroBackoff.
33 func (b ZeroBackoff) Next(retry int) (time.Duration, bool) {
34 return 0, true
35 }
36
37 // -- StopBackoff --
38
39 // StopBackoff is a fixed backoff policy that always returns false for
40 // Next(), meaning that the operation should never be retried.
41 type StopBackoff struct{}
42
43 // Next implements BackoffFunc for StopBackoff.
44 func (b StopBackoff) Next(retry int) (time.Duration, bool) {
45 return 0, false
46 }
47
48 // -- ConstantBackoff --
49
50 // ConstantBackoff is a backoff policy that always returns the same delay.
51 type ConstantBackoff struct {
52 interval time.Duration
53 }
54
55 // NewConstantBackoff returns a new ConstantBackoff.
56 func NewConstantBackoff(interval time.Duration) *ConstantBackoff {
57 return &ConstantBackoff{interval: interval}
58 }
59
60 // Next implements BackoffFunc for ConstantBackoff.
61 func (b *ConstantBackoff) Next(retry int) (time.Duration, bool) {
62 return b.interval, true
63 }
64
65 // -- Exponential --
66
67 // ExponentialBackoff implements the simple exponential backoff described by
68 // Douglas Thain at http://dthain.blogspot.de/2009/02/exponential-backoff-in-distributed.html.
69 type ExponentialBackoff struct {
70 t float64 // initial timeout (in msec)
71 f float64 // exponential factor (e.g. 2)
72 m float64 // maximum timeout (in msec)
73 }
74
75 // NewExponentialBackoff returns a ExponentialBackoff backoff policy.
76 // Use initialTimeout to set the first/minimal interval
77 // and maxTimeout to set the maximum wait interval.
78 func NewExponentialBackoff(initialTimeout, maxTimeout time.Duration) *ExponentialBackoff {
79 return &ExponentialBackoff{
80 t: float64(int64(initialTimeout / time.Millisecond)),
81 f: 2.0,
82 m: float64(int64(maxTimeout / time.Millisecond)),
83 }
84 }
85
86 // Next implements BackoffFunc for ExponentialBackoff.
87 func (b *ExponentialBackoff) Next(retry int) (time.Duration, bool) {
88 r := 1.0 + rand.Float64() // random number in [1..2]
89 m := math.Min(r*b.t*math.Pow(b.f, float64(retry)), b.m)
90 if m >= b.m {
91 return 0, false
92 }
93 d := time.Duration(int64(m)) * time.Millisecond
94 return d, true
95 }
96
97 // -- Simple Backoff --
98
99 // SimpleBackoff takes a list of fixed values for backoff intervals.
100 // Each call to Next returns the next value from that fixed list.
101 // After each value is returned, subsequent calls to Next will only return
102 // the last element. The values are optionally "jittered" (off by default).
103 type SimpleBackoff struct {
104 sync.Mutex
105 ticks []int
106 jitter bool
107 }
108
109 // NewSimpleBackoff creates a SimpleBackoff algorithm with the specified
110 // list of fixed intervals in milliseconds.
111 func NewSimpleBackoff(ticks ...int) *SimpleBackoff {
112 return &SimpleBackoff{
113 ticks: ticks,
114 jitter: false,
115 }
116 }
117
118 // Jitter enables or disables jittering values.
119 func (b *SimpleBackoff) Jitter(flag bool) *SimpleBackoff {
120 b.Lock()
121 b.jitter = flag
122 b.Unlock()
123 return b
124 }
125
126 // jitter randomizes the interval to return a value of [0.5*millis .. 1.5*millis].
127 func jitter(millis int) int {
128 if millis <= 0 {
129 return 0
130 }
131 return millis/2 + rand.Intn(millis)
132 }
133
134 // Next implements BackoffFunc for SimpleBackoff.
135 func (b *SimpleBackoff) Next(retry int) (time.Duration, bool) {
136 b.Lock()
137 defer b.Unlock()
138
139 if retry >= len(b.ticks) {
140 return 0, false
141 }
142
143 ms := b.ticks[retry]
144 if b.jitter {
145 ms = jitter(ms)
146 }
147 return time.Duration(ms) * time.Millisecond, true
148 }