Download The Downloader makes multi-thread http requests to remote URL, each chunk(except last one) has PartSize, cache some data, then return Reader with assembled data Supports range, do not support unknown FileSize, and will fail if FileSize is incorrect memory usage is at about Concurrency*PartS
(ctx context.Context, p *HttpRequestParams)
| 73 | // Supports range, do not support unknown FileSize, and will fail if FileSize is incorrect |
| 74 | // memory usage is at about Concurrency*PartSize, use this wisely |
| 75 | func (d Downloader) Download(ctx context.Context, p *HttpRequestParams) (readCloser io.ReadCloser, err error) { |
| 76 | |
| 77 | var finalP HttpRequestParams |
| 78 | awsutil.Copy(&finalP, p) |
| 79 | if finalP.Range.Length < 0 || finalP.Range.Start+finalP.Range.Length > finalP.Size { |
| 80 | finalP.Range.Length = finalP.Size - finalP.Range.Start |
| 81 | } |
| 82 | impl := downloader{params: &finalP, cfg: d, ctx: ctx} |
| 83 | |
| 84 | // Ensures we don't need nil checks later on |
| 85 | // 必需的选项 |
| 86 | if impl.cfg.Concurrency == 0 { |
| 87 | impl.cfg.Concurrency = DefaultDownloadConcurrency |
| 88 | } |
| 89 | if impl.cfg.PartSize == 0 { |
| 90 | impl.cfg.PartSize = DefaultDownloadPartSize |
| 91 | } |
| 92 | if conf.MinFreeMemory > 0 && impl.cfg.PartSize > int(conf.MaxBlockLimit) { |
| 93 | impl.cfg.PartSize = int(conf.MaxBlockLimit) |
| 94 | } |
| 95 | if impl.cfg.HttpClient == nil { |
| 96 | impl.cfg.HttpClient = DefaultHttpRequestFunc |
| 97 | } |
| 98 | |
| 99 | return impl.download() |
| 100 | } |
| 101 | |
| 102 | // downloader is the implementation structure used internally by Downloader. |
| 103 | type downloader struct { |