Skip to content

Commit

Permalink
Optimize 429 wait time
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Jan 9, 2024
1 parent b3df7cc commit 66798d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.3 (unreleased)

- Optimize wait time after 429 response

## 0.1.2

- Retry on 429 HTTP responses
Expand Down
15 changes: 14 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,20 @@ 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 == 408 || httpRes.StatusCode == 429 || (httpRes.StatusCode >= 502 && httpRes.StatusCode <= 504) {
} 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 >= 502 && httpRes.StatusCode <= 504) {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
} else {
Expand Down

0 comments on commit 66798d8

Please sign in to comment.