From: Matt Joiner Date: Thu, 1 Oct 2020 00:43:10 +0000 (+1000) Subject: Limit simultaneous announces to the same URL X-Git-Tag: v1.17.0~7 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=a3827099c40d4931255358b918aa1a55720b1505;p=btrtrc.git Limit simultaneous announces to the same URL --- diff --git a/client.go b/client.go index 6292d778..7ae72295 100644 --- a/client.go +++ b/client.go @@ -78,6 +78,7 @@ type Client struct { numHalfOpen int websocketTrackers websocketTrackers + activeAnnounces map[string]struct{} } type ipStr string diff --git a/torrent.go b/torrent.go index 21069ec5..3c0bcb09 100644 --- a/torrent.go +++ b/torrent.go @@ -1411,9 +1411,32 @@ func (t *Torrent) startScrapingTracker(_url string) { return nil } } + urlString := (*u).String() + cl := t.cl newAnnouncer := &trackerScraper{ u: *u, t: t, + allow: func() { + cl.lock() + defer cl.unlock() + if cl.activeAnnounces == nil { + cl.activeAnnounces = make(map[string]struct{}) + } + for { + if _, ok := cl.activeAnnounces[urlString]; ok { + cl.event.Wait() + } else { + break + } + } + cl.activeAnnounces[urlString] = struct{}{} + }, + done: func() { + cl.lock() + defer cl.unlock() + delete(cl.activeAnnounces, urlString) + cl.event.Broadcast() + }, } go newAnnouncer.Run() return newAnnouncer diff --git a/tracker_scraper.go b/tracker_scraper.go index 13237953..b7d38c5a 100644 --- a/tracker_scraper.go +++ b/tracker_scraper.go @@ -20,6 +20,7 @@ type trackerScraper struct { u url.URL t *Torrent lastAnnounce trackerAnnounceResult + allow, done func() } type torrentTrackerAnnouncer interface { @@ -108,6 +109,8 @@ func (me *trackerScraper) announce(event tracker.AnnounceEvent) (ret trackerAnno ret.Completed = time.Now() }() ret.Interval = time.Minute + me.allow() + defer me.done() ip, err := me.getIp() if err != nil { ret.Err = fmt.Errorf("error getting ip: %s", err)