Skip to content

Commit

Permalink
Http proxy fix (#1562)
Browse files Browse the repository at this point in the history
* fix http client to use proxy from environment

* add unit tests

* lint fix
  • Loading branch information
ashmeenkaur committed Jan 10, 2024
1 parent c4730b2 commit 0b148c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/storage/storageutil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func CreateHttpClient(storageClientConfig *StorageClientConfig) (httpClient *htt
// Using http1 makes the client more performant.
if storageClientConfig.ClientProtocol == mountpkg.HTTP1 {
transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxConnsPerHost: storageClientConfig.MaxConnsPerHost,
MaxIdleConnsPerHost: storageClientConfig.MaxIdleConnsPerHost,
// This disables HTTP/2 in transport.
Expand All @@ -57,6 +58,7 @@ func CreateHttpClient(storageClientConfig *StorageClientConfig) (httpClient *htt
} else {
// For http2, change in MaxConnsPerHost doesn't affect the performance.
transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
MaxConnsPerHost: storageClientConfig.MaxConnsPerHost,
ForceAttemptHTTP2: true,
Expand Down
20 changes: 20 additions & 0 deletions internal/storage/storageutil/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package storageutil

import (
"net/http"
"net/url"
"testing"

"github.com/jacobsa/oglematchers"
. "github.com/jacobsa/ogletest"
"golang.org/x/oauth2"
)

func TestClient(t *testing.T) { RunTests(t) }
Expand All @@ -29,6 +31,22 @@ type clientTest struct {

func init() { RegisterTestSuite(&clientTest{}) }

// Helpers

func (t *clientTest) validateProxyInTransport(httpClient *http.Client) {
userAgentRT, ok := httpClient.Transport.(*userAgentRoundTripper)
AssertEq(true, ok)
oauthTransport, ok := userAgentRT.wrapped.(*oauth2.Transport)
AssertEq(true, ok)
transport, ok := oauthTransport.Base.(*http.Transport)
AssertEq(true, ok)
if ok {
ExpectEq(http.ProxyFromEnvironment, transport.Proxy)
}
}

// Tests

func (t *clientTest) TestCreateTokenSrcWithCustomEndpoint() {
url, err := url.Parse(CustomEndpoint)
AssertEq(nil, err)
Expand Down Expand Up @@ -62,6 +80,7 @@ func (t *clientTest) TestCreateHttpClientWithHttp1() {
ExpectEq(nil, err)
ExpectNe(nil, httpClient)
ExpectNe(nil, httpClient.Transport)
t.validateProxyInTransport(httpClient)
ExpectEq(sc.HttpClientTimeout, httpClient.Timeout)
}

Expand All @@ -74,5 +93,6 @@ func (t *clientTest) TestCreateHttpClientWithHttp2() {
ExpectEq(nil, err)
ExpectNe(nil, httpClient)
ExpectNe(nil, httpClient.Transport)
t.validateProxyInTransport(httpClient)
ExpectEq(sc.HttpClientTimeout, httpClient.Timeout)
}

0 comments on commit 0b148c3

Please sign in to comment.