Skip to content

Commit 44487b5

Browse files
committed
Fixed issue #45
1 parent 91991df commit 44487b5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

web/download/multi.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package download
22

33
import (
44
"crypto"
5+
"log"
6+
"path/filepath"
7+
"sync"
8+
59
"github.com/nothub/hashutils/chksum"
610
"github.com/nothub/hashutils/encoding"
711
modrinth "github.com/nothub/mrpack-install/modrinth/api"
812
"github.com/nothub/mrpack-install/web"
9-
"log"
10-
"path/filepath"
11-
"sync"
1213
)
1314

1415
type Download struct {
@@ -19,17 +20,25 @@ type Download struct {
1920

2021
type Downloader struct {
2122
Downloads []*Download
22-
Threads int // TODO
23+
Threads int // Maximum number of concurrent downloads
2324
Retries int
2425
}
2526

2627
func (g *Downloader) Download(baseDir string) {
28+
semaphore := make(chan struct{}, g.Threads) // Create a semaphore to limit concurrency
2729
var wg sync.WaitGroup
30+
2831
for i := range g.Downloads {
2932
wg.Add(1)
3033
dl := g.Downloads[i]
34+
35+
semaphore <- struct{}{} // Acquire a slot in the semaphore
3136
go func() {
32-
defer wg.Done()
37+
defer func() {
38+
<-semaphore // Release the slot in the semaphore
39+
wg.Done()
40+
}()
41+
3342
absPath, _ := filepath.Abs(filepath.Join(baseDir, dl.Path))
3443
success := false
3544
for _, link := range dl.Urls {
@@ -62,5 +71,6 @@ func (g *Downloader) Download(baseDir string) {
6271
}
6372
}()
6473
}
74+
6575
wg.Wait()
6676
}

0 commit comments

Comments
 (0)