DownloadWithChecksum starts new download task with checksum verification
(ctx context.Context, url string, destination string, expected *utils.ChecksumInfo, ignoreMismatch bool)
| 174 | |
| 175 | // DownloadWithChecksum starts new download task with checksum verification |
| 176 | func (downloader *downloaderImpl) DownloadWithChecksum(ctx context.Context, url string, destination string, |
| 177 | expected *utils.ChecksumInfo, ignoreMismatch bool) error { |
| 178 | |
| 179 | if downloader.progress != nil { |
| 180 | downloader.progress.Printf("Downloading: %s\n", url) |
| 181 | defer downloader.progress.Flush() |
| 182 | } |
| 183 | req, err := downloader.newRequest(ctx, "GET", url) |
| 184 | |
| 185 | var temppath string |
| 186 | maxTries := downloader.maxTries |
| 187 | const delayMax = time.Duration(5 * time.Minute) |
| 188 | delay := time.Duration(1 * time.Second) |
| 189 | const delayMultiplier = 2 |
| 190 | for maxTries > 0 { |
| 191 | temppath, err = downloader.download(req, url, destination, expected, ignoreMismatch) |
| 192 | |
| 193 | if err != nil { |
| 194 | if retryableError(err) { |
| 195 | if downloader.progress != nil { |
| 196 | downloader.progress.Printf("Error (retrying): %s\n", err) |
| 197 | } |
| 198 | maxTries-- |
| 199 | time.Sleep(delay) |
| 200 | // Sleep exponentially at the next retry, but no longer than delayMax |
| 201 | delay *= delayMultiplier |
| 202 | if delay > delayMax { |
| 203 | delay = delayMax |
| 204 | } |
| 205 | } else { |
| 206 | if downloader.progress != nil { |
| 207 | downloader.progress.Printf("Error: %s \n", err) |
| 208 | } |
| 209 | break |
| 210 | } |
| 211 | } else { |
| 212 | // get out of the loop |
| 213 | break |
| 214 | } |
| 215 | if downloader.progress != nil { |
| 216 | downloader.progress.Printf("Retrying %d %s...\n", maxTries, url) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // still an error after retrying, giving up |
| 221 | if err != nil { |
| 222 | if downloader.progress != nil { |
| 223 | downloader.progress.Printf("Download Error: %s\n", url) |
| 224 | } |
| 225 | return err |
| 226 | } |
| 227 | |
| 228 | err = os.Rename(temppath, destination) |
| 229 | if err != nil { |
| 230 | _ = os.Remove(temppath) |
| 231 | return errors.Wrap(err, url) |
| 232 | } |
| 233 |
no test coverage detected