|
| 1 | +package wrappers |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/pkg/errors" |
| 11 | +) |
| 12 | + |
| 13 | +const defaultRateLimitWaitSeconds = 60 |
| 14 | + |
| 15 | +// SCMRateLimitConfig holds rate limit configuration for different SCM providers |
| 16 | +type SCMRateLimitConfig struct { |
| 17 | + Provider string |
| 18 | + ResetHeaderName string |
| 19 | + RemainingHeaderName string |
| 20 | + LimitHeaderName string |
| 21 | + RateLimitStatusCodes []int |
| 22 | + DefaultWaitTime time.Duration |
| 23 | +} |
| 24 | + |
| 25 | +// Common SCM rate limit configurations |
| 26 | +var ( |
| 27 | + GitHubRateLimitConfig = &SCMRateLimitConfig{ |
| 28 | + Provider: "GitHub", |
| 29 | + ResetHeaderName: "X-RateLimit-Reset", |
| 30 | + RemainingHeaderName: "X-RateLimit-Remaining", |
| 31 | + LimitHeaderName: "X-RateLimit-Limit", |
| 32 | + RateLimitStatusCodes: []int{403, 429}, |
| 33 | + DefaultWaitTime: defaultRateLimitWaitSeconds * time.Second, |
| 34 | + } |
| 35 | + |
| 36 | + GitLabRateLimitConfig = &SCMRateLimitConfig{ |
| 37 | + Provider: "GitLab", |
| 38 | + ResetHeaderName: "RateLimit-Reset", |
| 39 | + RemainingHeaderName: "RateLimit-Remaining", |
| 40 | + LimitHeaderName: "RateLimit-Limit", |
| 41 | + RateLimitStatusCodes: []int{429}, |
| 42 | + DefaultWaitTime: defaultRateLimitWaitSeconds * time.Second, |
| 43 | + } |
| 44 | + |
| 45 | + BitbucketRateLimitConfig = &SCMRateLimitConfig{ |
| 46 | + Provider: "Bitbucket", |
| 47 | + ResetHeaderName: "X-RateLimit-Reset", |
| 48 | + RemainingHeaderName: "X-RateLimit-Remaining", |
| 49 | + LimitHeaderName: "X-RateLimit-Limit", |
| 50 | + RateLimitStatusCodes: []int{429}, |
| 51 | + DefaultWaitTime: defaultRateLimitWaitSeconds * time.Second, |
| 52 | + } |
| 53 | + |
| 54 | + AzureRateLimitConfig = &SCMRateLimitConfig{ |
| 55 | + Provider: "Azure", |
| 56 | + ResetHeaderName: "X-Ratelimit-Reset", |
| 57 | + RemainingHeaderName: "X-Ratelimit-Remaining", |
| 58 | + LimitHeaderName: "X-Ratelimit-Limit", |
| 59 | + RateLimitStatusCodes: []int{429}, |
| 60 | + DefaultWaitTime: defaultRateLimitWaitSeconds * time.Second, |
| 61 | + } |
| 62 | +) |
| 63 | + |
| 64 | +// SCMRateLimitError represents a rate limit error from any SCM provider |
| 65 | +type SCMRateLimitError struct { |
| 66 | + Provider string |
| 67 | + ResetTime int64 |
| 68 | + Message string |
| 69 | +} |
| 70 | + |
| 71 | +func (e *SCMRateLimitError) Error() string { |
| 72 | + if e.Message != "" { |
| 73 | + return e.Message |
| 74 | + } |
| 75 | + return e.Provider + " API rate limit exceeded" |
| 76 | +} |
| 77 | + |
| 78 | +func (e *SCMRateLimitError) RetryAfter() time.Duration { |
| 79 | + if e.ResetTime > 0 { |
| 80 | + reset := time.Unix(e.ResetTime, 0) |
| 81 | + now := time.Now() |
| 82 | + if reset.After(now) { |
| 83 | + return reset.Sub(now) + (defaultRateLimitWaitSeconds * time.Second) // add buffer for 60 seconds |
| 84 | + } |
| 85 | + } |
| 86 | + return defaultRateLimitWaitSeconds * time.Second |
| 87 | +} |
| 88 | + |
| 89 | +// WithSCMRateLimitRetry wraps any SCM API call with rate limit retry logic |
| 90 | +func WithSCMRateLimitRetry(config *SCMRateLimitConfig, apiCall func() (*http.Response, error)) (*http.Response, error) { |
| 91 | + maxRetries := 3 |
| 92 | + retryCount := 0 |
| 93 | + |
| 94 | + for { |
| 95 | + resp, err := apiCall() |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + // Check if it's a rate limit error |
| 101 | + if isRateLimitStatusCode(resp.StatusCode, config) { |
| 102 | + rateLimitErr := ParseRateLimitHeaders(resp.Header, config) |
| 103 | + wait := config.DefaultWaitTime |
| 104 | + if rateLimitErr != nil { |
| 105 | + wait = rateLimitErr.RetryAfter() |
| 106 | + } |
| 107 | + if retryCount >= maxRetries { |
| 108 | + return nil, errors.Errorf("%s API rate limit exceeded after %d retries", config.Provider, maxRetries) |
| 109 | + } |
| 110 | + log.Printf("%s API rate limit exceeded (status %d). Waiting %v until %v before retrying... (attempt %d/%d)", |
| 111 | + config.Provider, resp.StatusCode, wait, time.Now().Add(wait), retryCount+1, maxRetries) |
| 112 | + time.Sleep(wait) |
| 113 | + // Reset Authorization header before retry |
| 114 | + if resp.Request != nil { |
| 115 | + resetAuthorizationHeader(resp.Request) |
| 116 | + } |
| 117 | + retryCount++ |
| 118 | + continue |
| 119 | + } |
| 120 | + return resp, err |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ParseRateLimitHeaders extracts rate limit information from HTTP response headers |
| 125 | +func ParseRateLimitHeaders(headers map[string][]string, config *SCMRateLimitConfig) *SCMRateLimitError { |
| 126 | + resetHeader := getHeaderValue(headers, config.ResetHeaderName) |
| 127 | + if resetHeader == "" { |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + resetTime, err := strconv.ParseInt(resetHeader, 10, 64) |
| 132 | + if err != nil { |
| 133 | + return nil |
| 134 | + } |
| 135 | + |
| 136 | + return &SCMRateLimitError{ |
| 137 | + Provider: config.Provider, |
| 138 | + ResetTime: resetTime, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// getHeaderValue retrieves a header value in a case-insensitive manner |
| 143 | +func getHeaderValue(headers map[string][]string, headerName string) string { |
| 144 | + for name, values := range headers { |
| 145 | + if strings.EqualFold(name, headerName) && len(values) > 0 { |
| 146 | + return values[0] |
| 147 | + } |
| 148 | + } |
| 149 | + return "" |
| 150 | +} |
| 151 | + |
| 152 | +// isRateLimitStatusCode checks if the status code indicates a rate limit error |
| 153 | +func isRateLimitStatusCode(statusCode int, config *SCMRateLimitConfig) bool { |
| 154 | + for _, code := range config.RateLimitStatusCodes { |
| 155 | + if statusCode == code { |
| 156 | + return true |
| 157 | + } |
| 158 | + } |
| 159 | + return false |
| 160 | +} |
| 161 | + |
| 162 | +// resetAuthorizationHeader removes the Authorization header from the request |
| 163 | +func resetAuthorizationHeader(req *http.Request) { |
| 164 | + req.Header.Del("Authorization") |
| 165 | +} |
0 commit comments