Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait between retries when there are download errors #65

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

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

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion binrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (m *Manager) installBinary(version *GoVersion) (string, error) {
}

goURL := fmt.Sprintf("%s/go%v.%v-%v.%v", m.GoStorageHome, version, m.GOOS, m.GOARCH, extension)
path, err := common.DownloadFile(goURL, tmp, m.HTTPTimeout)
path, err := common.DownloadFile(goURL, tmp, m.HTTPTimeout, common.DefaultRetryParams)
if err != nil {
return "", fmt.Errorf("failed downloading from %v: %w", goURL, err)
}
Expand Down
18 changes: 15 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ var log = logrus.WithField("package", "common")
// ErrNotFound is returned when the download fails due to HTTP 404 Not Found.
var ErrNotFound = errors.New("not found")

func DownloadFile(url, destinationDir string, httpTimeout time.Duration) (string, error) {
type retryParams struct {
maxRetries int
retryDelay time.Duration
}

var DefaultRetryParams = retryParams{
maxRetries: 5,
retryDelay: 10 * time.Second,
}

func DownloadFile(url, destinationDir string, httpTimeout time.Duration, r retryParams) (string, error) {
log.WithField("url", url).Debug("Downloading file")
var name string
var err error
var retry bool
for a := 1; a <= 3; a++ {

for a := 1; a <= r.maxRetries; a++ {
name, retry, err = downloadFile(url, destinationDir, httpTimeout)
if err != nil && retry {
log.WithError(err).Debugf("Download attempt %d failed", a)
log.WithError(err).Debugf("Download attempt %d/%d failed, retrying in %s", a, r.maxRetries, r.retryDelay)
time.Sleep(r.retryDelay)
continue
}
break
Expand Down
Loading