(effector Effector, retries int, delay time.Duration)
| 23 | ) |
| 24 | |
| 25 | func Retry(effector Effector, retries int, delay time.Duration) Effector { |
| 26 | return func(ctx context.Context) (string, error) { |
| 27 | for r := 0; ; r++ { |
| 28 | response, err := effector(ctx) |
| 29 | if err == nil || r >= retries { |
| 30 | return response, err |
| 31 | } |
| 32 | |
| 33 | log.Printf("Attempt %d failed; retrying in %v", r+1, delay) |
| 34 | |
| 35 | select { |
| 36 | case <-time.After(delay): |
| 37 | case <-ctx.Done(): |
| 38 | return "", ctx.Err() |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | } |