diff --git a/prioritized_peers.go b/prioritized_peers.go index faafe00f..0eddf161 100644 --- a/prioritized_peers.go +++ b/prioritized_peers.go @@ -1,6 +1,8 @@ package torrent import ( + "hash/maphash" + "github.com/anacrolix/multiless" "github.com/google/btree" ) @@ -12,11 +14,21 @@ type prioritizedPeersItem struct { p Peer } +var hashSeed = maphash.MakeSeed() + +func (me prioritizedPeersItem) addrHash() int64 { + var h maphash.Hash + h.SetSeed(hashSeed) + h.WriteString(me.p.Addr.String()) + return int64(h.Sum64()) +} + func (me prioritizedPeersItem) Less(than btree.Item) bool { other := than.(prioritizedPeersItem) return multiless.New().Bool( me.p.Trusted, other.p.Trusted).Uint32( - me.prio, other.prio, + me.prio, other.prio).Int64( + me.addrHash(), other.addrHash(), ).Less() } @@ -41,6 +53,17 @@ func (me *prioritizedPeers) Add(p Peer) bool { return me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p}) != nil } +// Returns true if a peer is replaced. +func (me *prioritizedPeers) AddReturningReplacedPeer(p Peer) (ret Peer, ok bool) { + item := me.om.ReplaceOrInsert(prioritizedPeersItem{me.getPrio(p), p}) + if item == nil { + return + } + ret = item.(prioritizedPeersItem).p + ok = true + return +} + func (me *prioritizedPeers) DeleteMin() (ret prioritizedPeersItem, ok bool) { i := me.om.DeleteMin() if i == nil { diff --git a/torrent.go b/torrent.go index 33ade6db..1cd5e257 100644 --- a/torrent.go +++ b/torrent.go @@ -266,8 +266,9 @@ func (t *Torrent) addPeer(p Peer) { return } } - if t.peers.Add(p) { + if replaced, ok := t.peers.AddReturningReplacedPeer(p); ok { torrent.Add("peers replaced", 1) + t.logger.Printf("added %v replacing %v", p, replaced) } t.openNewConns() for t.peers.Len() > cl.config.TorrentPeersHighWater {