]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client.go
Use Option for cached Torrent length
[btrtrc.git] / client.go
index df746ecd3369644da89fa24b8f927c006ba50c2f..28dbc8ed4466853a9be6abcc76276beab6d1ba3e 100644 (file)
--- a/client.go
+++ b/client.go
@@ -19,25 +19,24 @@ import (
        "strings"
        "time"
 
+       "github.com/davecgh/go-spew/spew"
+       "github.com/dustin/go-humanize"
+       gbtree "github.com/google/btree"
+       "github.com/pion/datachannel"
+       "golang.org/x/time/rate"
+
+       "github.com/anacrolix/chansync"
        "github.com/anacrolix/chansync/events"
        "github.com/anacrolix/dht/v2"
        "github.com/anacrolix/dht/v2/krpc"
        "github.com/anacrolix/generics"
+       . "github.com/anacrolix/generics"
        "github.com/anacrolix/log"
        "github.com/anacrolix/missinggo/perf"
        "github.com/anacrolix/missinggo/v2"
        "github.com/anacrolix/missinggo/v2/bitmap"
        "github.com/anacrolix/missinggo/v2/pproffd"
        "github.com/anacrolix/sync"
-       request_strategy "github.com/anacrolix/torrent/request-strategy"
-       "github.com/davecgh/go-spew/spew"
-       "github.com/dustin/go-humanize"
-       "github.com/google/btree"
-       "github.com/pion/datachannel"
-       "golang.org/x/time/rate"
-
-       "github.com/anacrolix/chansync"
-       . "github.com/anacrolix/generics"
 
        "github.com/anacrolix/torrent/bencode"
        "github.com/anacrolix/torrent/internal/limiter"
@@ -45,6 +44,7 @@ import (
        "github.com/anacrolix/torrent/metainfo"
        "github.com/anacrolix/torrent/mse"
        pp "github.com/anacrolix/torrent/peer_protocol"
+       request_strategy "github.com/anacrolix/torrent/request-strategy"
        "github.com/anacrolix/torrent/storage"
        "github.com/anacrolix/torrent/tracker"
        "github.com/anacrolix/torrent/webtorrent"
@@ -166,8 +166,8 @@ func (cl *Client) WriteStatus(_w io.Writer) {
                                w,
                                "%f%% of %d bytes (%s)",
                                100*(1-float64(t.bytesMissingLocked())/float64(t.info.TotalLength())),
-                               *t.length,
-                               humanize.Bytes(uint64(*t.length)))
+                               t.length(),
+                               humanize.Bytes(uint64(t.length())))
                } else {
                        w.WriteString("<missing metainfo>")
                }
@@ -772,7 +772,7 @@ func (cl *Client) outgoingConnection(t *Torrent, addr PeerRemoteAddr, ps PeerSou
        cl.noLongerHalfOpen(t, addr.String())
        if err != nil {
                if cl.config.Debug {
-                       cl.logger.Printf("error establishing outgoing connection to %v: %v", addr, err)
+                       cl.logger.Levelf(log.Debug, "error establishing outgoing connection to %v: %v", addr, err)
                }
                return
        }
@@ -991,7 +991,9 @@ func (p *Peer) initUpdateRequestsTimer() {
                        panic(p.updateRequestsTimer)
                }
        }
-       p.updateRequestsTimer = time.AfterFunc(math.MaxInt64, p.updateRequestsTimerFunc)
+       if enableUpdateRequestsTimer {
+               p.updateRequestsTimer = time.AfterFunc(math.MaxInt64, p.updateRequestsTimerFunc)
+       }
 }
 
 const peerUpdateRequestsTimerReason = "updateRequestsTimer"
@@ -1136,8 +1138,9 @@ func (cl *Client) badPeerAddr(addr PeerRemoteAddr) bool {
        return false
 }
 
+// Returns whether the IP address and port are considered "bad".
 func (cl *Client) badPeerIPPort(ip net.IP, port int) bool {
-       if port == 0 {
+       if port == 0 || ip == nil {
                return true
        }
        if cl.dopplegangerAddr(net.JoinHostPort(ip.String(), strconv.FormatInt(int64(port), 10))) {
@@ -1176,7 +1179,7 @@ func (cl *Client) newTorrentOpt(opts AddTorrentOpts) (t *Torrent) {
                cl:       cl,
                infoHash: opts.InfoHash,
                peers: prioritizedPeers{
-                       om: btree.New(32),
+                       om: gbtree.New(32),
                        getPrio: func(p PeerInfo) peerPriority {
                                ipPort := p.addr()
                                return bep40PriorityIgnoreError(cl.publicAddr(ipPort.IP), ipPort)
@@ -1326,7 +1329,7 @@ func (t *Torrent) MergeSpec(spec *TorrentSpec) error {
        defer cl.unlock()
        t.initialPieceCheckDisabled = spec.DisableInitialPieceCheck
        for _, url := range spec.Webseeds {
-               t.addWebSeed(url, spec.DefaultWebseedEscapePath)
+               t.addWebSeed(url)
        }
        for _, peerAddr := range spec.PeerAddrs {
                t.addPeer(PeerInfo{
@@ -1483,6 +1486,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr PeerRemot
                connString: connString,
                conn:       nc,
        }
+       c.initRequestState()
        // TODO: Need to be much more explicit about this, including allowing non-IP bannable addresses.
        if remoteAddr != nil {
                netipAddrPort, err := netip.ParseAddrPort(remoteAddr.String())