2
0

retry.go 1023 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package retry
  2. import (
  3. "context"
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. )
  8. // RetryCancellationErr is the error type that's returned if the retry is cancelled
  9. var RetryCancellationErr error = fmt.Errorf("RetryCancellationErr")
  10. // IsRetryCancelledError returns true if the error was a cancellation
  11. func IsRetryCancelledError(err error) bool {
  12. return err != nil && err.Error() == "RetryCancellationErr"
  13. }
  14. // Retry will run the f func until we receive a non error result up to the provided attempts or a cancellation.
  15. func Retry(ctx context.Context, f func() (interface{}, error), attempts uint, delay time.Duration) (interface{}, error) {
  16. var result interface{}
  17. var err error
  18. d := delay
  19. for r := attempts; r > 0; r-- {
  20. select {
  21. case <-ctx.Done():
  22. return nil, RetryCancellationErr
  23. default:
  24. }
  25. result, err = f()
  26. if err == nil {
  27. break
  28. }
  29. time.Sleep(d)
  30. jitter := time.Duration(rand.Int63n(int64(d))) // #nosec No need for a cryptographic strength random here
  31. d = d + jitter/2
  32. }
  33. return result, err
  34. }