Skip to content

Add --http-timeout flag #45

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

Merged
merged 1 commit into from
Jan 14, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Added

- Add `--http-timeout` flag to control the timeout for HTTP requests. [#43](https://github.com/andrewkroh/gvm/issues/43) [#45](https://github.com/andrewkroh/gvm/pull/45)

## [0.3.2]

### Added
Expand Down
10 changes: 6 additions & 4 deletions binrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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)
path, err := common.DownloadFile(goURL, tmp, m.HTTPTimeout)
if err != nil {
return "", fmt.Errorf("failed downloading from %v: %w", goURL, err)
}
Expand All @@ -44,7 +44,7 @@ func (m *Manager) AvailableBinaries() ([]*GoVersion, error) {
home, goos, goarch := m.GoStorageHome, m.GOOS, m.GOARCH

versions := map[string]struct{}{}
err := iterXMLDirListing(home, func(name string) bool {
err := m.iterXMLDirListing(home, func(name string) bool {
matches := reGostoreVersion.FindStringSubmatch(name)
if len(matches) < 4 {
return true
Expand Down Expand Up @@ -76,9 +76,11 @@ func (m *Manager) AvailableBinaries() ([]*GoVersion, error) {
return list, nil
}

func iterXMLDirListing(home string, fn func(entry string) bool) error {
func (m *Manager) iterXMLDirListing(home string, fn func(entry string) bool) error {
marker := ""
client := &http.Client{}
client := &http.Client{
Timeout: m.HTTPTimeout,
}

for {
type contents struct {
Expand Down
4 changes: 3 additions & 1 deletion cmd/gvm/gvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ var (
type commandFactory func(*kingpin.CmdClause) func(*gvm.Manager) error

func main() {

app := kingpin.New("gvm", usage)
debug := app.Flag("debug", "Enable debug logging to stderr.").Short('d').Bool()

Expand All @@ -53,6 +52,7 @@ func main() {
app.Flag("home", "GVM home directory.").StringVar(&manager.Home)
app.Flag("url", "Go binaries repository base URL.").StringVar(&manager.GoStorageHome)
app.Flag("repository", "Go upstream git repository.").StringVar(&manager.GoSourceURL)
app.Flag("http-timeout", "Timeout for HTTP requests.").Default("3m").DurationVar(&manager.HTTPTimeout)

command(useCommand, "use", "prepare go version and print environment variables").
Default()
Expand Down Expand Up @@ -85,6 +85,8 @@ func main() {
os.Exit(1)
}

logrus.Debug("GVM version: ", version)

action, exists := commands[selCommand]
if !exists {
app.Errorf("unknown command: %v", selCommand)
Expand Down
8 changes: 4 additions & 4 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ 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) (string, error) {
func DownloadFile(url, destinationDir string, httpTimeout time.Duration) (string, error) {
log.WithField("url", url).Debug("Downloading file")
var name string
var err error
var retry bool
for a := 1; a <= 3; a++ {
name, err, retry = downloadFile(url, destinationDir)
name, err, retry = downloadFile(url, destinationDir, httpTimeout)
if err != nil && retry {
log.WithError(err).Debugf("Download attempt %d failed", a)
continue
Expand All @@ -34,9 +34,9 @@ func DownloadFile(url, destinationDir string) (string, error) {
return name, err
}

func downloadFile(url, destinationDir string) (path string, err error, retryable bool) {
func downloadFile(url, destinationDir string, httpTimeout time.Duration) (path string, err error, retryable bool) {
client := http.Client{
Timeout: time.Duration(3 * time.Minute),
Timeout: httpTimeout,
}
resp, err := client.Get(url)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions gvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"sort"
"strings"
"time"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -53,6 +54,8 @@ type Manager struct {
// Defaults to https://go.googlesource.com/go
GoSourceURL string

HTTPTimeout time.Duration

Logger logrus.FieldLogger

cacheDir string
Expand All @@ -78,6 +81,10 @@ func (m *Manager) Init() error {
m.GoSourceURL = "https://go.googlesource.com/go"
}

if m.HTTPTimeout == 0 {
m.HTTPTimeout = 3 * time.Minute
}

if m.GOOS == "" {
m.GOOS = runtime.GOOS
}
Expand Down