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
2
2
3
3
import (
4
4
"crypto"
5
+ "log"
6
+ "path/filepath"
7
+ "sync"
8
+
5
9
"github.com/nothub/hashutils/chksum"
6
10
"github.com/nothub/hashutils/encoding"
7
11
modrinth "github.com/nothub/mrpack-install/modrinth/api"
8
12
"github.com/nothub/mrpack-install/web"
9
- "log"
10
- "path/filepath"
11
- "sync"
12
13
)
13
14
14
15
type Download struct {
@@ -19,17 +20,25 @@ type Download struct {
19
20
20
21
type Downloader struct {
21
22
Downloads []* Download
22
- Threads int // TODO
23
+ Threads int // Maximum number of concurrent downloads
23
24
Retries int
24
25
}
25
26
26
27
func (g * Downloader ) Download (baseDir string ) {
28
+ semaphore := make (chan struct {}, g .Threads ) // Create a semaphore to limit concurrency
27
29
var wg sync.WaitGroup
30
+
28
31
for i := range g .Downloads {
29
32
wg .Add (1 )
30
33
dl := g .Downloads [i ]
34
+
35
+ semaphore <- struct {}{} // Acquire a slot in the semaphore
31
36
go func () {
32
- defer wg .Done ()
37
+ defer func () {
38
+ <- semaphore // Release the slot in the semaphore
39
+ wg .Done ()
40
+ }()
41
+
33
42
absPath , _ := filepath .Abs (filepath .Join (baseDir , dl .Path ))
34
43
success := false
35
44
for _ , link := range dl .Urls {
@@ -62,5 +71,6 @@ func (g *Downloader) Download(baseDir string) {
62
71
}
63
72
}()
64
73
}
74
+
65
75
wg .Wait ()
66
76
}
You can’t perform that action at this time.
0 commit comments