]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Rework HTTP announce timeouts
authorMatt Joiner <anacrolix@gmail.com>
Thu, 1 Oct 2020 00:45:05 +0000 (10:45 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 1 Oct 2020 00:45:05 +0000 (10:45 +1000)
Use Request.Context to implement timeouts, set the default to 3s for announces from the Client.

tracker/http.go
tracker/tracker.go
tracker_scraper.go

index 82a23dc1f7cf04d7949ca8f5cc799f3c60f73b20..328c08322ba8e6cb1ac5339fce3d01f0e9eb6533 100644 (file)
@@ -113,17 +113,18 @@ func announceHTTP(opt Announce, _url *url.URL) (ret AnnounceResponse, err error)
                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)
index 93f11ccd8d589ad14fee5e698bed3d4df938fd08..5497037f5acb07d79302b867269875c419fa1775 100644 (file)
@@ -5,6 +5,7 @@ import (
        "errors"
        "net/http"
        "net/url"
+       "time"
 
        "github.com/anacrolix/dht/v2/krpc"
 )
@@ -70,6 +71,13 @@ func (me Announce) Do() (res AnnounceResponse, err error) {
        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":
                return announceHTTP(me, _url)
index b7d38c5aab993c287b3c6eae071594d6409cb8cb..62ca426167eb74ec29556294419c843b7ff51a3e 100644 (file)
@@ -2,6 +2,7 @@ package torrent
 
 import (
        "bytes"
+       "context"
        "errors"
        "fmt"
        "net"
@@ -119,8 +120,11 @@ func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnno
        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),