multithread
This commit is contained in:
72
util.go
72
util.go
@@ -9,6 +9,7 @@ import (
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
@@ -135,28 +136,61 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
// start downloading chunks
|
||||
ret := make([]byte, manifest.Size)
|
||||
totalMB := float32(manifest.Size) / 1024.0 / 1024.0
|
||||
for _, fragment := range manifest.Fragments {
|
||||
url := transformShaToURL(fragment.Sha512, fragment.Size)
|
||||
content, err := getContent(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
downloaded += len(content) * len(fragment.Offsets)
|
||||
for _, offset := range fragment.Offsets {
|
||||
trackFrag = append(trackFrag, Frag{Offset: offset, Size: len(content)})
|
||||
copy(ret[offset:offset+len(content)], content)
|
||||
}
|
||||
|
||||
if Debug {
|
||||
// save fragment to file in the fragments/ directory
|
||||
if err := os.WriteFile(filepath.Join(downloadDir, "fragments", filen, fmt.Sprintf("%d.%s.%d.bytes", fragment.Offsets[0], fragment.Sha512, fragment.Size)), content, os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
// A channel to signal the main goroutine to check for errors
|
||||
doneChan := make(chan bool)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(DownThreads)
|
||||
var mu sync.Mutex
|
||||
|
||||
fragChan := make(chan Fragment, DownThreads)
|
||||
errChan := make(chan error, DownThreads)
|
||||
|
||||
// Start worker threads
|
||||
for i := 0; i < DownThreads; i++ {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for fragment := range fragChan {
|
||||
url := transformShaToURL(fragment.Sha512, fragment.Size)
|
||||
content, err := getContent(url)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
downloaded += len(content) * len(fragment.Offsets)
|
||||
for _, offset := range fragment.Offsets {
|
||||
trackFrag = append(trackFrag, Frag{Offset: offset, Size: len(content)})
|
||||
copy(ret[offset:offset+len(content)], content)
|
||||
}
|
||||
percent := float32(downloaded) / float32(manifest.Size) * 100.0
|
||||
downedMB := float32(downloaded) / 1024.0 / 1024.0
|
||||
fmt.Printf("\r\033[32mDownloaded\033[0m %fMB/%fMB (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downedMB, totalMB, percent, filen)
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
percent := float32(downloaded) / float32(manifest.Size) * 100.0
|
||||
downedMB := float32(downloaded) / 1024.0 / 1024.0
|
||||
fmt.Printf("\r\033[32mDownloaded\033[0m %fMB/%fMB (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downedMB, totalMB, percent, filen)
|
||||
// A separate goroutine to wait for all workers to finish and then close the error channel
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(doneChan)
|
||||
}()
|
||||
|
||||
// Send fragments to workers
|
||||
for _, fragment := range manifest.Fragments {
|
||||
fragChan <- fragment
|
||||
}
|
||||
close(fragChan)
|
||||
|
||||
// Wait for workers to finish or an error to occur
|
||||
select {
|
||||
case <-doneChan:
|
||||
// All workers finished successfully
|
||||
case err := <-errChan:
|
||||
// An error occurred
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// sort trackFrag by Offset
|
||||
|
||||
Reference in New Issue
Block a user