Skip to content

add http client timeout option to raw config provider #418

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 9 additions & 5 deletions common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func newBaseClient(signer HTTPRequestSigner, dispatcher HTTPRequestDispatcher) B
return baseClient
}

func defaultHTTPDispatcher() http.Client {
func defaultHTTPDispatcher(timeout time.Duration) http.Client {
var httpClient http.Client
var tp = http.DefaultTransport.(*http.Transport)
if isExpectHeaderDisabled := IsEnvVarFalse(UsingExpectHeaderEnvVar); !isExpectHeaderDisabled {
Expand Down Expand Up @@ -239,21 +239,25 @@ func defaultHTTPDispatcher() http.Client {
tp.TLSClientConfig = &tls.Config{RootCAs: pool}
}
httpClient = http.Client{
Timeout: defaultTimeout,
Timeout: timeout,
Transport: tp,
}
return httpClient
}

func defaultBaseClient(provider KeyProvider) BaseClient {
dispatcher := defaultHTTPDispatcher()
func defaultBaseClient(provider ConfigurationProvider) BaseClient {
timeout := defaultTimeout
if httpConfigProvider, ok := provider.(httpConfigurationProvider); ok {
timeout = httpConfigProvider.HTTPTimeout()
}
dispatcher := defaultHTTPDispatcher(timeout)
signer := DefaultRequestSigner(provider)
return newBaseClient(signer, &dispatcher)
}

// DefaultBaseClientWithSigner creates a default base client with a given signer
func DefaultBaseClientWithSigner(signer HTTPRequestSigner) BaseClient {
dispatcher := defaultHTTPDispatcher()
dispatcher := defaultHTTPDispatcher(defaultTimeout)
return newBaseClient(signer, &dispatcher)
}

Expand Down
36 changes: 34 additions & 2 deletions common/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"path"
"regexp"
"strings"
"time"
)

// AuthenticationType for auth
Expand Down Expand Up @@ -47,6 +48,10 @@ type ConfigurationProvider interface {
AuthType() (AuthConfig, error)
}

type httpConfigurationProvider interface {
HTTPTimeout() time.Duration
}

// IsConfigurationProviderValid Tests all parts of the configuration provider do not return an error, this method will
// not check AuthType(), since authType() is not required to be there.
func IsConfigurationProviderValid(conf ConfigurationProvider) (ok bool, err error) {
Expand Down Expand Up @@ -75,11 +80,34 @@ type rawConfigurationProvider struct {
fingerprint string
privateKey string
privateKeyPassphrase *string
httpTimeout time.Duration
}

// RawConfigurationProviderOption is a raw configuration provider option.
type RawConfigurationProviderOption interface {
apply(*rawConfigurationProvider)
}

type httpTimeoutOption struct {
timeout time.Duration
}

// WithRawConfigProviderHTTPTimeout creates a raw config provider HTTP timeout option.
func WithRawConfigProviderHTTPTimeout(timeout time.Duration) RawConfigurationProviderOption {
return &httpTimeoutOption{timeout}
}

func (o *httpTimeoutOption) apply(p *rawConfigurationProvider) {
p.httpTimeout = o.timeout
}

// NewRawConfigurationProvider will create a ConfigurationProvider with the arguments of the function
func NewRawConfigurationProvider(tenancy, user, region, fingerprint, privateKey string, privateKeyPassphrase *string) ConfigurationProvider {
return rawConfigurationProvider{tenancy, user, region, fingerprint, privateKey, privateKeyPassphrase}
func NewRawConfigurationProvider(tenancy, user, region, fingerprint, privateKey string, privateKeyPassphrase *string, opts ...RawConfigurationProviderOption) ConfigurationProvider {
p := rawConfigurationProvider{tenancy, user, region, fingerprint, privateKey, privateKeyPassphrase, defaultTimeout}
for _, opt := range opts {
opt.apply(&p)
}
return p
}

func (p rawConfigurationProvider) PrivateRSAKey() (key *rsa.PrivateKey, err error) {
Expand Down Expand Up @@ -134,6 +162,10 @@ func (p rawConfigurationProvider) AuthType() (AuthConfig, error) {
return AuthConfig{UnknownAuthenticationType, false, nil}, nil
}

func (p rawConfigurationProvider) HTTPTimeout() time.Duration {
return p.httpTimeout
}

// environmentConfigurationProvider reads configuration from environment variables
type environmentConfigurationProvider struct {
PrivateKeyPassword string
Expand Down
38 changes: 38 additions & 0 deletions common/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"io/ioutil"
"os"
"testing"
"time"

"path"
"strings"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -208,6 +210,42 @@ func TestEnvironmentConfigurationProvider_BadRegion(t *testing.T) {
assert.Error(t, e)
}

func TestHTTPTimeoutRawConfigurationProvider(t *testing.T) {
tests := []struct {
name string
opts []RawConfigurationProviderOption
want time.Duration
}{
{
name: "default",
want: defaultTimeout,
},
{
name: "timeout option",
opts: []RawConfigurationProviderOption{
WithRawConfigProviderHTTPTimeout(time.Hour),
},
want: time.Hour,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewRawConfigurationProvider(
"ocid1.tenancy.oc1..aaaaaaaaxf3fuazos",
"ocid1.user.oc1..aaaaaaaa3p67n2kmpxnbcnff",
"us-ashburn-1",
"af:81:71:8e:d2",
testPrivateKeyConf,
nil,
tt.opts...,
)
httpConfigProvider, ok := c.(httpConfigurationProvider)
require.True(t, ok)
assert.Equal(t, tt.want, httpConfigProvider.HTTPTimeout())
})
}
}

func TestFileConfigurationProvider_parseConfigFileData(t *testing.T) {
data := `[DEFAULT]
user=someuser
Expand Down