Skip to content

Commit

Permalink
add stop channel for each announcer
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed May 24, 2024
1 parent 2b5fb4d commit 9c52dc1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions t.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tracker_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -252,6 +260,8 @@ func (me *trackerScraper) Run() {
}

select {
case <-me.stopCh:
return
case <-me.t.closed.Done():
return
case <-reconsider:
Expand Down
3 changes: 3 additions & 0 deletions wstracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (me websocketTrackerStatus) URL() *url.URL {
return &me.url
}

func (me websocketTrackerStatus) Stop() {
}

type refCountedWebtorrentTrackerClient struct {
webtorrent.TrackerClient
refCount int
Expand Down

0 comments on commit 9c52dc1

Please sign in to comment.