diff --git a/http/announce_test.go b/http/announce_test.go index 5495965..3c8ea57 100644 --- a/http/announce_test.go +++ b/http/announce_test.go @@ -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) diff --git a/tracker/announce.go b/tracker/announce.go index 4976b72..80d8fc2 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -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) diff --git a/tracker/models/peermap.go b/tracker/models/peermap.go index a6940d3..571318f 100644 --- a/tracker/models/peermap.go +++ b/tracker/models/peermap.go @@ -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.