Fix torrent caching and staleness

This commit is contained in:
Ben Adrian Sarmiento
2024-06-21 00:12:06 +02:00
parent 10fa7774f4
commit d0baeb3d40

View File

@@ -37,6 +37,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
if maxPages < maxParallelThreads { if maxPages < maxParallelThreads {
maxParallelThreads = maxPages maxParallelThreads = maxPages
} }
found := false
for { for {
allResults := make(chan fetchTorrentsResult, maxParallelThreads) // Channel to collect results from goroutines allResults := make(chan fetchTorrentsResult, maxParallelThreads) // Channel to collect results from goroutines
for i := 0; i < maxParallelThreads; i++ { // Launch GET_PARALLEL concurrent fetches 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...) batches[bIdx] = append(batches[bIdx], result.torrents...)
} }
for bIdx, batch := range batches { // 4 batches for bIdx, batch := range batches { // 4 batches
cachedCount := len(rd.torrentsCache) if !found && len(batch) > 0 {
for cIdx, cached := range rd.torrentsCache { // N cached torrents cachedCount := len(rd.torrentsCache)
cIdxEnd := cachedCount - 1 - cIdx for cIdx, cached := range rd.torrentsCache { // N cached torrents
for tIdx, torrent := range batch { // 250 torrents cIdxEnd := cachedCount - 1 - cIdx
tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total) for tIdx, torrent := range batch { // 250 torrents in batch
if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd { tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total)
allTorrents = append(allTorrents, batch[:tIdx]...) if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd {
allTorrents = append(allTorrents, rd.torrentsCache[cIdx:]...) found = true
rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total) break
rd.cacheTorrents(allTorrents) }
return allTorrents, len(allTorrents), nil }
if found {
// rd.log.Debugf("From torrent %s (id=%s) onwards, the torrents were untouched", cached.Name, cached.ID)
break
} }
} }
} }
allTorrents = append(allTorrents, batch...) allTorrents = append(allTorrents, batch...)
} }
if found {
allTorrents = append(allTorrents, rd.torrentsCache[len(allTorrents):]...)
}
rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total) rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total)