github.com/cenkalti/backoff/v4@v4.2.1/tries.go (about)

     1  package backoff
     2  
     3  import "time"
     4  
     5  /*
     6  WithMaxRetries creates a wrapper around another BackOff, which will
     7  return Stop if NextBackOff() has been called too many times since
     8  the last time Reset() was called
     9  
    10  Note: Implementation is not thread-safe.
    11  */
    12  func WithMaxRetries(b BackOff, max uint64) BackOff {
    13  	return &backOffTries{delegate: b, maxTries: max}
    14  }
    15  
    16  type backOffTries struct {
    17  	delegate BackOff
    18  	maxTries uint64
    19  	numTries uint64
    20  }
    21  
    22  func (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  
    35  func (b *backOffTries) Reset() {
    36  	b.numTries = 0
    37  	b.delegate.Reset()
    38  }