diff --git a/config/config.go b/config/config.go index 9740d1b..e9a613a 100644 --- a/config/config.go +++ b/config/config.go @@ -81,6 +81,8 @@ type TrackerConfig struct { PurgeInactiveTorrents bool `json:"purge_inactive_torrents"` Announce Duration `json:"announce"` MinAnnounce Duration `json:"min_announce"` + ReapInterval Duration `json:"reap_interval"` + ReapRatio float64 `json:"reap_ratio"` NumWantFallback int `json:"default_num_want"` TorrentMapShards int `json:"torrent_map_shards"` @@ -121,6 +123,8 @@ var DefaultConfig = Config{ PurgeInactiveTorrents: true, Announce: Duration{30 * time.Minute}, MinAnnounce: Duration{15 * time.Minute}, + ReapInterval: Duration{60 * time.Second}, + ReapRatio: 1.25, NumWantFallback: 50, TorrentMapShards: 1, diff --git a/example_config.json b/example_config.json index c71042b..bf24235 100644 --- a/example_config.json +++ b/example_config.json @@ -5,6 +5,8 @@ "purge_inactive_torrents": true, "announce": "30m", "min_announce": "15m", + "reap_interval": "60s", + "reap_ratio": "1.25", "default_num_want": 50, "torrent_map_shards": 1, "allow_ip_spoofing": true, diff --git a/http/announce_test.go b/http/announce_test.go index 55edb84..1406cea 100644 --- a/http/announce_test.go +++ b/http/announce_test.go @@ -84,9 +84,8 @@ func TestTorrentPurging(t *testing.T) { func TestStalePeerPurging(t *testing.T) { cfg := config.DefaultConfig - cfg.Announce = config.Duration{ - Duration: 10 * time.Millisecond, - } + cfg.MinAnnounce = config.Duration{10 * time.Millisecond} + cfg.ReapInterval = config.Duration{10 * time.Millisecond} srv, err := setupTracker(&cfg) if err != nil { @@ -110,7 +109,7 @@ func TestStalePeerPurging(t *testing.T) { // Add a leecher. peer2 := makePeerParams("peer2", false) expected := makeResponse(1, 1, peer1) - expected["interval"] = int64(0) + expected["min interval"] = int64(0) checkAnnounce(peer2, expected, srv, t) // Let them both expire. diff --git a/tracker/tracker.go b/tracker/tracker.go index 35eecbe..d01ef22 100644 --- a/tracker/tracker.go +++ b/tracker/tracker.go @@ -49,8 +49,8 @@ func New(cfg *config.Config) (*Tracker, error) { go tkr.purgeInactivePeers( cfg.PurgeInactiveTorrents, - cfg.Announce.Duration*2, - cfg.Announce.Duration, + time.Duration(float64(cfg.MinAnnounce.Duration)*cfg.ReapRatio), + cfg.ReapInterval.Duration, ) if cfg.ClientWhitelistEnabled { @@ -86,13 +86,6 @@ type Writer interface { // purgeInactivePeers periodically walks the torrent database and removes // peers that haven't announced recently. -// -// The default threshold is 2x the announce interval, which gives delayed -// peers a chance to stay alive, while ensuring the majority of responses -// contain active peers. -// -// The default interval is equal to the announce interval, since this is a -// relatively expensive operation. func (tkr *Tracker) purgeInactivePeers(purgeEmptyTorrents bool, threshold, interval time.Duration) { for _ = range time.NewTicker(interval).C { before := time.Now().Add(-threshold)