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
if !found && len(batch) > 0 {
cachedCount := len(rd.torrentsCache) cachedCount := len(rd.torrentsCache)
for cIdx, cached := range rd.torrentsCache { // N cached torrents for cIdx, cached := range rd.torrentsCache { // N cached torrents
cIdxEnd := cachedCount - 1 - cIdx cIdxEnd := cachedCount - 1 - cIdx
for tIdx, torrent := range batch { // 250 torrents for tIdx, torrent := range batch { // 250 torrents in batch
tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total) tIdxEnd := indexFromEnd(tIdx, page+bIdx, pageSize, result.total)
if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd { if torrent.ID == cached.ID && torrent.Progress == cached.Progress && tIdxEnd == cIdxEnd {
allTorrents = append(allTorrents, batch[:tIdx]...) found = true
allTorrents = append(allTorrents, rd.torrentsCache[cIdx:]...) break
rd.log.Debugf("Got %d/%d torrents", len(allTorrents), result.total) }
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)