tries.go 724 B

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