From f8712b234c0a83ef7e1dad5a3fff554f814477c0 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Wed, 31 Jan 2024 23:21:23 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20always=20use=20retry=20http=20cl?= =?UTF-8?q?ient=20and=20set=20sensible=20timout=20defaults=20(#3171)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🧹 always use retry http client and set sensible timout defaults * proxy update * 🧹 remove client timeout --- providers/providers.go | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/providers/providers.go b/providers/providers.go index 2127dc63aa..2d7f25b024 100644 --- a/providers/providers.go +++ b/providers/providers.go @@ -7,7 +7,9 @@ import ( "archive/tar" "encoding/json" "io" + "net" "net/http" + "net/url" "os" osfs "os" "path/filepath" @@ -25,7 +27,6 @@ import ( "go.mondoo.com/cnquery/v10/cli/config" "go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin" "go.mondoo.com/cnquery/v10/providers-sdk/v1/resources" - "go.mondoo.com/ranger-rpc" "golang.org/x/exp/slices" ) @@ -152,26 +153,40 @@ type Provider struct { HasBinary bool } -func httpClient() (*http.Client, error) { - proxy, err := config.GetAPIProxy() - if err != nil { - log.Fatal().Err(err).Msg("could not parse proxy URL") - } - return ranger.NewHttpClient(ranger.WithProxy(proxy)), nil -} +var ( + defaultHttpTimeout = 30 * time.Second + defaultIdleConnTimeout = 30 * time.Second + defaultTLSHandshakeTimeout = 10 * time.Second +) func httpClientWithRetry() (*http.Client, error) { + var proxyFn func(*http.Request) (*url.URL, error) + proxy, err := config.GetAPIProxy() if err != nil { log.Fatal().Err(err).Msg("could not parse proxy URL") } + + if proxy != nil { + proxyFn = http.ProxyURL(proxy) + } + retryClient := retryablehttp.NewClient() retryClient.RetryMax = 3 retryClient.Logger = &ZerologAdapter{logger: log.Logger} retryClient.HTTPClient = &http.Client{ Transport: &http.Transport{ - Proxy: http.ProxyURL(proxy), + Proxy: proxyFn, + DialContext: (&net.Dialer{ + Timeout: defaultHttpTimeout, + KeepAlive: 30 * time.Second, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: defaultIdleConnTimeout, + TLSHandshakeTimeout: defaultTLSHandshakeTimeout, + ExpectContinueTimeout: 1 * time.Second, }, + Timeout: defaultHttpTimeout, } return retryClient.StandardClient(), nil } @@ -371,7 +386,7 @@ func installVersion(name string, version string) (*Provider, error) { url = strings.ReplaceAll(url, "{ARCH}", runtime.GOARCH) log.Debug().Str("url", url).Msg("installing provider from URL") - client, err := httpClient() + client, err := httpClientWithRetry() if err != nil { return nil, err } @@ -416,7 +431,6 @@ func LatestVersion(name string) (string, error) { if err != nil { return "", err } - client.Timeout = time.Duration(5 * time.Second) res, err := client.Get("https://releases.mondoo.com/providers/latest.json") if err != nil {