client.go | 11 +---------- t.go | 2 +- torrent.go | 25 ++++++++++++++++++++++--- diff --git a/client.go b/client.go index fa63e446d494e92735254af68a7896aa94867b60..8c388280576f9c983591e093d81e14d2099576a4 100644 --- a/client.go +++ b/client.go @@ -1287,7 +1287,7 @@ break } go func() { cl.mu.Lock() - cl.addPeers(t, func() (ret []Peer) { + t.addPeers(func() (ret []Peer) { for i, cp := range pexMsg.Added { p := Peer{ IP: make([]byte, 4), @@ -1428,15 +1428,6 @@ if _, ok := cl.badPeerIPs[ip.String()]; ok { return true } return false -} - -func (cl *Client) addPeers(t *Torrent, peers []Peer) { - for _, p := range peers { - if cl.badPeerIPPort(p.IP, p.Port) { - continue - } - t.addPeer(p, cl) - } } // Prepare a Torrent without any attachment to a Client. That means we can diff --git a/t.go b/t.go index 854e0d563c0fe852e13c695b4b29234c4cf44698..e75009eca49e3c0d0a52ec9da35c5bc7a89e0c54 100644 --- a/t.go +++ b/t.go @@ -179,7 +179,7 @@ func (t *Torrent) AddPeers(pp []Peer) { cl := t.cl cl.mu.Lock() defer cl.mu.Unlock() - cl.addPeers(t, pp) + t.addPeers(pp) } // Marks the entire torrent for download. Requires the info first, see diff --git a/torrent.go b/torrent.go index 59dc38e72dfb7d7b0f04fadac618fc1ef1f42018..ac70a1458a63b15bc776bcba559c3d8b158b1a0f 100644 --- a/torrent.go +++ b/torrent.go @@ -71,9 +71,11 @@ // active connections if were told about the peer after connecting with // them. That encourages us to reconnect to peers that are well known. peers map[peersKey]Peer wantPeersEvent missinggo.Event - // An announcer for each tracker URL. trackerAnnouncers map[string]*trackerScraper + // How many times we've initiated a DHT announce. + numDHTAnnounces int + // Name used if the info name isn't available. displayName string // The bencoded bytes of the info dict. @@ -141,7 +143,8 @@ } return } -func (t *Torrent) addPeer(p Peer, cl *Client) { +func (t *Torrent) addPeer(p Peer) { + cl := t.cl cl.openNewConns(t) if len(t.peers) >= torrentPeersHighWater { return @@ -426,11 +429,15 @@ fmt.Fprintf(w, " %d:%d", begin, end) return true }) fmt.Fprintln(w) + fmt.Fprintf(w, "Trackers: ") for _url := range t.trackerAnnouncers { fmt.Fprintf(w, "%q ", _url) } fmt.Fprintf(w, "\n") + + fmt.Fprintf(w, "DHT Announces: %d\n", t.numDHTAnnounces) + fmt.Fprintf(w, "Pending peers: %d\n", len(t.peers)) fmt.Fprintf(w, "Half open: %d\n", len(t.halfOpen)) fmt.Fprintf(w, "Active peers: %d\n", len(t.conns)) @@ -1206,6 +1213,9 @@ if err != nil { log.Printf("error getting peers from dht: %s", err) return } + cl.mu.Lock() + t.numDHTAnnounces++ + cl.mu.Unlock() // Count all the unique addresses we got during this announce. allAddrs := make(map[string]struct{}) getPeers: @@ -1233,7 +1243,7 @@ }).String() allAddrs[key] = struct{}{} } cl.mu.Lock() - cl.addPeers(t, addPeers) + t.addPeers(addPeers) numPeers := len(t.peers) cl.mu.Unlock() if numPeers >= torrentPeersHighWater { @@ -1248,3 +1258,12 @@ ps.Close() // log.Printf("finished DHT peer scrape for %s: %d peers", t, len(allAddrs)) } } + +func (t *Torrent) addPeers(peers []Peer) { + for _, p := range peers { + if t.cl.badPeerIPPort(p.IP, p.Port) { + continue + } + t.addPeer(p) + } +}