mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-24 20:14:40 -04:00
Move progress reporting to its own goroutine
This commit is contained in:
parent
6b8713a09b
commit
99ea73d98d
1 changed files with 35 additions and 26 deletions
|
@ -51,7 +51,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
||||||
|
|
||||||
req.Header.Set("User-Agent", "WiiUDownloader")
|
req.Header.Set("User-Agent", "WiiUDownloader")
|
||||||
req.Header.Set("Connection", "Keep-Alive")
|
req.Header.Set("Connection", "Keep-Alive")
|
||||||
req.Header.Set("Accept-Encoding", "gzip")
|
req.Header.Set("Accept-Encoding", "")
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -79,35 +79,44 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
||||||
ticker := time.NewTicker(250 * time.Millisecond)
|
ticker := time.NewTicker(250 * time.Millisecond)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for range ticker.C {
|
||||||
|
if progressReporter.Cancelled() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
progressReporter.UpdateDownloadProgress(downloaded, calculateDownloadSpeed(downloaded, startTime, time.Now()), filePath)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
Loop:
|
||||||
for {
|
for {
|
||||||
n, err := resp.Body.Read(buffer)
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
if doRetries && attempt < maxRetries {
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return fmt.Errorf("download error after %d attempts: %+v", attempt, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = file.Write(buffer[:n])
|
|
||||||
if err != nil {
|
|
||||||
if doRetries && attempt < maxRetries {
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return fmt.Errorf("write error after %d attempts: %+v", attempt, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
downloaded += int64(n)
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
case <-ticker.C:
|
default:
|
||||||
progressReporter.UpdateDownloadProgress(downloaded, calculateDownloadSpeed(downloaded, startTime, time.Now()), filePath)
|
n, err := resp.Body.Read(buffer)
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
if doRetries && attempt < maxRetries {
|
||||||
|
time.Sleep(retryDelay)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return fmt.Errorf("download error after %d attempts: %+v", attempt, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n == 0 {
|
||||||
|
break Loop
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = file.Write(buffer[:n])
|
||||||
|
if err != nil {
|
||||||
|
if doRetries && attempt < maxRetries {
|
||||||
|
time.Sleep(retryDelay)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return fmt.Errorf("write error after %d attempts: %+v", attempt, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
downloaded += int64(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue