File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed
Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -2,13 +2,14 @@ package download
22
33import (
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
1415type Download struct {
@@ -19,17 +20,25 @@ type Download struct {
1920
2021type Downloader struct {
2122 Downloads []* Download
22- Threads int // TODO
23+ Threads int // Maximum number of concurrent downloads
2324 Retries int
2425}
2526
2627func (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}
You can’t perform that action at this time.
0 commit comments