try.go 788 B

1234567891011121314151617181920212223242526272829303132333435
  1. package s3
  2. import "errors"
  3. // MaxRetries is the maximum number of retries before bailing.
  4. var MaxRetries = 10
  5. var errMaxRetriesReached = errors.New("exceeded retry limit")
  6. // Func represents functions that can be retried.
  7. type Func func(attempt int) (retry bool, err error)
  8. // Do keeps trying the function until the second argument
  9. // returns false, or no error is returned.
  10. func Do(fn Func) error {
  11. var err error
  12. var cont bool
  13. attempt := 1
  14. for {
  15. cont, err = fn(attempt)
  16. if !cont || err == nil {
  17. break
  18. }
  19. attempt++
  20. if attempt > MaxRetries {
  21. return errMaxRetriesReached
  22. }
  23. }
  24. return err
  25. }
  26. // IsMaxRetries checks whether the error is due to hitting the
  27. // maximum number of retries or not.
  28. func IsMaxRetries(err error) bool {
  29. return err == errMaxRetriesReached
  30. }