From 44487b590f2e2409317688a152716028ed8e40cb Mon Sep 17 00:00:00 2001 From: Mohamed Tawous Date: Sun, 5 May 2024 04:40:04 -0400 Subject: [PATCH] Fixed issue #45 --- web/download/multi.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/web/download/multi.go b/web/download/multi.go index 3c6750e..a584829 100644 --- a/web/download/multi.go +++ b/web/download/multi.go @@ -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 { @@ -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 { @@ -62,5 +71,6 @@ func (g *Downloader) Download(baseDir string) { } }() } + wg.Wait() }