Track peer sources with a dict, and don't exceed high water mark

This commit is contained in:
Matt Joiner 2015-09-28 15:30:13 +10:00
parent 53259397fc
commit 58c997210e
3 changed files with 16 additions and 10 deletions

View File

@ -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

View File

@ -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'

View File

@ -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() {