Skip to content

Commit ad4c3cd

Browse files
authored
Add wait between retries when there are download errors (#65)
Currently gvm makes three attempts at retrying a failed download, but it doesn't wait in between. This commit adds rudimentary wait time between downloads. In a future refactoring we could use `go-retryablehttp` (we'd still need to guard for errors with `io.Copy`) but keeping the improvement short for now, hoping to reduce download related CI failures.
1 parent 46b74d3 commit ad4c3cd

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1616

1717
- Updated releases to use Go 1.18. [#54](https://github.com/andrewkroh/gvm/pull/54)
1818
- Report Go module version from `gvm --version` if installed via `go install`. [#57](https://github.com/andrewkroh/gvm/pull/57)
19+
- Add wait between retries when there are download errors. [#65](https://github.com/andrewkroh/gvm/pull/65)
1920

2021
### Fixed
2122

binrepo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (m *Manager) installBinary(version *GoVersion) (string, error) {
2828
}
2929

3030
goURL := fmt.Sprintf("%s/go%v.%v-%v.%v", m.GoStorageHome, version, m.GOOS, m.GOARCH, extension)
31-
path, err := common.DownloadFile(goURL, tmp, m.HTTPTimeout)
31+
path, err := common.DownloadFile(goURL, tmp, m.HTTPTimeout, common.DefaultRetryParams)
3232
if err != nil {
3333
return "", fmt.Errorf("failed downloading from %v: %w", goURL, err)
3434
}

common/common.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,27 @@ var log = logrus.WithField("package", "common")
1818
// ErrNotFound is returned when the download fails due to HTTP 404 Not Found.
1919
var ErrNotFound = errors.New("not found")
2020

21-
func DownloadFile(url, destinationDir string, httpTimeout time.Duration) (string, error) {
21+
type retryParams struct {
22+
maxRetries int
23+
retryDelay time.Duration
24+
}
25+
26+
var DefaultRetryParams = retryParams{
27+
maxRetries: 5,
28+
retryDelay: 10 * time.Second,
29+
}
30+
31+
func DownloadFile(url, destinationDir string, httpTimeout time.Duration, r retryParams) (string, error) {
2232
log.WithField("url", url).Debug("Downloading file")
2333
var name string
2434
var err error
2535
var retry bool
26-
for a := 1; a <= 3; a++ {
36+
37+
for a := 1; a <= r.maxRetries; a++ {
2738
name, retry, err = downloadFile(url, destinationDir, httpTimeout)
2839
if err != nil && retry {
29-
log.WithError(err).Debugf("Download attempt %d failed", a)
40+
log.WithError(err).Debugf("Download attempt %d/%d failed, retrying in %s", a, r.maxRetries, r.retryDelay)
41+
time.Sleep(r.retryDelay)
3042
continue
3143
}
3244
break

0 commit comments

Comments
 (0)