Skip to content

Commit

Permalink
internal/proxy: inject transport in proxy.New
Browse files Browse the repository at this point in the history
This removes the dependency from internal/proxy on ochttp.

For golang/go#61399

Change-Id: Ib2a350151752dc18c9a6e03b38b2d423ed55f4a1
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/523513
LUCI-TryBot-Result: Go LUCI <[email protected]>
Run-TryBot: Michael Matloob <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
kokoro-CI: kokoro <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
  • Loading branch information
matloob committed Aug 29, 2023
1 parent 18516de commit db4dc42
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
expg := cmdconfig.ExperimentGetter(ctx, cfg)
log.Infof(ctx, "cmd/frontend: initialized cmdconfig.ExperimentGetter")

proxyClient, err := proxy.New(*proxyURL)
proxyClient, err := proxy.New(*proxyURL, &ochttp.Transport{})
if err != nil {
log.Fatal(ctx, err)
}
Expand All @@ -103,7 +103,7 @@ func main() {
defer db.Close()
dsg = func(context.Context) internal.DataSource { return db }
sourceClient := source.NewClient(&http.Client{
Transport: &ochttp.Transport{},
Transport: new(ochttp.Transport),
Timeout: config.SourceTimeout,
})
// The closure passed to queue.New is only used for testing and local
Expand Down
2 changes: 1 addition & 1 deletion cmd/pkgsite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func main() {
die("GOPROXY environment variable is not set")
}
var err error
serverCfg.proxy, err = proxy.New(url)
serverCfg.proxy, err = proxy.New(url, nil)
if err != nil {
die("connecting to proxy: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
if err != nil {
log.Fatal(ctx, err)
}
proxyClient, err := proxy.New(cfg.ProxyURL)
proxyClient, err := proxy.New(cfg.ProxyURL, new(ochttp.Transport))
if err != nil {
log.Fatal(ctx, err)
}
Expand Down
4 changes: 2 additions & 2 deletions devtools/cmd/seeddb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ func main() {
func run(ctx context.Context, db *database.DB, proxyURL string) error {
start := time.Now()

proxyClient, err := proxy.New(proxyURL)
proxyClient, err := proxy.New(proxyURL, new(ochttp.Transport))
if err != nil {
return err
}

sourceClient := source.NewClient(&http.Client{
Transport: &ochttp.Transport{},
Transport: new(ochttp.Transport),
Timeout: config.SourceTimeout,
})
seedModules, err := readSeedFile(ctx, *seedfile)
Expand Down
2 changes: 1 addition & 1 deletion internal/fetch/gen_zip_signatures.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
flag.Parse()
ctx := context.Background()

prox, err := proxy.New("https://proxy.golang.org")
prox, err := proxy.New("https://proxy.golang.org", nil)
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/proxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"strings"
"time"

"go.opencensus.io/plugin/ochttp"
"golang.org/x/mod/module"
"golang.org/x/net/context/ctxhttp"
"golang.org/x/pkgsite/internal/derrors"
Expand Down Expand Up @@ -52,11 +51,12 @@ const DisableFetchHeader = "Disable-Module-Fetch"

// New constructs a *Client using the provided url, which is expected to
// be an absolute URI that can be directly passed to http.Get.
func New(u string) (_ *Client, err error) {
// The optional transport parameter is used by the underlying http client.
func New(u string, transport http.RoundTripper) (_ *Client, err error) {
defer derrors.WrapStack(&err, "proxy.New(%q)", u)
return &Client{
url: strings.TrimRight(u, "/"),
HTTPClient: &http.Client{Transport: &ochttp.Transport{}},
HTTPClient: &http.Client{Transport: transport},
disableFetch: false,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestZipSize(t *testing.T) {
}

func TestEncodedURL(t *testing.T) {
c, err := proxy.New("u")
c, err := proxy.New("u", nil)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/proxy/proxytest/proxytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"go.opencensus.io/plugin/ochttp"
"golang.org/x/mod/modfile"
"golang.org/x/pkgsite/internal/proxy"
"golang.org/x/pkgsite/internal/testing/testhelper"
Expand All @@ -36,7 +37,7 @@ func SetupTestClient(t *testing.T, modules []*Module) (*proxy.Client, func()) {
func NewClientForServer(s *Server) (*proxy.Client, func(), error) {
// override client.httpClient to skip TLS verification
httpClient, prox, serverClose := testhelper.SetupTestClientAndServer(s.mux)
client, err := proxy.New(prox.URL)
client, err := proxy.New(prox.URL, new(ochttp.Transport))
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion tests/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sort"
"strings"

"go.opencensus.io/plugin/ochttp"
"golang.org/x/mod/semver"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
Expand Down Expand Up @@ -107,7 +108,7 @@ func run(ctx context.Context, cmd, pkgPath, modulePath, frontendHost, proxyURL s

func generate(ctx context.Context, pkgPath, modulePath, tmpPath, proxyURL string) (err error) {
defer derrors.Wrap(&err, "generate(ctx, %q, %q, %q, %q)", pkgPath, modulePath, tmpPath, proxyURL)
proxyClient, err := proxy.New(proxyURL)
proxyClient, err := proxy.New(proxyURL, new(ochttp.Transport))
if err != nil {
return err
}
Expand Down

0 comments on commit db4dc42

Please sign in to comment.