Skip to content

Commit

Permalink
✨ add retry to providers install (#2781)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuskimmina authored Jan 2, 2024
1 parent d5114c5 commit 4e0dfae
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.8 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
Expand Down
53 changes: 52 additions & 1 deletion providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"time"

"github.com/cockroachdb/errors"
"github.com/hashicorp/go-retryablehttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"github.com/ulikunitz/xz"
Expand Down Expand Up @@ -135,6 +137,22 @@ func httpClient() (*http.Client, error) {
return ranger.NewHttpClient(ranger.WithProxy(proxy)), nil
}

func httpClientWithRetry() (*http.Client, error) {
proxy, err := config.GetAPIProxy()
if err != nil {
log.Fatal().Err(err).Msg("could not parse proxy URL")
}
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3
retryClient.Logger = &ZerologAdapter{logger: log.Logger}
retryClient.HTTPClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),
},
}
return retryClient.StandardClient(), nil
}

// List providers that are going to be used in their default order:
// builtin > user > system. The providers are also loaded and provider their
// metadata/configuration.
Expand Down Expand Up @@ -353,7 +371,7 @@ func installVersion(name string, version string) (*Provider, error) {
}

func LatestVersion(name string) (string, error) {
client, err := httpClient()
client, err := httpClientWithRetry()
if err != nil {
return "", err
}
Expand All @@ -363,6 +381,7 @@ func LatestVersion(name string) (string, error) {
if err != nil {
return "", err
}

data, err := io.ReadAll(res.Body)
if err != nil {
log.Debug().Err(err).Msg("reading latest.json failed")
Expand Down Expand Up @@ -710,3 +729,35 @@ func MustLoadSchemaFromFile(name string, path string) *resources.Schema {
}
return MustLoadSchema(name, raw)
}

// ZerologAdapter adapts the zerolog logger to the LeveledLogger interface.
// Converts all retry logs to debug logs
type ZerologAdapter struct {
logger zerolog.Logger
}

func (z *ZerologAdapter) Error(msg string, keysAndValues ...interface{}) {
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg)
}

func (z *ZerologAdapter) Info(msg string, keysAndValues ...interface{}) {
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg)
}

func (z *ZerologAdapter) Debug(msg string, keysAndValues ...interface{}) {
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg)
}

func (z *ZerologAdapter) Warn(msg string, keysAndValues ...interface{}) {
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg)
}

func convertToFields(keysAndValues ...interface{}) map[string]interface{} {
fields := make(map[string]interface{})
for i := 0; i < len(keysAndValues); i += 2 {
if i+1 < len(keysAndValues) {
fields[keysAndValues[i].(string)] = keysAndValues[i+1]
}
}
return fields
}

0 comments on commit 4e0dfae

Please sign in to comment.