Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #45 #46

Merged
merged 1 commit into from
May 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}