Skip to content

Commit

Permalink
Retry on 404 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Jan 25, 2025
1 parent 2b67263 commit 34c304a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 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.1 (unreleased)

- Retry on 404 responses for POST and PUT requests

## 0.1.0

- Initial release
59 changes: 31 additions & 28 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,36 +279,39 @@ func (client *Client) Do(req Req) (Res, error) {
if httpRes.StatusCode >= 200 && httpRes.StatusCode <= 299 {
log.Printf("[DEBUG] Exit from Do method")
break
}

if ok := client.Backoff(attempts); !ok {
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 >= 500 && httpRes.StatusCode <= 599 {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
} else if httpRes.StatusCode == 404 && (httpRes.Request.Method == "POST" || httpRes.Request.Method == "PUT") {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
} else {
if ok := client.Backoff(attempts); !ok {
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 >= 500 && httpRes.StatusCode <= 599 {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
if res.Get("errors").Exists() && len(res.Get("errors").Array()) > 0 {
log.Printf("[ERROR] JSON error: %s", res.Get("errors").String())
return res, fmt.Errorf("HTTP Request failed: StatusCode %v, JSON error: %s", httpRes.StatusCode, res.Get("errors").String())
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
if res.Get("errors").Exists() && len(res.Get("errors").Array()) > 0 {
log.Printf("[ERROR] JSON error: %s", res.Get("errors").String())
return res, fmt.Errorf("HTTP Request failed: StatusCode %v, JSON error: %s", httpRes.StatusCode, res.Get("errors").String())
} else {
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
}
return res, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
}
}
}
Expand Down

0 comments on commit 34c304a

Please sign in to comment.