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 {
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)