|
1 | 1 | package apiclient
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "math/rand" |
| 4 | + "fmt" |
5 | 5 | "net/http"
|
6 | 6 | "slices"
|
7 | 7 | "time"
|
8 | 8 |
|
| 9 | + "github.com/cenkalti/backoff/v5" |
9 | 10 | log "github.com/sirupsen/logrus"
|
10 | 11 |
|
11 | 12 | "github.com/crowdsecurity/crowdsec/pkg/fflag"
|
12 | 13 | )
|
13 | 14 |
|
14 | 15 | type retryRoundTripper struct {
|
15 | 16 | next http.RoundTripper
|
16 |
| - maxAttempts int |
| 17 | + maxAttempts uint |
17 | 18 | retryStatusCodes []int
|
18 | 19 | withBackOff bool
|
19 |
| - onBeforeRequest func(attempt int) |
20 | 20 | }
|
21 | 21 |
|
22 |
| -func (r retryRoundTripper) ShouldRetry(statusCode int) bool { |
| 22 | +func (r retryRoundTripper) shouldRetry(statusCode int) bool { |
23 | 23 | return slices.Contains(r.retryStatusCodes, statusCode)
|
24 | 24 | }
|
25 | 25 |
|
26 | 26 | func (r retryRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
27 |
| - var ( |
28 |
| - resp *http.Response |
29 |
| - err error |
30 |
| - ) |
31 |
| - |
32 |
| - backoff := 0 |
33 |
| - maxAttempts := r.maxAttempts |
34 |
| - |
| 27 | + maxAttempts := max(r.maxAttempts, 1) |
35 | 28 | if fflag.DisableHttpRetryBackoff.IsEnabled() {
|
36 | 29 | maxAttempts = 1
|
37 | 30 | }
|
38 | 31 |
|
39 |
| - for i := range maxAttempts { |
40 |
| - if i > 0 { |
41 |
| - if r.withBackOff { |
42 |
| - //nolint:gosec |
43 |
| - backoff += 10 + rand.Intn(20) |
44 |
| - } |
45 |
| - |
46 |
| - log.Infof("retrying in %d seconds (attempt %d of %d)", backoff, i+1, r.maxAttempts) |
47 |
| - |
48 |
| - select { |
49 |
| - case <-req.Context().Done(): |
50 |
| - return nil, req.Context().Err() |
51 |
| - case <-time.After(time.Duration(backoff) * time.Second): |
52 |
| - } |
53 |
| - } |
| 32 | + var bo backoff.BackOff |
| 33 | + |
| 34 | + if r.withBackOff { |
| 35 | + // Use exponential + jitter; the default values are: |
| 36 | + // |
| 37 | + // DefaultInitialInterval = 500 * time.Millisecond |
| 38 | + // DefaultRandomizationFactor = 0.5 |
| 39 | + // DefaultMultiplier = 1.5 |
| 40 | + // DefaultMaxInterval = 60 * time.Second |
| 41 | + // MaxElapsedTime = 15 * time.Minute |
| 42 | + exp := backoff.NewExponentialBackOff() |
| 43 | + exp.InitialInterval = 20 * time.Second |
| 44 | + exp.Multiplier = 2 |
| 45 | + bo = exp |
| 46 | + } else { |
| 47 | + // backoff is disabled, policy of "no wait" |
| 48 | + bo = backoff.NewConstantBackOff(0) |
| 49 | + } |
54 | 50 |
|
55 |
| - if r.onBeforeRequest != nil { |
56 |
| - r.onBeforeRequest(i) |
57 |
| - } |
| 51 | + attemptLeft := maxAttempts |
58 | 52 |
|
| 53 | + operation := func() (*http.Response, error) { |
59 | 54 | clonedReq := cloneRequest(req)
|
60 | 55 |
|
61 |
| - resp, err = r.next.RoundTrip(clonedReq) |
| 56 | + attemptLeft-- |
| 57 | + |
| 58 | + resp, err := r.next.RoundTrip(clonedReq) |
62 | 59 | if err != nil {
|
63 |
| - if left := maxAttempts - i - 1; left > 0 { |
64 |
| - log.Errorf("error while performing request: %s; %d retries left", err, left) |
| 60 | + if attemptLeft > 0 { |
| 61 | + log.Errorf("while performing request: %s; %d retries left", err, attemptLeft) |
65 | 62 | }
|
66 | 63 |
|
67 |
| - continue |
| 64 | + return nil, fmt.Errorf("retryable error: %w", err) |
68 | 65 | }
|
69 | 66 |
|
70 |
| - if !r.ShouldRetry(resp.StatusCode) { |
71 |
| - return resp, nil |
| 67 | + if r.shouldRetry(resp.StatusCode) { |
| 68 | + log.Errorf("request returned status %d: %s; %d retries left", resp.StatusCode, resp.Status, attemptLeft) |
| 69 | + return nil, fmt.Errorf("retryable status: %d", resp.StatusCode) |
72 | 70 | }
|
| 71 | + |
| 72 | + return resp, nil |
| 73 | + } |
| 74 | + |
| 75 | + resp, err := backoff.Retry(req.Context(), operation, |
| 76 | + backoff.WithBackOff(bo), |
| 77 | + backoff.WithMaxTries(maxAttempts), |
| 78 | + ) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
73 | 81 | }
|
74 | 82 |
|
75 |
| - return resp, err |
| 83 | + return resp, nil |
76 | 84 | }
|
0 commit comments