From c360a75a6193f5c4effb7690a8f0b558dac92b4a Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Sat, 17 Feb 2024 19:08:23 +0100 Subject: [PATCH] fix(retry): disable retries by default Retrying HTTP requests with a body will cause the entire body to be read into memory. Ref: #343 --- pkg/cmd/factory/c8yclient.go | 35 ++++++++++++++++++++-------------- pkg/config/cliConfiguration.go | 2 +- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pkg/cmd/factory/c8yclient.go b/pkg/cmd/factory/c8yclient.go index 87683cdc6..c23f68d21 100644 --- a/pkg/cmd/factory/c8yclient.go +++ b/pkg/cmd/factory/c8yclient.go @@ -85,22 +85,29 @@ func CreateCumulocityClient(f *cmdutil.Factory, sessionFile, username, password WithCompression(cfg.ShouldUseCompression()), ) - // Use retry client - retryClient := retryablehttp.NewClient() - retryClient.RetryMax = cfg.HTTPRetryMax() - retryClient.RetryWaitMin = cfg.HTTPRetryWaitMin() - retryClient.RetryWaitMax = cfg.HTTPRetryWaitMax() - retryClient.Logger = RetryLogger{l: log} - retryClient.ErrorHandler = func(resp *http.Response, err error, numTries int) (*http.Response, error) { - // Pass error back so that the activity log is processed - log.Warnf("Giving up after %d attempt/s. err=%s", numTries, err) - if resp != nil && resp.Body != nil { - defer resp.Body.Close() + var httpClient *http.Client + if cfg.HTTPRetryMax() > 0 { + // Use retry client + // Note: Don't use the retry client unless we have to as it will the body (if set) + // of outgoing HTTP requests into memory (and it affects the loading as well) + retryClient := retryablehttp.NewClient() + retryClient.RetryMax = cfg.HTTPRetryMax() + retryClient.RetryWaitMin = cfg.HTTPRetryWaitMin() + retryClient.RetryWaitMax = cfg.HTTPRetryWaitMax() + retryClient.Logger = RetryLogger{l: log} + retryClient.ErrorHandler = func(resp *http.Response, err error, numTries int) (*http.Response, error) { + // Pass error back so that the activity log is processed + log.Warnf("Giving up after %d attempt/s. err=%s", numTries, err) + if resp != nil && resp.Body != nil { + defer resp.Body.Close() + } + return resp, err } - return resp, err + retryClient.HTTPClient = internalHttpClient + httpClient = retryClient.StandardClient() + } else { + httpClient = internalHttpClient } - retryClient.HTTPClient = internalHttpClient - httpClient := retryClient.StandardClient() cacheBodyPaths := cfg.CacheBodyKeys() if len(cacheBodyPaths) > 0 { diff --git a/pkg/config/cliConfiguration.go b/pkg/config/cliConfiguration.go index 315759cbc..4de2a469a 100644 --- a/pkg/config/cliConfiguration.go +++ b/pkg/config/cliConfiguration.go @@ -456,7 +456,7 @@ func (c *Config) bindSettings() { // HTTP settings WithBindEnv(SettingsUseCompression, true), - WithBindEnv(SettingsHTTPMaxRetries, 3), + WithBindEnv(SettingsHTTPMaxRetries, 0), WithBindEnv(SettingsHTTPRetryWaitMax, "50s"), WithBindEnv(SettingsHTTPRetryWaitMin, "5s"),