blob: 28d58ca37c684020e7db4e1f5a1394cc4b4edef9 [file] [log] [blame]
Abhay Kumara2ae5992025-11-10 14:02:24 +00001package 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 return Stop
25 }
26 if b.maxTries > 0 {
27 if b.maxTries <= b.numTries {
28 return Stop
29 }
30 b.numTries++
31 }
32 return b.delegate.NextBackOff()
33}
34
35func (b *backOffTries) Reset() {
36 b.numTries = 0
37 b.delegate.Reset()
38}