retry.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package fx
  2. import (
  3. "context"
  4. "errors"
  5. "time"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. )
  8. const defaultRetryTimes = 3
  9. var errTimeout = errors.New("retry timeout")
  10. type (
  11. // RetryOption defines the method to customize DoWithRetry.
  12. RetryOption func(*retryOptions)
  13. retryOptions struct {
  14. times int
  15. interval time.Duration
  16. timeout time.Duration
  17. }
  18. )
  19. // DoWithRetry runs fn, and retries if failed. Default to retry 3 times.
  20. // Note that if the fn function accesses global variables outside the function
  21. // and performs modification operations, it is best to lock them,
  22. // otherwise there may be data race issues
  23. func DoWithRetry(fn func() error, opts ...RetryOption) error {
  24. return retry(func(errChan chan error, retryCount int) {
  25. errChan <- fn()
  26. }, opts...)
  27. }
  28. // DoWithRetryCtx runs fn, and retries if failed. Default to retry 3 times.
  29. // fn retryCount indicates the current number of retries, starting from 0
  30. // Note that if the fn function accesses global variables outside the function
  31. // and performs modification operations, it is best to lock them,
  32. // otherwise there may be data race issues
  33. func DoWithRetryCtx(ctx context.Context, fn func(ctx context.Context, retryCount int) error,
  34. opts ...RetryOption) error {
  35. return retry(func(errChan chan error, retryCount int) {
  36. errChan <- fn(ctx, retryCount)
  37. }, opts...)
  38. }
  39. func retry(fn func(errChan chan error, retryCount int), opts ...RetryOption) error {
  40. options := newRetryOptions()
  41. for _, opt := range opts {
  42. opt(options)
  43. }
  44. var berr errorx.BatchError
  45. var cancelFunc context.CancelFunc
  46. ctx := context.Background()
  47. if options.timeout > 0 {
  48. ctx, cancelFunc = context.WithTimeout(ctx, options.timeout)
  49. defer cancelFunc()
  50. }
  51. errChan := make(chan error, 1)
  52. for i := 0; i < options.times; i++ {
  53. go fn(errChan, i)
  54. select {
  55. case err := <-errChan:
  56. if err != nil {
  57. berr.Add(err)
  58. } else {
  59. return nil
  60. }
  61. case <-ctx.Done():
  62. berr.Add(errTimeout)
  63. return berr.Err()
  64. }
  65. if options.interval > 0 {
  66. select {
  67. case <-ctx.Done():
  68. berr.Add(errTimeout)
  69. return berr.Err()
  70. case <-time.After(options.interval):
  71. }
  72. }
  73. }
  74. return berr.Err()
  75. }
  76. // WithRetry customize a DoWithRetry call with given retry times.
  77. func WithRetry(times int) RetryOption {
  78. return func(options *retryOptions) {
  79. options.times = times
  80. }
  81. }
  82. func WithInterval(interval time.Duration) RetryOption {
  83. return func(options *retryOptions) {
  84. options.interval = interval
  85. }
  86. }
  87. func WithTimeout(timeout time.Duration) RetryOption {
  88. return func(options *retryOptions) {
  89. options.timeout = timeout
  90. }
  91. }
  92. func newRetryOptions() *retryOptions {
  93. return &retryOptions{
  94. times: defaultRetryTimes,
  95. }
  96. }