From: Matt Joiner Date: Sun, 20 Dec 2020 22:29:20 +0000 (+1100) Subject: Pass trackerScraper context to announces X-Git-Tag: v1.20.0~11 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=b67ccab7f2a687ddf75a31bc0206f6c11c58bd39;p=btrtrc.git Pass trackerScraper context to announces This will cancel announces for Closed Torrents and let them move more quickly to announcing the stopped event. Spotted by @hwh33. --- diff --git a/tracker/tracker.go b/tracker/tracker.go index 65cad803..7f2a5b50 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -66,6 +66,9 @@ type Announce struct { Context context.Context } +// The code *is* the documentation. +const DefaultTrackerAnnounceTimeout = 15 * time.Second + func (me Announce) Do() (res AnnounceResponse, err error) { _url, err := url.Parse(me.TrackerUrl) if err != nil { @@ -75,7 +78,7 @@ func (me Announce) Do() (res AnnounceResponse, err error) { // This is just to maintain the old behaviour that should be a timeout of 15s. Users can // override it by providing their own Context. See comments elsewhere about longer timeouts // acting as rate limiting overloaded trackers. - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), DefaultTrackerAnnounceTimeout) defer cancel() me.Context = ctx } diff --git a/tracker_scraper.go b/tracker_scraper.go index c18f21f8..21c4111b 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -136,13 +136,15 @@ func (me *trackerScraper) announce(ctx context.Context, event tracker.AnnounceEv me.t.cl.rLock() req := me.t.announceRequest(event) me.t.cl.rUnlock() - // The default timeout is currently 15s, and that works well as backpressure on concurrent - // access to the tracker. - //ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) - //defer cancel() + // The default timeout works well as backpressure on concurrent access to the tracker. Since + // we're passing our own Context now, we will include that timeout ourselves to maintain similar + // behavior to previously, albeit with this context now being cancelled when the Torrent is + // closed. + ctx, cancel := context.WithTimeout(ctx, tracker.DefaultTrackerAnnounceTimeout) + defer cancel() me.t.logger.WithDefaultLevel(log.Debug).Printf("announcing to %q: %#v", me.u.String(), req) res, err := tracker.Announce{ - //Context: ctx, + Context: ctx, HTTPProxy: me.t.cl.config.HTTPProxy, UserAgent: me.t.cl.config.HTTPUserAgent, TrackerUrl: me.trackerUrl(ip), @@ -243,7 +245,7 @@ func (me *trackerScraper) Run() { } func (me *trackerScraper) announceStopped() { - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), tracker.DefaultTrackerAnnounceTimeout) defer cancel() me.announce(ctx, tracker.Stopped) }