Fix Purge reporting all peers as Seeders

PeerMaps now are constructed with a boolean value that tells the map
whether they are hosting seeders or leechers.
This commit is contained in:
Jimmy Zelinskie 2014-08-05 06:53:35 -04:00
parent 01fa778ce2
commit fc18028796
3 changed files with 17 additions and 8 deletions

View file

@ -374,8 +374,8 @@ func loadPrivateTestData(tkr *tracker.Tracker) error {
torrent := &models.Torrent{
ID: 1,
Infohash: infoHash,
Seeders: models.NewPeerMap(),
Leechers: models.NewPeerMap(),
Seeders: models.NewPeerMap(true),
Leechers: models.NewPeerMap(false),
}
return conn.PutTorrent(torrent)

View file

@ -37,8 +37,8 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
if err == models.ErrTorrentDNE && !tkr.cfg.PrivateEnabled {
torrent = &models.Torrent{
Infohash: ann.Infohash,
Seeders: models.NewPeerMap(),
Leechers: models.NewPeerMap(),
Seeders: models.NewPeerMap(true),
Leechers: models.NewPeerMap(false),
}
err = conn.PutTorrent(torrent)

View file

@ -14,14 +14,16 @@ import (
// PeerMap is a thread-safe map from PeerKeys to Peers.
type PeerMap struct {
peers map[PeerKey]Peer
seeders bool
peers map[PeerKey]Peer
sync.RWMutex
}
// NewPeerMap initializes the map for a new PeerMap.
func NewPeerMap() PeerMap {
func NewPeerMap(seeders bool) PeerMap {
return PeerMap{
peers: make(map[PeerKey]Peer),
peers: make(map[PeerKey]Peer),
seeders: seeders,
}
}
@ -92,15 +94,22 @@ func (pm *PeerMap) UnmarshalJSON(b []byte) error {
// Purge iterates over all of the peers within a PeerMap and deletes them if
// they are older than the provided time.
func (pm *PeerMap) Purge(unixtime int64) {
pm.Lock()
defer pm.Unlock()
for key, peer := range pm.peers {
if peer.LastAnnounce <= unixtime {
delete(pm.peers, key)
stats.RecordPeerEvent(stats.ReapedSeed, peer.HasIPv6())
if pm.seeders {
stats.RecordPeerEvent(stats.ReapedSeed, peer.HasIPv6())
} else {
stats.RecordPeerEvent(stats.ReapedLeech, peer.HasIPv6())
}
}
}
return
}
// AppendPeers adds peers to given IPv4 or IPv6 lists.