|
| 1 | +package retry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// Default Delay func options |
| 9 | +const ( |
| 10 | + DefaultAttempts uint8 = 10 |
| 11 | + DefaultDelay = time.Millisecond * 100 |
| 12 | + DefaultDelayFactor = 1 |
| 13 | +) |
| 14 | + |
| 15 | +type config struct { |
| 16 | + maxAttempts uint8 |
| 17 | + delay time.Duration |
| 18 | + delayFactor int |
| 19 | + stopRetryIf StopRetryIfFunc |
| 20 | + onRetry OnRetryFunc |
| 21 | +} |
| 22 | + |
| 23 | +// Option is a function type for |
| 24 | +// manipulating Retry configs |
| 25 | +type Option func(*config) |
| 26 | + |
| 27 | +// MaxAttempts is an option to set maximum attempts number |
| 28 | +// It could be a integer number between 0-255 |
| 29 | +// Default is 10 |
| 30 | +func MaxAttempts(attempts uint8) Option { |
| 31 | + return func(c *config) { |
| 32 | + c.maxAttempts = attempts |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Delay is an option to set minimum delay between each retries |
| 37 | +// Default is 100ms |
| 38 | +func Delay(delay time.Duration) Option { |
| 39 | + return func(c *config) { |
| 40 | + c.delay = delay |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// DelayFactor is an option to set increase of delay |
| 45 | +// duration on each retries |
| 46 | +// Actual delay calculated by: (delay * delayFactor * attemptNumber) |
| 47 | +// Default is 1 |
| 48 | +func DelayFactor(factor int) Option { |
| 49 | + return func(c *config) { |
| 50 | + c.delayFactor = factor |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// StopRetryIfFunc is a function type to set conditions |
| 55 | +// To stop continuing the retry mechanism |
| 56 | +type StopRetryIfFunc func(ctx context.Context, err error) bool |
| 57 | + |
| 58 | +// StopRetryIf is an option to set StopRetryIfFunc |
| 59 | +func StopRetryIf(fn StopRetryIfFunc) Option { |
| 60 | + return func(c *config) { |
| 61 | + c.stopRetryIf = fn |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// OnRetryFunc is a function type to set some |
| 66 | +// functionality to retry function |
| 67 | +// It stops retry mechanism when error was not nil |
| 68 | +type OnRetryFunc func(ctx context.Context, attempt int) error |
| 69 | + |
| 70 | +// OnRetry is an option to set OnRetryFunc |
| 71 | +func OnRetry(fn OnRetryFunc) Option { |
| 72 | + return func(c *config) { |
| 73 | + c.onRetry = fn |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// Retry tries to handle the operation func |
| 78 | +// It tries until maxAttempts exceeded |
| 79 | +// At the end of retries returns error from operation func |
| 80 | +func Retry(ctx context.Context, operation func() error, opts ...Option) error { |
| 81 | + cfg := &config{ |
| 82 | + maxAttempts: DefaultAttempts, |
| 83 | + delay: DefaultDelay, |
| 84 | + delayFactor: DefaultDelayFactor, |
| 85 | + } |
| 86 | + |
| 87 | + for _, opt := range opts { |
| 88 | + opt(cfg) |
| 89 | + } |
| 90 | + |
| 91 | + var err error |
| 92 | + for attempt := 0; attempt < int(cfg.maxAttempts); attempt++ { |
| 93 | + err = operation() |
| 94 | + if err == nil { |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + if cfg.stopRetryIf != nil { |
| 99 | + if cfg.stopRetryIf(ctx, err) { |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if cfg.onRetry != nil { |
| 105 | + if err := cfg.onRetry(ctx, attempt+1); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + select { |
| 111 | + case <-ctx.Done(): |
| 112 | + return err |
| 113 | + case <-time.After(cfg.delay * time.Duration(attempt*cfg.delayFactor)): |
| 114 | + continue |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return err |
| 119 | +} |
0 commit comments