Simpler, more efficient way of deleting inactive torrents

This commit is contained in:
Justin Li
2014-07-16 15:03:33 -04:00
parent e29fb21edb
commit 7fe6dc3b4e
5 changed files with 21 additions and 80 deletions
+16 -20
View File
@@ -171,6 +171,22 @@ func (c *Conn) DeleteTorrent(infohash string) error {
return nil
}
func (c *Conn) PurgeInactiveTorrent(infohash string) error {
c.torrentsM.Lock()
defer c.torrentsM.Unlock()
torrent, exists := c.torrents[infohash]
if !exists {
return tracker.ErrTorrentDNE
}
if torrent.PeerCount() == 0 {
delete(c.torrents, infohash)
}
return nil
}
func (c *Conn) PutUser(u *models.User) error {
c.usersM.Lock()
defer c.usersM.Unlock()
@@ -207,23 +223,3 @@ func (c *Conn) DeleteClient(peerID string) error {
return nil
}
func (c *Conn) PurgeInactiveTorrents(before time.Time) error {
unixtime := before.Unix()
var queue []string
c.torrentsM.RLock()
for key, torrent := range c.torrents {
if torrent.LastAction < unixtime && torrent.PeerCount() == 0 {
queue = append(queue, key)
}
}
c.torrentsM.RUnlock()
c.torrentsM.Lock()
for _, key := range queue {
delete(c.torrents, key)
}
c.torrentsM.Unlock()
return nil
}