retry.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2017 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "fmt"
  16. "time"
  17. "github.com/pkg/errors"
  18. )
  19. // Retry will attempt the given function until it succeeds, up to the given maximum amount of retries,
  20. // sleeping for the given duration in between attempts.
  21. func Retry(maxRetries int, delay time.Duration, f func() error) error {
  22. tries := 0
  23. for {
  24. err := f()
  25. if err == nil {
  26. // function succeeded, all done
  27. return nil
  28. }
  29. tries++
  30. if tries > maxRetries {
  31. return fmt.Errorf("max retries exceeded, last err: %v", err)
  32. }
  33. logger.Infof("retrying after %v, last error: %v", delay, err)
  34. <-time.After(delay)
  35. }
  36. }
  37. // RetryFunc is a function that returns true when it is done and it should be retried no longer.
  38. // It should return error if there has been an error. The error will be logged if done==false
  39. // (should keep retrying). The error will be returned by the calling function if done==true (should
  40. // stop retrying).
  41. type RetryFunc func() (done bool, err error)
  42. // RetryWithTimeout retries the RetryFunc until the timeout occurs. It will retry once more after
  43. // the timeout to avoid race conditions at the expense of running for slightly longer than timeout
  44. // in the timeout/error case.
  45. // The given description will be output in log/error messages as "... waiting for <description>..."
  46. func RetryWithTimeout(f RetryFunc, period time.Duration, timeout time.Duration, description string) error {
  47. tt := time.After(timeout)
  48. var done bool
  49. var err error
  50. for {
  51. done, err = f()
  52. if done {
  53. break
  54. }
  55. if err != nil {
  56. logger.Errorf("error occurred waiting for %s. retrying after %v. %v", description, period, err)
  57. }
  58. logger.Debugf("waiting for %s. retrying after %v", description, period)
  59. select {
  60. case <-time.After(period):
  61. // go back to start of loop
  62. case <-tt:
  63. // timeout; try one last time
  64. done, err = f()
  65. if !done {
  66. if err != nil {
  67. return errors.Wrapf(err, "timed out waiting for %s", description)
  68. }
  69. return fmt.Errorf("timed out waiting for %s", description)
  70. }
  71. return err
  72. }
  73. }
  74. return err
  75. }