mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-15 23:54:47 -04:00
Optimize download speed
This commit is contained in:
parent
18d62273e8
commit
459ad89953
2 changed files with 29 additions and 46 deletions
|
@ -42,13 +42,14 @@ func main() {
|
||||||
logger.Fatal(err.Error())
|
logger.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
|
t.MaxIdleConns = 100
|
||||||
|
t.MaxConnsPerHost = 100
|
||||||
|
t.MaxIdleConnsPerHost = 100
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: &http.Transport{
|
Timeout: time.Duration(120) * time.Second,
|
||||||
MaxIdleConns: 1000,
|
Transport: t,
|
||||||
MaxIdleConnsPerHost: 1000,
|
|
||||||
MaxConnsPerHost: 100,
|
|
||||||
},
|
|
||||||
Timeout: 30 * time.Second,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Connect("activate", func() {
|
app.Connect("activate", func() {
|
||||||
|
|
|
@ -41,32 +41,6 @@ func calculateDownloadSpeed(downloaded int64, startTime, endTime time.Time) int6
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadChunk(buffer []byte, resp *http.Response, file *os.File, doRetries bool, attempt int) (int64, error) {
|
|
||||||
n, err := resp.Body.Read(buffer)
|
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
if doRetries && attempt < maxRetries {
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return 0, fmt.Errorf("download error after %d attempts: %+v", attempt, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = file.Write(buffer[:n])
|
|
||||||
if err != nil {
|
|
||||||
if doRetries && attempt < maxRetries {
|
|
||||||
time.Sleep(retryDelay)
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
return 0, fmt.Errorf("write error after %d attempts: %+v", attempt, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return int64(n), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadFile(ctx context.Context, progressReporter ProgressReporter, client *http.Client, downloadURL, dstPath string, doRetries bool, buffer []byte) error {
|
func downloadFile(ctx context.Context, progressReporter ProgressReporter, client *http.Client, downloadURL, dstPath string, doRetries bool, buffer []byte) error {
|
||||||
filePath := filepath.Base(dstPath)
|
filePath := filepath.Base(dstPath)
|
||||||
|
|
||||||
|
@ -78,6 +52,10 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Header.Set("User-Agent", "WiiUDownloader")
|
||||||
|
req.Header.Set("Connection", "Keep-Alive")
|
||||||
|
req.Header.Set("Accept-Encoding", "")
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -110,30 +88,34 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
n, err := downloadChunk(buffer, resp, file, doRetries, attempt)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if n == 0 {
|
|
||||||
break Loop
|
|
||||||
}
|
|
||||||
|
|
||||||
downloaded += n
|
|
||||||
progressReporter.UpdateDownloadProgress(downloaded, calculateDownloadSpeed(downloaded, startTime, time.Now()), filePath)
|
progressReporter.UpdateDownloadProgress(downloaded, calculateDownloadSpeed(downloaded, startTime, time.Now()), filePath)
|
||||||
default:
|
default:
|
||||||
n, err := downloadChunk(buffer, resp, file, doRetries, attempt)
|
n, err := resp.Body.Read(buffer)
|
||||||
if err != nil {
|
if err != nil && err != io.EOF {
|
||||||
return err
|
if doRetries && attempt < maxRetries {
|
||||||
|
time.Sleep(retryDelay)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return fmt.Errorf("download error after %d attempts: %+v", attempt, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
break Loop
|
break Loop
|
||||||
}
|
}
|
||||||
|
|
||||||
downloaded += n
|
_, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue