From 2502e5473fec6695c65ddf30862ef4a1b23d690f Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 27 Jan 2024 23:53:22 +0100 Subject: [PATCH] thinking about timeout --- internal/app.go | 2 +- internal/universal/downloader.go | 15 ++++++++++++--- pkg/http/client.go | 14 ++++---------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/internal/app.go b/internal/app.go index 27c377c..f18f14d 100644 --- a/internal/app.go +++ b/internal/app.go @@ -72,7 +72,7 @@ func MainApp(configPath string) { torrentMgr := torrent.NewTorrentManager(config, rd, workerPool, repairPool, log.Named("manager")) downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, true, config, log.Named("dlclient")) - downloader := universal.NewDownloader(downloadClient) + downloader := universal.NewDownloader(downloadClient, config.GetRealDebridTimeout()) router := chi.NewRouter() handlers.AttachHandlers(router, downloader, torrentMgr, config, rd, log.Named("router")) diff --git a/internal/universal/downloader.go b/internal/universal/downloader.go index 958af23..c186334 100644 --- a/internal/universal/downloader.go +++ b/internal/universal/downloader.go @@ -14,11 +14,15 @@ import ( ) type Downloader struct { - client *zurghttp.HTTPClient + client *zurghttp.HTTPClient + timeoutSecs int } -func NewDownloader(client *zurghttp.HTTPClient) *Downloader { - return &Downloader{client: client} +func NewDownloader(client *zurghttp.HTTPClient, timeoutSecs int) *Downloader { + return &Downloader{ + client: client, + timeoutSecs: timeoutSecs, + } } // DownloadFile handles a GET request for files in torrents @@ -162,7 +166,12 @@ func (dl *Downloader) streamFileToResponse(torrent *intTor.Torrent, file *intTor log.Debugf("Downloading unrestricted link %s (%s)%s", unrestrict.Download, unrestrict.Link, rangeLog) } + // timeout := time.Duration(dl.timeoutSecs) * time.Second + // ctx, cancel := context.WithTimeout(context.TODO(), timeout) + // dlReq = dlReq.WithContext(ctx) + download, err := dl.client.Do(dlReq) + if err != nil { log.Warnf("Cannot download file %s: %v", unrestrict.Download, err) if file != nil && unrestrict.Streamable == 1 { diff --git a/pkg/http/client.go b/pkg/http/client.go index a6a895b..be2fd65 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -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 {