Skip to content

Commit

Permalink
Parallel solve running torrents
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed May 6, 2024
1 parent 7ea67c5 commit e461a92
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions backend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import (
"github.com/CortexFoundation/torrentfs/backend/job"
"github.com/CortexFoundation/torrentfs/params"
"github.com/CortexFoundation/torrentfs/types"

"golang.org/x/sync/errgroup"
)

type TorrentManager struct {
Expand Down Expand Up @@ -1163,24 +1165,21 @@ func (tm *TorrentManager) pendingLoop() {
}
}

func (tm *TorrentManager) finish(t *caffe.Torrent) {
func (tm *TorrentManager) finish(t *caffe.Torrent) error {
t.Lock()
defer t.Unlock()

if _, err := os.Stat(filepath.Join(tm.DataDir, t.InfoHash())); err == nil {
//tm.activeTorrents.Delete(t.InfoHash())
//tm.seedingChan <- t
tm.Seeding(t)
return tm.Seeding(t)
} else {
if err := os.Symlink(
filepath.Join(params.DefaultTmpPath, t.InfoHash()),
filepath.Join(tm.DataDir, t.InfoHash()),
); err == nil {
//tm.activeTorrents.Delete(t.InfoHash())
//tm.seedingChan <- t
tm.Seeding(t)
return tm.Seeding(t)
}
}
return nil
}

/*func (tm *TorrentManager) dur() uint64 {
Expand All @@ -1198,6 +1197,8 @@ func (tm *TorrentManager) activeLoop() {
//timer_2 = time.NewTicker(time.Second * params.QueryTimeInterval * 3600 * 18)
//clean = []*Torrent{}
counter = 0

workers errgroup.Group
)

defer func() {
Expand Down Expand Up @@ -1300,7 +1301,7 @@ func (tm *TorrentManager) activeLoop() {
clean = append(clean, t)
} else {
if t.Torrent.BytesCompleted() < t.BytesRequested() {
t.Leech()
workers.Go(func() error { return t.Leech() })
}
}
}
Expand All @@ -1313,8 +1314,16 @@ func (tm *TorrentManager) activeLoop() {
return true
})

if err := workers.Wait(); err != nil {
log.Warn("Leech error", "err", err)
}

for _, i := range clean {
tm.finish(i)
workers.Go(func() error { return tm.finish(i) })
}

if err := workers.Wait(); err != nil {
log.Warn("Finished error", "err", err)
}

counter++
Expand Down

0 comments on commit e461a92

Please sign in to comment.