Use status code constants
This commit is contained in:
@@ -181,7 +181,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
|
||||
resp, err = r.client.Do(req)
|
||||
|
||||
// http 4xx and 5xx errors
|
||||
if resp != nil && resp.StatusCode/100 >= 4 {
|
||||
if resp != nil && resp.StatusCode >= http.StatusBadRequest {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if body != nil {
|
||||
var errResp ApiErrorResponse
|
||||
@@ -285,22 +285,23 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err
|
||||
return 1
|
||||
}
|
||||
if resp != nil {
|
||||
if resp.StatusCode == 429 {
|
||||
if resp.StatusCode == http.StatusTooManyRequests {
|
||||
// Too many requests: retry infinitely, default: 4 secs
|
||||
time.Sleep(time.Duration(rateLimitSleep) * time.Second)
|
||||
return 0
|
||||
}
|
||||
if resp.StatusCode/100 == 4 {
|
||||
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode < http.StatusInternalServerError {
|
||||
// other client errors: retry
|
||||
return 1
|
||||
}
|
||||
if resp.StatusCode/100 == 5 {
|
||||
if resp.StatusCode >= http.StatusInternalServerError {
|
||||
// server errors: don't retry
|
||||
return -1
|
||||
}
|
||||
okResponseCode := resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusPartialContent
|
||||
// if the request has a Range header but the server doesn't respond with a Content-Range header
|
||||
hasRangeHeader := req.Header.Get("Range") != "" && !strings.HasPrefix(req.Header.Get("Range"), "bytes=0-")
|
||||
if hasRangeHeader && resp.StatusCode/100 == 2 && resp.Header.Get("Content-Range") == "" {
|
||||
if hasRangeHeader && okResponseCode && resp.Header.Get("Content-Range") == "" {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
return 0
|
||||
}
|
||||
@@ -331,7 +332,7 @@ func (r *HTTPClient) CanFetchFirstByte(url string) bool {
|
||||
resp, _ := r.Do(req)
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode == 200 || resp.StatusCode == 206
|
||||
return resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusPartialContent
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user