Skip to content

Commit

Permalink
Handle 429 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed May 3, 2024
1 parent f793fb0 commit 680aeb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.5 (unreleased)

- Handle 429 responses including Retry-After header

## 0.1.4

- Retry on specific authentication failures
Expand Down
13 changes: 13 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ func (client *Client) Do(req Req) (Res, error) {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
} else if httpRes.StatusCode == 429 {
retryAfter := httpRes.Header.Get("Retry-After")
retryAfterDuration := time.Duration(0)
if retryAfter == "0" {
retryAfterDuration = time.Second
} else if retryAfter != "" {
retryAfterDuration, _ = time.ParseDuration(retryAfter + "s")
} else {
retryAfterDuration = 15 * time.Second
}
log.Printf("[WARNING] HTTP Request rate limited, waiting %v seconds, Retries: %v", retryAfterDuration.Seconds(), attempts)
time.Sleep(retryAfterDuration)
continue
} else if httpRes.StatusCode == 408 || (httpRes.StatusCode >= 500 && httpRes.StatusCode <= 599) {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
Expand Down

0 comments on commit 680aeb0

Please sign in to comment.