Only prune torrents if they have no peers

This commit is contained in:
Justin Li 2014-07-16 14:11:01 -04:00
parent 5f4f63cc44
commit e29fb21edb
3 changed files with 14 additions and 3 deletions

View file

@ -213,7 +213,7 @@ func (c *Conn) PurgeInactiveTorrents(before time.Time) error {
c.torrentsM.RLock() c.torrentsM.RLock()
for key, torrent := range c.torrents { for key, torrent := range c.torrents {
if torrent.LastAction < unixtime { if torrent.LastAction < unixtime && torrent.PeerCount() == 0 {
queue = append(queue, key) queue = append(queue, key)
} }
} }

View file

@ -78,7 +78,15 @@ func TestTorrentPurging(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if status != http.StatusOK {
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
}
time.Sleep(1010 * time.Millisecond)
_, status, err = fetchPath(torrentApiPath)
if err != nil {
t.Fatal(err)
}
if status != http.StatusOK { if status != http.StatusOK {
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status)) t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
} }
@ -88,13 +96,12 @@ func TestTorrentPurging(t *testing.T) {
peer["event"] = "stopped" peer["event"] = "stopped"
announce(peer, srv) announce(peer, srv)
time.Sleep(1000 * time.Millisecond) time.Sleep(1010 * time.Millisecond)
_, status, err = fetchPath(torrentApiPath) _, status, err = fetchPath(torrentApiPath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if status != http.StatusNotFound { if status != http.StatusNotFound {
t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status)) t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status))
} }

View file

@ -103,6 +103,10 @@ func (t *Torrent) InLeecherPool(p *Peer) (exists bool) {
return return
} }
func (t *Torrent) PeerCount() int {
return len(t.Seeders) + len(t.Leechers)
}
// User is a registered user for private trackers. // User is a registered user for private trackers.
type User struct { type User struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`