Remove get torrents count config and use worker pool on all places
This commit is contained in:
@@ -11,21 +11,24 @@ import (
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
|
||||
"github.com/debridmediamanager/zurg/pkg/logutil"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
)
|
||||
|
||||
type RealDebrid struct {
|
||||
apiClient *zurghttp.HTTPClient
|
||||
unrestrictClient *zurghttp.HTTPClient
|
||||
downloadClient *zurghttp.HTTPClient
|
||||
workerPool *ants.Pool
|
||||
cfg config.ConfigInterface
|
||||
log *logutil.Logger
|
||||
}
|
||||
|
||||
func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPClient, cfg config.ConfigInterface, log *logutil.Logger) *RealDebrid {
|
||||
func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPClient, workerPool *ants.Pool, cfg config.ConfigInterface, log *logutil.Logger) *RealDebrid {
|
||||
return &RealDebrid{
|
||||
apiClient: apiClient,
|
||||
unrestrictClient: unrestrictClient,
|
||||
downloadClient: downloadClient,
|
||||
workerPool: workerPool,
|
||||
cfg: cfg,
|
||||
log: log,
|
||||
}
|
||||
@@ -173,7 +176,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
|
||||
allTorrents = []Torrent{}
|
||||
page := 1
|
||||
// compute ceiling of totalCount / limit
|
||||
maxPages := (totalCount + rd.cfg.GetTorrentsCount() - 1) / rd.cfg.GetTorrentsCount()
|
||||
maxPages := (totalCount + 250 - 1) / 250
|
||||
rd.log.Debugf("Torrents total count is %d", totalCount)
|
||||
maxParallelThreads := 4
|
||||
if maxPages < maxParallelThreads {
|
||||
@@ -182,13 +185,13 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) {
|
||||
for {
|
||||
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) {
|
||||
rd.workerPool.Submit(func() {
|
||||
if page > maxPages {
|
||||
allResults <- getTorrentsResult{nil, nil, 0}
|
||||
return
|
||||
}
|
||||
allResults <- rd.getPageOfTorrents(page+add, rd.cfg.GetTorrentsCount())
|
||||
}(i)
|
||||
allResults <- rd.getPageOfTorrents(page+i, 250)
|
||||
})
|
||||
}
|
||||
// Collect results from all goroutines
|
||||
for i := 0; i < maxParallelThreads; i++ {
|
||||
@@ -379,13 +382,13 @@ func (rd *RealDebrid) GetDownloads() []Download {
|
||||
allResults := make(chan []Download, maxParallelThreads) // Channel to collect results from goroutines
|
||||
errChan := make(chan error, maxParallelThreads) // Channel to collect errors from goroutines
|
||||
for i := 0; i < maxParallelThreads; i++ { // Launch GET_PARALLEL concurrent fetches
|
||||
go func(add int) {
|
||||
if page+add > maxPages {
|
||||
rd.workerPool.Submit(func() {
|
||||
if page+i > maxPages {
|
||||
allResults <- nil
|
||||
errChan <- nil
|
||||
return
|
||||
}
|
||||
result, _, err := rd.fetchPageOfDownloads(page+add, limit)
|
||||
result, _, err := rd.fetchPageOfDownloads(page+i, limit)
|
||||
if err != nil {
|
||||
allResults <- nil
|
||||
errChan <- err
|
||||
@@ -393,7 +396,7 @@ func (rd *RealDebrid) GetDownloads() []Download {
|
||||
}
|
||||
allResults <- result
|
||||
errChan <- nil
|
||||
}(i)
|
||||
})
|
||||
}
|
||||
// Collect results from all goroutines
|
||||
for i := 0; i < maxParallelThreads; i++ {
|
||||
|
||||
Reference in New Issue
Block a user