From 58c997210ef72b6e6f6bf2983f000ce59b851c0e Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 28 Sep 2015 15:30:13 +1000 Subject: [PATCH] Track peer sources with a dict, and don't exceed high water mark --- client.go | 10 ++-------- connection.go | 1 + torrent.go | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index f11e892b..898ee23a 100644 --- a/client.go +++ b/client.go @@ -48,9 +48,7 @@ var ( unexpectedChunksReceived = expvar.NewInt("chunksReceivedUnexpected") chunksReceived = expvar.NewInt("chunksReceived") - peersFoundByDHT = expvar.NewInt("peersFoundByDHT") - peersFoundByPEX = expvar.NewInt("peersFoundByPEX") - peersFoundByTracker = expvar.NewInt("peersFoundByTracker") + peersAddedBySource = expvar.NewMap("peersAddedBySource") uploadChunksPosted = expvar.NewInt("uploadChunksPosted") unexpectedCancels = expvar.NewInt("unexpectedCancels") @@ -1700,7 +1698,6 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error { return }()) me.mu.Unlock() - peersFoundByPEX.Add(int64(len(pexMsg.Added))) }() default: err = fmt.Errorf("unexpected extended message ID: %v", msg.ExtendedID) @@ -1883,9 +1880,8 @@ func (me *Client) addPeers(t *torrent, peers []Peer) { // The spec says to scrub these yourselves. Fine. continue } - t.addPeer(p) + t.addPeer(p, me) } - me.openNewConns(t) } func (cl *Client) cachedMetaInfoFilename(ih InfoHash) string { @@ -2331,7 +2327,6 @@ func (cl *Client) announceTorrentDHT(t *torrent, impliedPort bool) { }).String() allAddrs[key] = struct{}{} } - peersFoundByDHT.Add(int64(len(addPeers))) cl.mu.Lock() cl.addPeers(t, addPeers) numPeers := len(t.Peers) @@ -2399,7 +2394,6 @@ func (cl *Client) announceTorrentSingleTracker(tr tracker.Client, req *tracker.A cl.mu.Unlock() // log.Printf("%s: %d new peers from %s", t, len(peers), tr) - peersFoundByTracker.Add(int64(len(peers))) time.Sleep(time.Second * time.Duration(resp.Interval)) return nil diff --git a/connection.go b/connection.go index bedb1a9b..0edda653 100644 --- a/connection.go +++ b/connection.go @@ -23,6 +23,7 @@ var optimizedCancels = expvar.NewInt("optimizedCancels") type peerSource byte const ( + peerSourceTracker = '\x00' // It's the default. peerSourceIncoming = 'I' peerSourceDHT = 'H' peerSourcePEX = 'X' diff --git a/torrent.go b/torrent.go index 79a6a159..82d97a49 100644 --- a/torrent.go +++ b/torrent.go @@ -181,8 +181,19 @@ func (t *torrent) ceaseNetworking() { } } -func (t *torrent) addPeer(p Peer) { - t.Peers[peersKey{string(p.IP), p.Port}] = p +func (t *torrent) addPeer(p Peer, cl *Client) { + cl.openNewConns(t) + if len(t.Peers) >= torrentPeersHighWater { + return + } + key := peersKey{string(p.IP), p.Port} + if _, ok := t.Peers[key]; ok { + return + } + t.Peers[key] = p + peersAddedBySource.Add(string(p.Source), 1) + cl.openNewConns(t) + } func (t *torrent) invalidateMetadata() { -- 2.48.1