Skip to content

Commit

Permalink
add Torrent.ModifyTrackers() func (#945)
Browse files Browse the repository at this point in the history
* add ClearTrackers() func

* add stop channel for each announcer

* add stop once avoid multi close

* use waitgroup to stop ann
  • Loading branch information
ucwong authored May 26, 2024
1 parent f705f59 commit 7958f02
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions t.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ func (t *Torrent) AddTrackers(announceList [][]string) {
t.addTrackers(announceList)
}

func (t *Torrent) ModifyTrackers(announceList [][]string) {
t.cl.lock()
defer t.cl.unlock()
t.modifyTrackers(announceList)
}

func (t *Torrent) Piece(i pieceIndex) *Piece {
return t.piece(i)
}
Expand Down
15 changes: 15 additions & 0 deletions torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,20 @@ func (t *Torrent) addTrackers(announceList [][]string) {
t.updateWantPeersEvent()
}

func (t *Torrent) modifyTrackers(announceList [][]string) {
var workers errgroup.Group
for _, v := range t.trackerAnnouncers {
workers.Go(func() error {
v.Stop()
return nil
})
}
workers.Wait()

clear(t.metainfo.AnnounceList)
t.addTrackers(announceList)
}

// Don't call this before the info is available.
func (t *Torrent) bytesCompleted() int64 {
if !t.haveInfo() {
Expand Down Expand Up @@ -1959,6 +1973,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
14 changes: 14 additions & 0 deletions tracker_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net"
"net/url"
"sync"
"time"

"github.com/anacrolix/dht/v2/krpc"
Expand All @@ -23,11 +24,16 @@ type trackerScraper struct {
t *Torrent
lastAnnounce trackerAnnounceResult
lookupTrackerIp func(*url.URL) ([]net.IP, error)

stopOnce sync.Once
stopCh chan struct{}
}

type torrentTrackerAnnouncer interface {
statusLine() string
URL() *url.URL

Stop()
}

func (me trackerScraper) URL() *url.URL {
Expand Down Expand Up @@ -202,6 +208,12 @@ func (me *trackerScraper) canIgnoreInterval(notify *<-chan struct{}) bool {
}
}

func (me *trackerScraper) Stop() {
me.stopOnce.Do(func() {
close(me.stopCh)
})
}

func (me *trackerScraper) Run() {
defer me.announceStopped()

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

select {
case <-me.stopCh:
return
case <-me.t.closed.Done():
return
case <-reconsider:
Expand Down
2 changes: 1 addition & 1 deletion webseed/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,4 @@ func readRequestPartResponses(ctx context.Context, parts []requestPart) (_ []byt
}
}
return buf.Bytes(), err
}
}
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 7958f02

Please sign in to comment.