diff options
Diffstat (limited to 'client/httpclient.go')
-rw-r--r-- | client/httpclient.go | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/client/httpclient.go b/client/httpclient.go index fe6e2e5..82b4b40 100644 --- a/client/httpclient.go +++ b/client/httpclient.go @@ -24,6 +24,9 @@ const ( httpProtocol = "http://" httpsProtocol = "https://" defaultReadTimeoutSeconds = 300 + defaultRetryWaitMin = time.Second + defaultRetryWaitMax = 15 * time.Second + defaultRetryMax = 2 ) var ErrCafileNotFound = errors.New("cafile not found") @@ -35,8 +38,10 @@ type HttpClient struct { } type httpClientCfg struct { - keyPath, certPath string - caFile, caPath string + keyPath, certPath string + caFile, caPath string + retryWaitMin, retryWaitMax time.Duration + retryMax int } func (hcc httpClientCfg) HaveCertAndKey() bool { return hcc.keyPath != "" && hcc.certPath != "" } @@ -53,6 +58,14 @@ func WithClientCert(certPath, keyPath string) HTTPClientOpt { } } +func WithHTTPRetryOpts(waitMin, waitMax time.Duration, maxAttempts int) HTTPClientOpt { + return func(hcc *httpClientCfg) { + hcc.retryWaitMin = waitMin + hcc.retryWaitMax = waitMax + hcc.retryMax = maxAttempts + } +} + func validateCaFile(filename string) error { if filename == "" { return nil @@ -71,6 +84,18 @@ func validateCaFile(filename string) error { // NewHTTPClientWithOpts builds an HTTP client using the provided options func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath string, readTimeoutSeconds uint64, opts []HTTPClientOpt) (*HttpClient, error) { + hcc := &httpClientCfg{ + caFile: caFile, + caPath: caPath, + retryWaitMin: defaultRetryWaitMin, + retryWaitMax: defaultRetryWaitMax, + retryMax: defaultRetryMax, + } + + for _, opt := range opts { + opt(hcc) + } + var transport *http.Transport var host string var err error @@ -84,15 +109,6 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri return nil, err } - hcc := &httpClientCfg{ - caFile: caFile, - caPath: caPath, - } - - for _, opt := range opts { - opt(hcc) - } - transport, host, err = buildHttpsTransport(*hcc, gitlabURL) if err != nil { return nil, err @@ -110,8 +126,9 @@ func NewHTTPClientWithOpts(gitlabURL, gitlabRelativeURLRoot, caFile, caPath stri if os.Getenv("FF_GITLAB_SHELL_RETRYABLE_HTTP") == "1" { c := retryablehttp.NewClient() - c.RetryMax = 2 - c.RetryWaitMax = 15 * time.Second + c.RetryMax = hcc.retryMax + c.RetryWaitMax = hcc.retryWaitMax + c.RetryWaitMin = hcc.retryWaitMin c.Logger = nil c.HTTPClient.Transport = correlation.NewInstrumentedRoundTripper(tracing.NewRoundTripper(transport)) c.HTTPClient.Timeout = readTimeout(readTimeoutSeconds) |