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:
parent
01fa778ce2
commit
fc18028796
3 changed files with 17 additions and 8 deletions
|
@ -374,8 +374,8 @@ func loadPrivateTestData(tkr *tracker.Tracker) error {
|
||||||
torrent := &models.Torrent{
|
torrent := &models.Torrent{
|
||||||
ID: 1,
|
ID: 1,
|
||||||
Infohash: infoHash,
|
Infohash: infoHash,
|
||||||
Seeders: models.NewPeerMap(),
|
Seeders: models.NewPeerMap(true),
|
||||||
Leechers: models.NewPeerMap(),
|
Leechers: models.NewPeerMap(false),
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn.PutTorrent(torrent)
|
return conn.PutTorrent(torrent)
|
||||||
|
|
|
@ -37,8 +37,8 @@ func (tkr *Tracker) HandleAnnounce(ann *models.Announce, w Writer) error {
|
||||||
if err == models.ErrTorrentDNE && !tkr.cfg.PrivateEnabled {
|
if err == models.ErrTorrentDNE && !tkr.cfg.PrivateEnabled {
|
||||||
torrent = &models.Torrent{
|
torrent = &models.Torrent{
|
||||||
Infohash: ann.Infohash,
|
Infohash: ann.Infohash,
|
||||||
Seeders: models.NewPeerMap(),
|
Seeders: models.NewPeerMap(true),
|
||||||
Leechers: models.NewPeerMap(),
|
Leechers: models.NewPeerMap(false),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = conn.PutTorrent(torrent)
|
err = conn.PutTorrent(torrent)
|
||||||
|
|
|
@ -14,14 +14,16 @@ import (
|
||||||
|
|
||||||
// PeerMap is a thread-safe map from PeerKeys to Peers.
|
// PeerMap is a thread-safe map from PeerKeys to Peers.
|
||||||
type PeerMap struct {
|
type PeerMap struct {
|
||||||
peers map[PeerKey]Peer
|
seeders bool
|
||||||
|
peers map[PeerKey]Peer
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPeerMap initializes the map for a new PeerMap.
|
// NewPeerMap initializes the map for a new PeerMap.
|
||||||
func NewPeerMap() PeerMap {
|
func NewPeerMap(seeders bool) PeerMap {
|
||||||
return 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
|
// Purge iterates over all of the peers within a PeerMap and deletes them if
|
||||||
// they are older than the provided time.
|
// they are older than the provided time.
|
||||||
func (pm *PeerMap) Purge(unixtime int64) {
|
func (pm *PeerMap) Purge(unixtime int64) {
|
||||||
|
|
||||||
pm.Lock()
|
pm.Lock()
|
||||||
defer pm.Unlock()
|
defer pm.Unlock()
|
||||||
|
|
||||||
for key, peer := range pm.peers {
|
for key, peer := range pm.peers {
|
||||||
if peer.LastAnnounce <= unixtime {
|
if peer.LastAnnounce <= unixtime {
|
||||||
delete(pm.peers, key)
|
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.
|
// AppendPeers adds peers to given IPv4 or IPv6 lists.
|
||||||
|
|
Loading…
Add table
Reference in a new issue