Send tracker stopped event from the tracker scraper routine

Fixes potential blocking on the announce while the client lock is held, as well as differing arguments to the other announces introduced by #321.
This commit is contained in:
Matt Joiner 2019-07-17 11:56:25 +10:00
parent e56ea362d9
commit 5b44954223
2 changed files with 5 additions and 20 deletions

View File

@ -666,9 +666,6 @@ func (t *Torrent) numPiecesCompleted() (num int) {
}
func (t *Torrent) close() (err error) {
for _, ta := range t.trackerAnnouncers {
ta.Stop()
}
t.closed.Set()
t.tickleReaders()
if t.storage != nil {

View File

@ -9,16 +9,13 @@ import (
"time"
"github.com/anacrolix/dht/krpc"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/torrent/tracker"
)
// Announces a torrent to a tracker at regular intervals, when peers are
// required.
type trackerScraper struct {
u url.URL
// Causes the trackerScraper to stop running.
stop missinggo.Event
u url.URL
t *Torrent
lastAnnounce trackerAnnounceResult
}
@ -109,6 +106,7 @@ func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnno
me.t.cl.lock()
req := me.t.announceRequest(event)
me.t.cl.unlock()
//log.Printf("announcing %s %s to %q", me.t, req.Event, me.u.String())
res, err := tracker.Announce{
HTTPProxy: me.t.cl.config.HTTPProxy,
UserAgent: me.t.cl.config.HTTPUserAgent,
@ -131,6 +129,7 @@ func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnno
}
func (me *trackerScraper) Run() {
defer me.announceStopped()
// make sure first announce is a "started"
e := tracker.Started
for {
@ -159,8 +158,6 @@ func (me *trackerScraper) Run() {
select {
case <-me.t.closed.LockedChan(me.t.cl.locker()):
return
case <-me.stop.LockedChan(me.t.cl.locker()):
return
case <-wantPeers:
goto wait
case <-time.After(time.Until(ar.Completed.Add(interval))):
@ -168,15 +165,6 @@ func (me *trackerScraper) Run() {
}
}
func (me *trackerScraper) Stop() {
req := me.t.announceRequest(tracker.Stopped)
ip, _ := me.getIp()
tracker.Announce{
HTTPProxy: me.t.cl.config.HTTPProxy,
UserAgent: me.t.cl.config.HTTPUserAgent,
TrackerUrl: me.trackerUrl(ip),
Request: req,
HostHeader: me.u.Host,
ServerName: me.u.Hostname(),
}.Do()
func (me *trackerScraper) announceStopped() {
me.announce(tracker.Stopped)
}