its the fastest ever

This commit is contained in:
Ben Sarmiento
2024-01-28 02:23:19 +01:00
parent abdc8fcbb0
commit f07b65d5da
7 changed files with 70 additions and 66 deletions

View File

@@ -144,20 +144,16 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
var resp *http.Response
var err error
attempt := 0
for {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
r.replaceHostIfNeeded(req) // needed for ipv6
if !strings.Contains(req.URL.Host, "api.real-debrid.com") {
r.log.Debugf("downloading %s", req.URL)
}
resp, err = r.client.Do(req)
// check if error is context deadline exceeded
if r.ensureIPv6Host && r.cfg.ShouldForceIPv6() && err != nil && strings.Contains(err.Error(), "context deadline exceeded") {
attempt += 1
if attempt > r.maxRetries {
break
}
continue
}
if resp != nil && resp.StatusCode/100 >= 4 {
body, _ := io.ReadAll(resp.Body)
if body != nil {
@@ -169,7 +165,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
}
}
}
incr := r.shouldRetry(resp, reqHasRangeHeader, err, r.cfg.GetRateLimitSleepSeconds())
incr := r.shouldRetry(resp, reqHasRangeHeader, err, r.cfg.GetRateLimitSleepSecs())
if incr > 0 {
attempt += incr
if attempt > r.maxRetries {
@@ -255,6 +251,9 @@ func (r *HTTPClient) shouldRetry(resp *http.Response, reqHasRangeHeader bool, er
}
}
}
if err != nil && strings.Contains(err.Error(), "timeout awaiting response headers") {
return 1
}
if resp != nil {
if resp.StatusCode == 429 {
time.Sleep(time.Duration(rateLimitSleep) * time.Second)
@@ -279,7 +278,7 @@ func backoffFunc(attempt int) time.Duration {
}
func (r *HTTPClient) CanFetchFirstByte(url string) bool {
timeout := time.Duration(r.cfg.GetRealDebridTimeout()) * time.Second
timeout := time.Duration(r.timeoutSecs) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequest("GET", url, nil)