From d0baeb3d40d013bbf9424fbf884cf39b0f3cb992 Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Fri, 21 Jun 2024 00:12:06 +0200 Subject: [PATCH] Fix torrent caching and staleness --- pkg/realdebrid/torrents.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/pkg/realdebrid/torrents.go b/pkg/realdebrid/torrents.go index 26220ff..c31f836 100644 --- a/pkg/realdebrid/torrents.go +++ b/pkg/realdebrid/torrents.go @@ -37,6 +37,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { if maxPages < maxParallelThreads { maxParallelThreads = maxPages } + found := false for { allResults := make(chan fetchTorrentsResult, maxParallelThreads) // Channel to collect results from goroutines for i := 0; i < maxParallelThreads; i++ { // Launch GET_PARALLEL concurrent fetches @@ -66,22 +67,28 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { batches[bIdx] = append(batches[bIdx], result.torrents...) } for bIdx, batch := range batches { // 4 batches - cachedCount := len(rd.torrentsCache) - for cIdx, cached := range rd.torrentsCache { // N cached torrents - cIdxEnd := cachedCount - 1 - cIdx - for tIdx, torrent := range batch { // 250 torrents - tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total) - if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd { - allTorrents = append(allTorrents, batch[:tIdx]...) - allTorrents = append(allTorrents, rd.torrentsCache[cIdx:]...) - rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total) - rd.cacheTorrents(allTorrents) - return allTorrents, len(allTorrents), nil + if !found && len(batch) > 0 { + cachedCount := len(rd.torrentsCache) + for cIdx, cached := range rd.torrentsCache { // N cached torrents + cIdxEnd := cachedCount - 1 - cIdx + for tIdx, torrent := range batch { // 250 torrents in batch + tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total) + if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd { + found = true + break + } + } + if found { + // rd.log.Debugf("From torrent %s (id=%s) onwards, the torrents were untouched", cached.Name, cached.ID) + break } } } allTorrents = append(allTorrents, batch...) } + if found { + allTorrents = append(allTorrents, rd.torrentsCache[len(allTorrents):]...) + } rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total)