blob: cfeefd9b764c4272f518e7db3331c31a35d84834 [file] [log] [blame]
Girish Gowdra64503432020-01-07 10:59:10 +05301package backoff
2
3import "time"
4
5/*
6WithMaxRetries creates a wrapper around another BackOff, which will
7return Stop if NextBackOff() has been called too many times since
8the last time Reset() was called
9
10Note: Implementation is not thread-safe.
11*/
12func WithMaxRetries(b BackOff, max uint64) BackOff {
13 return &backOffTries{delegate: b, maxTries: max}
14}
15
16type backOffTries struct {
17 delegate BackOff
18 maxTries uint64
19 numTries uint64
20}
21
22func (b *backOffTries) NextBackOff() time.Duration {
23 if b.maxTries > 0 {
24 if b.maxTries <= b.numTries {
25 return Stop
26 }
27 b.numTries++
28 }
29 return b.delegate.NextBackOff()
30}
31
32func (b *backOffTries) Reset() {
33 b.numTries = 0
34 b.delegate.Reset()
35}