retry.go 2.6 KB

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