Skip to content

Commit

Permalink
Populate default retry configuration options (#231)
Browse files Browse the repository at this point in the history
* Populate default retry configuration options

* assertEqual

* better comment

* .
  • Loading branch information
averche authored Sep 13, 2023
1 parent 1bad0b3 commit ff66746
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
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
14 changes: 13 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,14 @@ 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 the nested func pointers,
// which are not comparable in Go (https://go.dev/ref/spec#Comparison_operators).
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))
}

0 comments on commit ff66746

Please sign in to comment.