From e29fb21edbc7c2a594f914a570b640aacb488d1c Mon Sep 17 00:00:00 2001 From: Justin Li Date: Wed, 16 Jul 2014 14:11:01 -0400 Subject: [PATCH] Only prune torrents if they have no peers --- drivers/tracker/memory/conn.go | 2 +- http/announce_test.go | 11 +++++++++-- models/models.go | 4 ++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/tracker/memory/conn.go b/drivers/tracker/memory/conn.go index 106ad20..90eba49 100644 --- a/drivers/tracker/memory/conn.go +++ b/drivers/tracker/memory/conn.go @@ -213,7 +213,7 @@ func (c *Conn) PurgeInactiveTorrents(before time.Time) error { c.torrentsM.RLock() for key, torrent := range c.torrents { - if torrent.LastAction < unixtime { + if torrent.LastAction < unixtime && torrent.PeerCount() == 0 { queue = append(queue, key) } } diff --git a/http/announce_test.go b/http/announce_test.go index 5027350..1183333 100644 --- a/http/announce_test.go +++ b/http/announce_test.go @@ -78,7 +78,15 @@ func TestTorrentPurging(t *testing.T) { if err != nil { 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 { t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status)) } @@ -88,13 +96,12 @@ func TestTorrentPurging(t *testing.T) { peer["event"] = "stopped" announce(peer, srv) - time.Sleep(1000 * time.Millisecond) + time.Sleep(1010 * time.Millisecond) _, status, err = fetchPath(torrentApiPath) if err != nil { t.Fatal(err) } - if status != http.StatusNotFound { t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status)) } diff --git a/models/models.go b/models/models.go index cf37311..8766f2e 100644 --- a/models/models.go +++ b/models/models.go @@ -103,6 +103,10 @@ func (t *Torrent) InLeecherPool(p *Peer) (exists bool) { return } +func (t *Torrent) PeerCount() int { + return len(t.Seeders) + len(t.Leechers) +} + // User is a registered user for private trackers. type User struct { ID uint64 `json:"id"`