Fix torrent fetching logic

This commit is contained in:
Ben Sarmiento
2024-05-06 10:58:48 +02:00
parent ae94252156
commit ab81eb5f39
2 changed files with 15 additions and 6 deletions

View File

@@ -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),

View File

@@ -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