]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move a few methods to Torrent, and track how many DHT announces occur
authorMatt Joiner <anacrolix@gmail.com>
Wed, 15 Jun 2016 05:29:47 +0000 (15:29 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 15 Jun 2016 05:29:47 +0000 (15:29 +1000)
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
t.go
torrent.go

index fa63e446d494e92735254af68a7896aa94867b60..8c388280576f9c983591e093d81e14d2099576a4 100644 (file)
--- 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 854e0d563c0fe852e13c695b4b29234c4cf44698..e75009eca49e3c0d0a52ec9da35c5bc7a89e0c54 100644 (file)
--- 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
index 59dc38e72dfb7d7b0f04fadac618fc1ef1f42018..ac70a1458a63b15bc776bcba559c3d8b158b1a0f 100644 (file)
@@ -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)
+       }
+}