From ab81eb5f3992a26cf49996bcf6099ada241edf4a Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 6 May 2024 10:58:48 +0200 Subject: [PATCH] Fix torrent fetching logic --- internal/torrent/manager.go | 1 + pkg/realdebrid/api.go | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index b26121e..9b3b676 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -63,6 +63,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w log: log, DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](), + DownloadMap: cmap.New[*realdebrid.Download](), RootNode: fs.NewFileNode("root", true), diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index 3898fe8..9b61701 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -166,15 +166,23 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { totalCount := result.totalCount if onlyOne { + rd.log.Debugf("Returning early the %d torrents", len(allTorrents)) return allTorrents, totalCount, nil } - const MAX_PARALLEL = 4 + // reset allTorrents + allTorrents = []Torrent{} page := 1 - maxPages := totalCount / rd.cfg.GetTorrentsCount() + // compute ceiling of totalCount / limit + maxPages := (totalCount + rd.cfg.GetTorrentsCount() - 1) / rd.cfg.GetTorrentsCount() + rd.log.Debugf("Total count is %d, max pages is %d", totalCount, maxPages) + maxParallelThreads := 4 + if maxPages < maxParallelThreads { + maxParallelThreads = maxPages + } for { - allResults := make(chan getTorrentsResult, MAX_PARALLEL) // Channel to collect results from goroutines - for i := 0; i < MAX_PARALLEL; i++ { // Launch GET_PARALLEL concurrent fetches + allResults := make(chan getTorrentsResult, maxParallelThreads) // Channel to collect results from goroutines + for i := 0; i < maxParallelThreads; i++ { // Launch GET_PARALLEL concurrent fetches go func(add int) { if page > maxPages { allResults <- getTorrentsResult{nil, nil, 0} @@ -184,7 +192,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { }(i) } // Collect results from all goroutines - for i := 0; i < MAX_PARALLEL; i++ { + for i := 0; i < maxParallelThreads; i++ { res := <-allResults if res.err != nil { return nil, 0, res.err @@ -198,7 +206,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { break } - page += MAX_PARALLEL + page += maxParallelThreads } return allTorrents, totalCount, nil