Skip to content
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

Populate default retry configuration options #231

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion client_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ type RetryConfiguration struct {
// Default: 1000 milliseconds
RetryWaitMin time.Duration `env:"VAULT_RETRY_WAIT_MIN"`

// MaxRetryWait controls the maximum time to wait before retrying when
// RetryWaitMax controls the maximum time to wait before retrying when
// a 5xx or 412 error occurs.
// Default: 1500 milliseconds
RetryWaitMax time.Duration `env:"VAULT_RETRY_WAIT_MAX"`
Expand Down
24 changes: 20 additions & 4 deletions client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,34 @@ func WithRequestTimeout(timeout time.Duration) ClientOption {
}

// WithTLS configures the TLS settings in the base http.Client.
func WithTLS(configuration TLSConfiguration) ClientOption {
func WithTLS(tls TLSConfiguration) ClientOption {
return func(c *ClientConfiguration) error {
c.TLS = configuration
c.TLS = tls
return nil
}
}

// WithRetryConfiguration configures the internal go-retryablehttp client.
// The library sets reasonable defaults for this setting.
func WithRetryConfiguration(configuration RetryConfiguration) ClientOption {
func WithRetryConfiguration(retry RetryConfiguration) ClientOption {
return func(c *ClientConfiguration) error {
c.RetryConfiguration = configuration
// if any of the required RetryConfiguration values are missing, use defaults
defaultRetryConfiguration := DefaultConfiguration().RetryConfiguration

if retry.CheckRetry == nil {
retry.CheckRetry = defaultRetryConfiguration.CheckRetry
}

if retry.Backoff == nil {
retry.Backoff = defaultRetryConfiguration.Backoff
}

if retry.ErrorHandler == nil {
retry.ErrorHandler = defaultRetryConfiguration.ErrorHandler
}

c.RetryConfiguration = retry

return nil
}
}
Expand Down
13 changes: 12 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package vault

import (
"fmt"
"testing"
"time"

Expand All @@ -29,7 +30,7 @@ func Test_Client_Clone(t *testing.T) {

clone := client.Clone()

assert.Equal(t, client, clone)
assertEqual(t, client, clone)

assert.Equal(t, "http://test", clone.Configuration().Address)
assert.Equal(t, 30*time.Second, clone.Configuration().RequestTimeout)
Expand All @@ -38,3 +39,13 @@ func Test_Client_Clone(t *testing.T) {
assert.Equal(t, "test-token", clone.clientRequestModifiers.headers.token)
assert.Equal(t, "test-namespace", clone.clientRequestModifiers.headers.namespace)
}

// assertEqual compares the two clients, accounting for nested func pointers
averche marked this conversation as resolved.
Show resolved Hide resolved
func assertEqual(t *testing.T, c1 *Client, c2 *Client) {
assert.Equal(t, fmt.Sprintf("%v", c1.configuration), fmt.Sprintf("%v", c2.configuration))
assert.Equal(t, fmt.Sprintf("%v", c1.parsedBaseAddress), fmt.Sprintf("%v", c2.parsedBaseAddress))
assert.Equal(t, fmt.Sprintf("%v", c1.client), fmt.Sprintf("%v", c2.client))
assert.Equal(t, fmt.Sprintf("%v", c1.clientWithRetries), fmt.Sprintf("%v", c2.clientWithRetries))
assert.Equal(t, fmt.Sprintf("%v", c1.clientRequestModifiers), fmt.Sprintf("%v", c2.clientRequestModifiers))
assert.Equal(t, fmt.Sprintf("%v", c1.replicationStates.states), fmt.Sprintf("%v", c2.replicationStates.states))
}