Skip to content

Commit

Permalink
fast timeout of tracker scraping, fixes scakemyer/plugin.video.quasar…
Browse files Browse the repository at this point in the history
  • Loading branch information
scakemyer committed Dec 14, 2016
1 parent c3f31b8 commit 3491265
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions providers/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package providers
import (
"sort"
"sync"
"time"

"github.com/op/go-logging"
"github.com/scakemyer/quasar/bittorrent"
Expand All @@ -28,6 +29,8 @@ const (
Sort480p720p1080p
)

var trackerTimeout = time.Duration(5) * time.Second

var log = logging.MustGetLogger("linkssearch")

func Search(searchers []Searcher, query string) []*bittorrent.Torrent {
Expand Down Expand Up @@ -110,6 +113,20 @@ func SearchEpisode(searchers []EpisodeSearcher, show *tmdb.Show, episode *tmdb.E
return processLinks(torrentsChan, SortShows)
}

func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false // completed normally
case <-time.After(timeout):
return true // timed out
}
}

func processLinks(torrentsChan chan *bittorrent.Torrent, sortType int) []*bittorrent.Torrent {
trackers := map[string]*bittorrent.Tracker{}
torrentsMap := map[string]*bittorrent.Torrent{}
Expand Down Expand Up @@ -182,6 +199,7 @@ func processLinks(torrentsChan chan *bittorrent.Torrent, sortType int) []*bittor
}

log.Infof("Scraping torrent metrics from %d trackers...\n", len(trackers))

scrapeResults := make(chan []bittorrent.ScrapeResponseEntry)
go func() {
wg := sync.WaitGroup{}
Expand All @@ -190,13 +208,18 @@ func processLinks(torrentsChan chan *bittorrent.Torrent, sortType int) []*bittor
go func(tracker *bittorrent.Tracker) {
defer wg.Done()
if err := tracker.Connect(); err != nil {
log.Errorf("Tracker %s is not available because: %s", tracker, err)
log.Errorf("Tracker %s failed: %s", tracker, err)
return
}
scrapeResults <- tracker.Scrape(torrents)
}(tracker)
}
wg.Wait()
// wg.Wait()
if waitTimeout(&wg, trackerTimeout) {
log.Error("Timed out waiting for trackers.")
} else {
log.Notice("Finished scraping trackers.")
}
close(scrapeResults)
}()

Expand All @@ -210,6 +233,7 @@ func processLinks(torrentsChan chan *bittorrent.Torrent, sortType int) []*bittor
}
}
}
log.Notice("Finished comparing seeds/peers of results to trackers...")

conf := config.Get()
sortMode := conf.SortingModeMovies
Expand Down

0 comments on commit 3491265

Please sign in to comment.