thinking about timeout

This commit is contained in:
Ben Sarmiento
2024-01-27 23:53:22 +01:00
parent ce4b794098
commit 2502e5473f
3 changed files with 17 additions and 14 deletions

View File

@@ -53,7 +53,7 @@ func (e *ApiErrorResponse) Error() string {
func NewHTTPClient(token string, maxRetries int, timeoutSecs int, ensureIPv6Host bool, cfg config.ConfigInterface, log *logutil.Logger) *HTTPClient {
client := HTTPClient{
bearerToken: token,
client: &http.Client{},
client: http.DefaultClient,
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
@@ -146,11 +146,9 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
attempt := 0
for {
r.replaceHostIfNeeded(req) // needed for ipv6
r.log.Debugf("downloading %s", req.URL)
timeout := time.Duration(r.cfg.GetRealDebridTimeout()) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req = req.WithContext(ctx)
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") {
@@ -172,7 +170,6 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
}
}
incr := r.shouldRetry(resp, reqHasRangeHeader, err, r.cfg.GetRateLimitSleepSeconds())
r.log.Debugf("got %s incr %d/%d", req.URL, incr, attempt)
if incr > 0 {
attempt += incr
if attempt > r.maxRetries {
@@ -230,9 +227,6 @@ func (r *HTTPClient) proxyDialer(proxyURL *url.URL) (proxy.Dialer, error) {
}
func (r *HTTPClient) shouldRetry(resp *http.Response, reqHasRangeHeader bool, err error, rateLimitSleep int) int {
if err != nil {
r.log.Errorf("http error +%v", err)
}
if err != nil && strings.HasPrefix(err.Error(), "api response error:") {
if apiErr, ok := err.(*ApiErrorResponse); ok {
switch apiErr.Code {