tracker/http.go | 13 +++++++------ tracker/tracker.go | 8 ++++++++ tracker_scraper.go | 4 ++++ diff --git a/tracker/http.go b/tracker/http.go index 82a23dc1f7cf04d7949ca8f5cc799f3c60f73b20..328c08322ba8e6cb1ac5339fce3d01f0e9eb6533 100644 --- a/tracker/http.go +++ b/tracker/http.go @@ -113,17 +113,18 @@ if opt.Context != nil { req = req.WithContext(opt.Context) } resp, err := (&http.Client{ - Timeout: time.Second * 15, + //Timeout: time.Second * 15, Transport: &http.Transport{ - Dial: (&net.Dialer{ - Timeout: 15 * time.Second, - }).Dial, - Proxy: opt.HTTPProxy, - TLSHandshakeTimeout: 15 * time.Second, + //Dial: (&net.Dialer{ + // Timeout: 15 * time.Second, + //}).Dial, + Proxy: opt.HTTPProxy, + //TLSHandshakeTimeout: 15 * time.Second, TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, ServerName: opt.ServerName, }, + // This is for S3 trackers that hold connections open. DisableKeepAlives: true, }, }).Do(req) diff --git a/tracker/tracker.go b/tracker/tracker.go index 93f11ccd8d589ad14fee5e698bed3d4df938fd08..5497037f5acb07d79302b867269875c419fa1775 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -5,6 +5,7 @@ "context" "errors" "net/http" "net/url" + "time" "github.com/anacrolix/dht/v2/krpc" ) @@ -69,6 +70,13 @@ func (me Announce) Do() (res AnnounceResponse, err error) { _url, err := url.Parse(me.TrackerUrl) if err != nil { return + } + if me.Context == nil { + // This is just to maintain the old behaviour that should be a timeout of 15s. Users can + // override it by providing their own Context. + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + me.Context = ctx } switch _url.Scheme { case "http", "https": diff --git a/tracker_scraper.go b/tracker_scraper.go index b7d38c5aab993c287b3c6eae071594d6409cb8cb..62ca426167eb74ec29556294419c843b7ff51a3e 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -2,6 +2,7 @@ package torrent import ( "bytes" + "context" "errors" "fmt" "net" @@ -119,8 +120,11 @@ } me.t.cl.rLock() req := me.t.announceRequest(event) me.t.cl.rUnlock() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() me.t.logger.WithDefaultLevel(log.Debug).Printf("announcing to %q: %#v", me.u.String(), req) res, err := tracker.Announce{ + Context: ctx, HTTPProxy: me.t.cl.config.HTTPProxy, UserAgent: me.t.cl.config.HTTPUserAgent, TrackerUrl: me.trackerUrl(ip),