Retry on download requests

This commit is contained in:
Ben Adrian Sarmiento
2024-08-26 20:19:12 +02:00
parent d4adc2ae21
commit bac6351071

View File

@@ -263,6 +263,23 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err
return false
}
// retry on timeout errors for download requests
if err != nil && strings.Contains(err.Error(), "timeout") && strings.Contains(req.Host, ".download.real-debrid.") {
oldHost := req.Host
// remove old host from the list of reachable hosts
for i, host := range r.hosts {
if host == oldHost {
r.hosts = append(r.hosts[:i], r.hosts[i+1:]...)
break
}
}
// pick a new host from the list
req.Host = r.hosts[rand.Intn(len(r.hosts))]
req.URL.Host = req.Host
r.log.Debugf("Download timed out, attempt #%d, retrying with a new host (%s -> %s)", attempts+1, oldHost, req.URL.Host)
return true
}
if apiErr, ok := err.(*ApiErrorResponse); ok {
switch apiErr.Code {
case 5: // Slow down (retry infinitely)
@@ -314,7 +331,7 @@ func (r *HTTPClient) shouldRetry(req *http.Request, resp *http.Response, err err
}
if attempts >= r.maxRetries {
r.log.Debugf("Request failed, attempt #%d (error=%v)", attempts+1, err)
r.log.Errorf("Request failed, attempt #%d (error=%v), giving up", attempts+1, err)
return false
}
secs := r.backoff(attempts, 1)