Skip to content

Commit 8022cf7

Browse files
authored
Merge pull request #41 from Code-Hex/fix/somecode
fixed somecode
2 parents b87c593 + 92a235b commit 8022cf7

File tree

3 files changed

+15
-36
lines changed

3 files changed

+15
-36
lines changed

client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func newDownloadClient(maxIdleConnsPerHost int) *http.Client {
1818
tr.MaxIdleConns = 0 // no limit
1919
tr.MaxIdleConnsPerHost = maxIdleConnsPerHost
2020
tr.DisableCompression = true
21-
tr.ForceAttemptHTTP2 = false
2221
return &http.Client{
2322
Transport: tr,
2423
}

download.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,16 @@ type parallelDownloadConfig struct {
189189
func parallelDownload(ctx context.Context, c *parallelDownloadConfig) error {
190190
eg, ctx := errgroup.WithContext(ctx)
191191

192-
eg.Go(func() error {
193-
return progressBar(ctx, c.ContentLength, c.PartialDir)
194-
})
192+
bar := pb.Start64(c.ContentLength).SetWriter(stdout).Set(pb.Bytes, true)
193+
defer bar.Finish()
194+
195+
// check file size already downloaded for resume
196+
size, err := checkProgress(c.PartialDir)
197+
if err != nil {
198+
return errors.Wrap(err, "failed to get directory size")
199+
}
200+
201+
bar.SetCurrent(size)
195202

196203
for _, task := range c.Tasks {
197204
task := task
@@ -200,14 +207,14 @@ func parallelDownload(ctx context.Context, c *parallelDownloadConfig) error {
200207
if err != nil {
201208
return err
202209
}
203-
return task.download(req)
210+
return task.download(req, bar)
204211
})
205212
}
206213

207214
return eg.Wait()
208215
}
209216

210-
func (t *task) download(req *http.Request) error {
217+
func (t *task) download(req *http.Request, bar *pb.ProgressBar) error {
211218
resp, err := t.Client.Do(req)
212219
if err != nil {
213220
return errors.Wrapf(err, "failed to get response: %q", t.String())
@@ -220,7 +227,9 @@ func (t *task) download(req *http.Request) error {
220227
}
221228
defer output.Close()
222229

223-
if _, err := io.Copy(output, resp.Body); err != nil {
230+
rd := bar.NewProxyReader(resp.Body)
231+
232+
if _, err := io.Copy(output, rd); err != nil {
224233
return errors.Wrapf(err, "failed to write response body: %q", t.String())
225234
}
226235

util.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package pget
22

33
import (
4-
"context"
54
"fmt"
65
"os"
76
"path/filepath"
8-
"time"
9-
10-
"github.com/cheggaaa/pb/v3"
11-
"github.com/pkg/errors"
127
)
138

149
func getPartialDirname(targetDir, filename string, procs int) string {
@@ -52,27 +47,3 @@ func makeRange(i, procs int, rangeSize, contentLength int64) Range {
5247
func (r Range) BytesRange() string {
5348
return fmt.Sprintf("bytes=%d-%d", r.low, r.high)
5449
}
55-
56-
func progressBar(ctx context.Context, contentLength int64, dirname string) error {
57-
bar := pb.Start64(contentLength).SetWriter(stdout).Set(pb.Bytes, true)
58-
defer bar.Finish()
59-
60-
for {
61-
select {
62-
case <-ctx.Done():
63-
return nil
64-
case <-time.After(100 * time.Millisecond): // To save cpu resource
65-
size, err := checkProgress(dirname)
66-
if err != nil {
67-
return errors.Wrap(err, "failed to get directory size")
68-
}
69-
70-
if size < contentLength {
71-
bar.SetCurrent(size)
72-
} else {
73-
bar.SetCurrent(contentLength)
74-
return nil
75-
}
76-
}
77-
}
78-
}

0 commit comments

Comments
 (0)