From b84298351a28f6dfc8707cde2734cb50e8862ab6 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 15 Jun 2016 15:29:47 +1000 Subject: [PATCH] Move a few methods to Torrent, and track how many DHT announces occur I suspect that DHT announces may occur too regularly, or without a break for torrents that aren't able to get over the lower water pending peer mark. --- client.go | 11 +---------- t.go | 2 +- torrent.go | 25 ++++++++++++++++++++++--- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/client.go b/client.go index fa63e446..8c388280 100644 --- a/client.go +++ b/client.go @@ -1287,7 +1287,7 @@ func (cl *Client) connectionLoop(t *Torrent, c *connection) error { } 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), @@ -1430,15 +1430,6 @@ func (cl *Client) badPeerIPPort(ip net.IP, port int) bool { 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 // initialize fields all fields that don't require the Client without locking // it. diff --git a/t.go b/t.go index 854e0d56..e75009ec 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 59dc38e7..ac70a145 100644 --- a/torrent.go +++ b/torrent.go @@ -71,9 +71,11 @@ type Torrent struct { // 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 @@ func (t *Torrent) worstConns(cl *Client) (wcs *worstConns) { 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 @@ func (t *Torrent) writeStatus(w io.Writer, cl *Client) { 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 @@ func (t *Torrent) announceDHT(impliedPort bool) { 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 @@ func (t *Torrent) announceDHT(impliedPort bool) { 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 @@ func (t *Torrent) announceDHT(impliedPort bool) { // 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) + } +} -- 2.48.1