Use ants pool instead
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -25,20 +26,20 @@ type TorrentManager struct {
|
||||
requiredVersion string
|
||||
checksum string
|
||||
api *realdebrid.RealDebrid
|
||||
workerPool chan bool
|
||||
antsPool *ants.Pool
|
||||
log *zap.SugaredLogger
|
||||
}
|
||||
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
// it will fetch all torrents and their info in the background
|
||||
// and store them in-memory and cached in files
|
||||
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid) *TorrentManager {
|
||||
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p *ants.Pool) *TorrentManager {
|
||||
t := &TorrentManager{
|
||||
cfg: cfg,
|
||||
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
|
||||
requiredVersion: "10.11.2023",
|
||||
requiredVersion: "18.11.2023",
|
||||
api: api,
|
||||
workerPool: make(chan bool, cfg.GetNumOfWorkers()),
|
||||
antsPool: p,
|
||||
log: logutil.NewLogger().Named("manager"),
|
||||
}
|
||||
|
||||
@@ -59,11 +60,10 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid) *
|
||||
for i := range newTorrents {
|
||||
wg.Add(1)
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
t.workerPool <- true
|
||||
// TODO wrap getMoreInfo and limit the execution time!
|
||||
torrentsChan <- t.getMoreInfo(newTorrents[idx])
|
||||
<-t.workerPool
|
||||
_ = t.antsPool.Submit(func() {
|
||||
defer wg.Done()
|
||||
torrentsChan <- t.getMoreInfo(newTorrents[idx])
|
||||
})
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
@@ -164,9 +164,7 @@ func (t *TorrentManager) mergeToMain(t1, t2 *Torrent) *Torrent {
|
||||
|
||||
// proxy
|
||||
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.UnrestrictResponse {
|
||||
t.workerPool <- true
|
||||
ret := t.api.UnrestrictUntilOk(link)
|
||||
<-t.workerPool
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -248,10 +246,10 @@ func (t *TorrentManager) startRefreshJob() {
|
||||
for i := range newTorrents {
|
||||
wg.Add(1)
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
t.workerPool <- true
|
||||
torrentsChan <- t.getMoreInfo(newTorrents[idx])
|
||||
<-t.workerPool
|
||||
_ = t.antsPool.Submit(func() {
|
||||
defer wg.Done()
|
||||
torrentsChan <- t.getMoreInfo(newTorrents[idx])
|
||||
})
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
@@ -484,13 +482,16 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([
|
||||
|
||||
for _, link := range links {
|
||||
wg.Add(1)
|
||||
go func(lnk string) {
|
||||
link := link // redeclare to avoid closure on loop variable
|
||||
// Use the existing workerPool to submit tasks
|
||||
err := t.antsPool.Submit(func() {
|
||||
defer wg.Done()
|
||||
t.workerPool <- true
|
||||
resp := t.api.UnrestrictUntilOk(lnk)
|
||||
<-t.workerPool
|
||||
resp := t.api.UnrestrictUntilOk(link)
|
||||
resultsChan <- Result{Response: resp}
|
||||
}(link)
|
||||
})
|
||||
if err != nil {
|
||||
t.log.Errorf("Error submitting task to workerPool: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
|
||||
Reference in New Issue
Block a user