|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "fmt" |
| 7 | + "io" |
| 8 | + "math/rand/v2" |
6 | 9 | "net/http" |
| 10 | + "strconv" |
7 | 11 | "time" |
8 | 12 |
|
9 | 13 | objectstorage "github.com/stackitcloud/stackit-sdk-go/services/objectstorage/v2api" |
@@ -35,14 +39,124 @@ func EnableProject(ctx context.Context, projectId, region string, client objects |
35 | 39 | return nil |
36 | 40 | } |
37 | 41 |
|
| 42 | +// RetryTransport wraps an underlying RoundTripper to retry on HTTP 429 rate limit errors with jitter. |
| 43 | +type RetryTransport struct { |
| 44 | + Base http.RoundTripper |
| 45 | + MaxRetries int |
| 46 | + BaseBackoff time.Duration |
| 47 | + MaxJitter time.Duration |
| 48 | +} |
| 49 | + |
| 50 | +func (t *RetryTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 51 | + base := t.Base |
| 52 | + if base == nil { |
| 53 | + base = http.DefaultTransport |
| 54 | + } |
| 55 | + |
| 56 | + // Preserve request body for retries if present |
| 57 | + var bodyBytes []byte |
| 58 | + if req.Body != nil && req.Body != http.NoBody { |
| 59 | + var err error |
| 60 | + bodyBytes, err = io.ReadAll(req.Body) |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + err = req.Body.Close() |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + var resp *http.Response |
| 72 | + var err error |
| 73 | + |
| 74 | + for attempt := 0; attempt <= t.MaxRetries; attempt++ { |
| 75 | + // Re-hydrate the request body on each attempt |
| 76 | + if bodyBytes != nil { |
| 77 | + req.Body = io.NopCloser(bytes.NewReader(bodyBytes)) |
| 78 | + } |
| 79 | + |
| 80 | + resp, err = base.RoundTrip(req) |
| 81 | + |
| 82 | + // If success or non-429 error, return immediately |
| 83 | + if err != nil || resp.StatusCode != http.StatusTooManyRequests { |
| 84 | + return resp, err |
| 85 | + } |
| 86 | + |
| 87 | + // Stop if max retries reached |
| 88 | + if attempt == t.MaxRetries { |
| 89 | + break |
| 90 | + } |
| 91 | + |
| 92 | + // Calculate base sleep duration (value of Retry-After Header, if header isn't present falls back to exponential backoff) |
| 93 | + wait := t.getWaitDuration(resp, attempt) |
| 94 | + |
| 95 | + // Always add random jitter regardless of Retry-After header presence. Else all resource / datasource |
| 96 | + // goroutines would try again in parallel after exactly the same interval. |
| 97 | + jitter := time.Duration(rand.Int64N(int64(t.MaxJitter))) //nolint:gosec // only used for jitter |
| 98 | + totalWait := wait + jitter |
| 99 | + |
| 100 | + // Drain and close response body before retrying to reuse TCP connections |
| 101 | + _, err = io.Copy(io.Discard, resp.Body) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + err = resp.Body.Close() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + select { |
| 112 | + case <-req.Context().Done(): |
| 113 | + return nil, req.Context().Err() |
| 114 | + case <-time.After(totalWait): |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return resp, err |
| 119 | +} |
| 120 | + |
| 121 | +func (t *RetryTransport) getWaitDuration(resp *http.Response, attempt int) time.Duration { |
| 122 | + if retryAfter := resp.Header.Get("Retry-After"); retryAfter != "" { |
| 123 | + // Try parsing as integer seconds |
| 124 | + if seconds, err := strconv.Atoi(retryAfter); err == nil { |
| 125 | + return time.Duration(seconds) * time.Second |
| 126 | + } |
| 127 | + // Try parsing as HTTP-Date string |
| 128 | + if date, err := http.ParseTime(retryAfter); err == nil { |
| 129 | + if d := time.Until(date); d > 0 { |
| 130 | + return d |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Fallback to exponential backoff |
| 136 | + return t.BaseBackoff * (1 << attempt) |
| 137 | +} |
| 138 | + |
38 | 139 | func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags *diag.Diagnostics) *objectstorage.APIClient { |
| 140 | + // Add middleware to retry on HTTP 429 rate limits. |
| 141 | + // This solution is **not** intended to be copied to each and every service (!!). |
| 142 | + // This should be rolled out centrally instead, should be easily doable after |
| 143 | + // this refactoring: https://github.com/stackitcloud/terraform-provider-stackit/pull/1663 |
| 144 | + retryRoundTripper := &RetryTransport{ |
| 145 | + Base: providerData.RoundTripper, |
| 146 | + MaxRetries: 3, |
| 147 | + BaseBackoff: 1 * time.Second, |
| 148 | + MaxJitter: 500 * time.Millisecond, // Always added to wait time |
| 149 | + } |
| 150 | + |
39 | 151 | apiClientConfigOptions := []config.ConfigurationOption{ |
40 | | - config.WithCustomAuth(providerData.RoundTripper), |
| 152 | + config.WithCustomAuth(retryRoundTripper), |
41 | 153 | utils.UserAgentConfigOption(providerData.Version), |
42 | 154 | } |
| 155 | + |
43 | 156 | if providerData.ObjectStorageCustomEndpoint != "" { |
44 | 157 | apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.ObjectStorageCustomEndpoint)) |
45 | 158 | } |
| 159 | + |
46 | 160 | apiClient, err := objectstorage.NewAPIClient(apiClientConfigOptions...) |
47 | 161 | if err != nil { |
48 | 162 | core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err)) |
|
0 commit comments