fix: improve http request retry logic with status code checks
Add shouldRetry helper function to determine retryable status codes. Close response body between retry attempts and break early for non-retryable status codes.
This commit is contained in:
parent
c20f20c92c
commit
43fc99ba83
1 changed files with 19 additions and 3 deletions
|
|
@ -31,14 +31,30 @@ type BuiltinSkill struct {
|
|||
|
||||
const maxRetries = 3
|
||||
|
||||
func shouldRetry(statusCode int) bool {
|
||||
return statusCode == http.StatusTooManyRequests ||
|
||||
statusCode >= 500
|
||||
}
|
||||
|
||||
func doRequestWithRetry(client *http.Client, req *http.Request) (*http.Response, error) {
|
||||
var resp *http.Response
|
||||
var err error
|
||||
|
||||
for i := range maxRetries {
|
||||
if i > 0 && resp != nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
|
||||
resp, err = client.Do(req)
|
||||
if err == nil && resp.StatusCode == http.StatusOK {
|
||||
if err == nil {
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
break
|
||||
}
|
||||
if !shouldRetry(resp.StatusCode) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if i < maxRetries-1 {
|
||||
time.Sleep(time.Second * time.Duration(i+1))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue