Use Request.Context to implement timeouts, set the default to 3s for announces from the Client.
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)
"errors"
"net/http"
"net/url"
+ "time"
"github.com/anacrolix/dht/v2/krpc"
)
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)
import (
"bytes"
+ "context"
"errors"
"fmt"
"net"
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),