Store pending peers in a dict to prevent duplicates

This commit is contained in:
Matt Joiner 2014-08-21 21:10:19 +10:00
parent 74f05db2ea
commit e8f184a7bf
2 changed files with 22 additions and 4 deletions

View File

@ -874,8 +874,14 @@ func (me *Client) openNewConns() {
if me.halfOpen >= me.halfOpenLimit {
return
}
p := t.Peers[0]
t.Peers = t.Peers[1:]
var (
k peersKey
p Peer
)
for k, p = range t.Peers {
break
}
delete(t.Peers, k)
me.initiateConn(p, t)
}
}
@ -889,7 +895,7 @@ func (me *Client) AddPeers(infoHash InfoHash, peers []Peer) error {
if t == nil {
return errors.New("no such torrent")
}
t.Peers = append(t.Peers, peers...)
t.AddPeers(peers)
me.openNewConns()
return nil
}
@ -920,6 +926,7 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
func newTorrent(ih InfoHash, announceList [][]string) (t *torrent, err error) {
t = &torrent{
InfoHash: ih,
Peers: make(map[peersKey]Peer, 2000),
}
t.Trackers = make([][]tracker.Client, len(announceList))
for tierIndex := range announceList {

View File

@ -38,6 +38,11 @@ type torrentPiece struct {
bytesLeftElement *list.Element
}
type peersKey struct {
IPBytes string
Port int
}
type torrent struct {
closed bool
InfoHash InfoHash
@ -48,7 +53,7 @@ type torrent struct {
dataLock sync.RWMutex
Info *metainfo.Info
Conns []*connection
Peers []Peer
Peers map[peersKey]Peer
// BEP 12 Multitracker Metadata Extension. The tracker.Client instances
// mirror their respective URLs from the announce-list key.
Trackers [][]tracker.Client
@ -57,6 +62,12 @@ type torrent struct {
metadataHave []bool
}
func (t *torrent) AddPeers(pp []Peer) {
for _, p := range pp {
t.Peers[peersKey{string(p.IP), p.Port}] = p
}
}
func (t *torrent) InvalidateMetadata() {
t.MetaData = nil
t.metadataHave = nil