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

Enable Transport configuration for http client #129

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,10 @@ func WithAuthenticator(authr auth.Authenticator) connOption {
c.Authenticator = authr
}
}

// WithTransport sets up the transport configuration to be used by the httpclient.
func WithTransport(t http.RoundTripper) connOption {
return func(c *config.Config) {
c.Transport = t
}
}
12 changes: 12 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dbsql

import (
"net/http"
"testing"
"time"

Expand All @@ -22,6 +23,7 @@ func TestNewConnector(t *testing.T) {
schema := "schema-string"
userAgentEntry := "user-agent"
sessionParams := map[string]string{"key": "value"}
roundTripper := mockRoundTripper{}
con, err := NewConnector(
WithServerHostname(host),
WithPort(port),
Expand All @@ -33,6 +35,7 @@ func TestNewConnector(t *testing.T) {
WithUserAgentEntry(userAgentEntry),
WithSessionParams(sessionParams),
WithRetries(10, 3*time.Second, 60*time.Second),
WithTransport(roundTripper),
)
expectedUserConfig := config.UserConfig{
Host: host,
Expand All @@ -50,6 +53,7 @@ func TestNewConnector(t *testing.T) {
RetryMax: 10,
RetryWaitMin: 3 * time.Second,
RetryWaitMax: 60 * time.Second,
Transport: roundTripper,
}
expectedCfg := config.WithDefaults()
expectedCfg.DriverVersion = DriverVersion
Expand Down Expand Up @@ -127,3 +131,11 @@ func TestNewConnector(t *testing.T) {
assert.Equal(t, expectedCfg, coni.cfg)
})
}

type mockRoundTripper struct{}

var _ http.RoundTripper = mockRoundTripper{}

func (m mockRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
return &http.Response{StatusCode: 200}, nil
}
18 changes: 14 additions & 4 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func isRetryableServerResponse(resp *http.Response) bool {
}

type Transport struct {
Base *http.Transport
Base http.RoundTripper
Authr auth.Authenticator
trace bool
}
Expand Down Expand Up @@ -396,10 +396,20 @@ func PooledClient(cfg *config.Config) *http.Client {
if cfg.Authenticator == nil {
return nil
}
tr := &Transport{
Base: PooledTransport(),
Authr: cfg.Authenticator,

var tr *Transport
if cfg.Transport != nil {
tr = &Transport{
Base: cfg.Transport,
Authr: cfg.Authenticator,
}
} else {
tr = &Transport{
Base: PooledTransport(),
Authr: cfg.Authenticator,
}
}

return &http.Client{
Transport: tr,
Timeout: cfg.ClientTimeout,
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -97,6 +98,7 @@ type UserConfig struct {
RetryWaitMin time.Duration
RetryWaitMax time.Duration
RetryMax int
Transport http.RoundTripper
}

// DeepCopy returns a true deep copy of UserConfig
Expand Down Expand Up @@ -135,6 +137,7 @@ func (ucfg UserConfig) DeepCopy() UserConfig {
RetryWaitMin: ucfg.RetryWaitMin,
RetryWaitMax: ucfg.RetryWaitMax,
RetryMax: ucfg.RetryMax,
Transport: ucfg.Transport,
}
}

Expand Down