Skip to content

Commit

Permalink
Merge pull request #46 from mmtawous/main
Browse files Browse the repository at this point in the history
Fixed issue #45 by mmtawous
  • Loading branch information
nothub authored May 5, 2024
2 parents ce06ea7 + 44487b5 commit 20549fc
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions web/download/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package download

import (
"crypto"
"log"
"path/filepath"
"sync"

"github.com/nothub/hashutils/chksum"
"github.com/nothub/hashutils/encoding"
modrinth "github.com/nothub/mrpack-install/modrinth/api"
"github.com/nothub/mrpack-install/web"
"log"
"path/filepath"
"sync"
)

type Download struct {
Expand All @@ -19,17 +20,25 @@ type Download struct {

type Downloader struct {
Downloads []*Download
Threads int // TODO
Threads int // Maximum number of concurrent downloads
Retries int
}

func (g *Downloader) Download(baseDir string) {
semaphore := make(chan struct{}, g.Threads) // Create a semaphore to limit concurrency
var wg sync.WaitGroup

for i := range g.Downloads {
wg.Add(1)
dl := g.Downloads[i]

semaphore <- struct{}{} // Acquire a slot in the semaphore
go func() {
defer wg.Done()
defer func() {
<-semaphore // Release the slot in the semaphore
wg.Done()
}()

absPath, _ := filepath.Abs(filepath.Join(baseDir, dl.Path))
success := false
for _, link := range dl.Urls {
Expand Down Expand Up @@ -62,5 +71,6 @@ func (g *Downloader) Download(baseDir string) {
}
}()
}

wg.Wait()
}

0 comments on commit 20549fc

Please sign in to comment.