Store pending peers in a dict to prevent duplicates
This commit is contained in:
parent
74f05db2ea
commit
e8f184a7bf
13
client.go
13
client.go
@ -874,8 +874,14 @@ func (me *Client) openNewConns() {
|
|||||||
if me.halfOpen >= me.halfOpenLimit {
|
if me.halfOpen >= me.halfOpenLimit {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p := t.Peers[0]
|
var (
|
||||||
t.Peers = t.Peers[1:]
|
k peersKey
|
||||||
|
p Peer
|
||||||
|
)
|
||||||
|
for k, p = range t.Peers {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
delete(t.Peers, k)
|
||||||
me.initiateConn(p, t)
|
me.initiateConn(p, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -889,7 +895,7 @@ func (me *Client) AddPeers(infoHash InfoHash, peers []Peer) error {
|
|||||||
if t == nil {
|
if t == nil {
|
||||||
return errors.New("no such torrent")
|
return errors.New("no such torrent")
|
||||||
}
|
}
|
||||||
t.Peers = append(t.Peers, peers...)
|
t.AddPeers(peers)
|
||||||
me.openNewConns()
|
me.openNewConns()
|
||||||
return nil
|
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) {
|
func newTorrent(ih InfoHash, announceList [][]string) (t *torrent, err error) {
|
||||||
t = &torrent{
|
t = &torrent{
|
||||||
InfoHash: ih,
|
InfoHash: ih,
|
||||||
|
Peers: make(map[peersKey]Peer, 2000),
|
||||||
}
|
}
|
||||||
t.Trackers = make([][]tracker.Client, len(announceList))
|
t.Trackers = make([][]tracker.Client, len(announceList))
|
||||||
for tierIndex := range announceList {
|
for tierIndex := range announceList {
|
||||||
|
13
torrent.go
13
torrent.go
@ -38,6 +38,11 @@ type torrentPiece struct {
|
|||||||
bytesLeftElement *list.Element
|
bytesLeftElement *list.Element
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type peersKey struct {
|
||||||
|
IPBytes string
|
||||||
|
Port int
|
||||||
|
}
|
||||||
|
|
||||||
type torrent struct {
|
type torrent struct {
|
||||||
closed bool
|
closed bool
|
||||||
InfoHash InfoHash
|
InfoHash InfoHash
|
||||||
@ -48,7 +53,7 @@ type torrent struct {
|
|||||||
dataLock sync.RWMutex
|
dataLock sync.RWMutex
|
||||||
Info *metainfo.Info
|
Info *metainfo.Info
|
||||||
Conns []*connection
|
Conns []*connection
|
||||||
Peers []Peer
|
Peers map[peersKey]Peer
|
||||||
// BEP 12 Multitracker Metadata Extension. The tracker.Client instances
|
// BEP 12 Multitracker Metadata Extension. The tracker.Client instances
|
||||||
// mirror their respective URLs from the announce-list key.
|
// mirror their respective URLs from the announce-list key.
|
||||||
Trackers [][]tracker.Client
|
Trackers [][]tracker.Client
|
||||||
@ -57,6 +62,12 @@ type torrent struct {
|
|||||||
metadataHave []bool
|
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() {
|
func (t *torrent) InvalidateMetadata() {
|
||||||
t.MetaData = nil
|
t.MetaData = nil
|
||||||
t.metadataHave = nil
|
t.metadataHave = nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user