From 9c52dc1ba1920762768d6526f9125064d011dd51 Mon Sep 17 00:00:00 2001 From: ucwong Date: Fri, 24 May 2024 18:30:22 +0800 Subject: [PATCH] add stop channel for each announcer --- t.go | 4 ++-- torrent.go | 7 ++++++- tracker_scraper.go | 10 ++++++++++ wstracker.go | 3 +++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/t.go b/t.go index d080072ac6..219656a6ce 100644 --- a/t.go +++ b/t.go @@ -266,10 +266,10 @@ func (t *Torrent) AddTrackers(announceList [][]string) { t.addTrackers(announceList) } -func (t *Torrent) ClearTrackers() { +func (t *Torrent) ModifyTrackers(announceList [][]string) { t.cl.lock() defer t.cl.unlock() - t.clearTrackers() + t.modifyTrackers(announceList) } func (t *Torrent) Piece(i pieceIndex) *Piece { diff --git a/torrent.go b/torrent.go index 9326e338da..41dc249e13 100644 --- a/torrent.go +++ b/torrent.go @@ -1708,8 +1708,12 @@ func (t *Torrent) addTrackers(announceList [][]string) { t.updateWantPeersEvent() } -func (t *Torrent) clearTrackers() { +func (t *Torrent) modifyTrackers(announceList [][]string) { + for _, v := range t.trackerAnnouncers { + v.Stop() + } clear(t.metainfo.AnnounceList) + t.addTrackers(announceList) } // Don't call this before the info is available. @@ -1963,6 +1967,7 @@ func (t *Torrent) startScrapingTrackerWithInfohash(u *url.URL, urlStr string, sh u: *u, t: t, lookupTrackerIp: t.cl.config.LookupTrackerIp, + stopCh: make(chan struct{}), } go newAnnouncer.Run() return newAnnouncer diff --git a/tracker_scraper.go b/tracker_scraper.go index 0668c9127e..4573341355 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -23,11 +23,15 @@ type trackerScraper struct { t *Torrent lastAnnounce trackerAnnounceResult lookupTrackerIp func(*url.URL) ([]net.IP, error) + + stopCh chan struct{} } type torrentTrackerAnnouncer interface { statusLine() string URL() *url.URL + + Stop() } func (me trackerScraper) URL() *url.URL { @@ -202,6 +206,10 @@ func (me *trackerScraper) canIgnoreInterval(notify *<-chan struct{}) bool { } } +func (me *trackerScraper) Stop() { + close(me.stopCh) +} + func (me *trackerScraper) Run() { defer me.announceStopped() @@ -252,6 +260,8 @@ func (me *trackerScraper) Run() { } select { + case <-me.stopCh: + return case <-me.t.closed.Done(): return case <-reconsider: diff --git a/wstracker.go b/wstracker.go index ff7b4b1600..f94c3923b0 100644 --- a/wstracker.go +++ b/wstracker.go @@ -31,6 +31,9 @@ func (me websocketTrackerStatus) URL() *url.URL { return &me.url } +func (me websocketTrackerStatus) Stop() { +} + type refCountedWebtorrentTrackerClient struct { webtorrent.TrackerClient refCount int