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:
yumosx 2026-02-25 17:37:06 +08:00
parent c20f20c92c
commit 43fc99ba83

View file

@ -31,14 +31,30 @@ type BuiltinSkill struct {
const maxRetries = 3 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) { func doRequestWithRetry(client *http.Client, req *http.Request) (*http.Response, error) {
var resp *http.Response var resp *http.Response
var err error var err error
for i := range maxRetries { for i := range maxRetries {
resp, err = client.Do(req) if i > 0 && resp != nil {
if err == nil && resp.StatusCode == http.StatusOK { resp.Body.Close()
break
} }
resp, err = client.Do(req)
if err == nil {
if resp.StatusCode == http.StatusOK {
break
}
if !shouldRetry(resp.StatusCode) {
break
}
}
if i < maxRetries-1 { if i < maxRetries-1 {
time.Sleep(time.Second * time.Duration(i+1)) time.Sleep(time.Second * time.Duration(i+1))
} }