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

@@ -72,7 +72,7 @@ func MainApp(configPath string) {
torrentMgr := torrent.NewTorrentManager(config, rd, workerPool, repairPool, log.Named("manager")) torrentMgr := torrent.NewTorrentManager(config, rd, workerPool, repairPool, log.Named("manager"))
downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, true, config, log.Named("dlclient")) 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() router := chi.NewRouter()
handlers.AttachHandlers(router, downloader, torrentMgr, config, rd, log.Named("router")) handlers.AttachHandlers(router, downloader, torrentMgr, config, rd, log.Named("router"))

View File

@@ -15,10 +15,14 @@ import (
type Downloader struct { type Downloader struct {
client *zurghttp.HTTPClient client *zurghttp.HTTPClient
timeoutSecs int
} }
func NewDownloader(client *zurghttp.HTTPClient) *Downloader { func NewDownloader(client *zurghttp.HTTPClient, timeoutSecs int) *Downloader {
return &Downloader{client: client} return &Downloader{
client: client,
timeoutSecs: timeoutSecs,
}
} }
// DownloadFile handles a GET request for files in torrents // 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) 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) download, err := dl.client.Do(dlReq)
if err != nil { if err != nil {
log.Warnf("Cannot download file %s: %v", unrestrict.Download, err) log.Warnf("Cannot download file %s: %v", unrestrict.Download, err)
if file != nil && unrestrict.Streamable == 1 { if file != nil && unrestrict.Streamable == 1 {

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 { func NewHTTPClient(token string, maxRetries int, timeoutSecs int, ensureIPv6Host bool, cfg config.ConfigInterface, log *logutil.Logger) *HTTPClient {
client := HTTPClient{ client := HTTPClient{
bearerToken: token, bearerToken: token,
client: &http.Client{}, client: http.DefaultClient,
maxRetries: maxRetries, maxRetries: maxRetries,
timeoutSecs: timeoutSecs, timeoutSecs: timeoutSecs,
backoff: backoffFunc, backoff: backoffFunc,
@@ -146,11 +146,9 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
attempt := 0 attempt := 0
for { for {
r.replaceHostIfNeeded(req) // needed for ipv6 r.replaceHostIfNeeded(req) // needed for ipv6
if !strings.Contains(req.URL.Host, "api.real-debrid.com") {
r.log.Debugf("downloading %s", req.URL) 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)
resp, err = r.client.Do(req) resp, err = r.client.Do(req)
// check if error is context deadline exceeded // check if error is context deadline exceeded
if r.ensureIPv6Host && r.cfg.ShouldForceIPv6() && err != nil && strings.Contains(err.Error(), "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()) incr := r.shouldRetry(resp, reqHasRangeHeader, err, r.cfg.GetRateLimitSleepSeconds())
r.log.Debugf("got %s incr %d/%d", req.URL, incr, attempt)
if incr > 0 { if incr > 0 {
attempt += incr attempt += incr
if attempt > r.maxRetries { 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 { 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 err != nil && strings.HasPrefix(err.Error(), "api response error:") {
if apiErr, ok := err.(*ApiErrorResponse); ok { if apiErr, ok := err.(*ApiErrorResponse); ok {
switch apiErr.Code { switch apiErr.Code {