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

httpcli: make caching transport unwrappable #49877

Merged
merged 2 commits into from Mar 22, 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
2 changes: 1 addition & 1 deletion cmd/gitserver/server/vcs_syncer_python_packages_test.go
Expand Up @@ -266,6 +266,6 @@ func newTestClient(t testing.TB, name string, update bool) *pypi.Client {

doer := httpcli.NewFactory(nil, httptestutil.NewRecorderOpt(rec))

c := pypi.NewClient("urn", []string{"https://pypi.org/simple"}, doer)
c, _ := pypi.NewClient("urn", []string{"https://pypi.org/simple"}, doer)
return c
}
20 changes: 16 additions & 4 deletions cmd/gitserver/shared/shared.go
Expand Up @@ -491,7 +491,10 @@ func getVCSSyncer(
if err != nil {
return nil, err
}
cli := npm.NewHTTPClient(urn, c.Registry, c.Credentials, httpcli.ExternalClientFactory)
cli, err := npm.NewHTTPClient(urn, c.Registry, c.Credentials, httpcli.ExternalClientFactory)
if err != nil {
return nil, err
}
return server.NewNpmPackagesSyncer(c, depsSvc, cli), nil
case extsvc.TypeGoModules:
var c schema.GoModulesConnection
Expand All @@ -507,23 +510,32 @@ func getVCSSyncer(
if err != nil {
return nil, err
}
cli := pypi.NewClient(urn, c.Urls, httpcli.ExternalClientFactory)
cli, err := pypi.NewClient(urn, c.Urls, httpcli.ExternalClientFactory)
if err != nil {
return nil, err
}
return server.NewPythonPackagesSyncer(&c, depsSvc, cli, reposDir), nil
case extsvc.TypeRustPackages:
var c schema.RustPackagesConnection
urn, err := extractOptions(&c)
if err != nil {
return nil, err
}
cli := crates.NewClient(urn, httpcli.ExternalClientFactory)
cli, err := crates.NewClient(urn, httpcli.ExternalClientFactory)
if err != nil {
return nil, err
}
return server.NewRustPackagesSyncer(&c, depsSvc, cli), nil
case extsvc.TypeRubyPackages:
var c schema.RubyPackagesConnection
urn, err := extractOptions(&c)
if err != nil {
return nil, err
}
cli := rubygems.NewClient(urn, c.Repository, httpcli.ExternalClientFactory)
cli, err := rubygems.NewClient(urn, c.Repository, httpcli.ExternalClientFactory)
if err != nil {
return nil, err
}
return server.NewRubyPackagesSyncer(&c, depsSvc, cli), nil
}
return &server.GitRepoSyncer{}, nil
Expand Down
9 changes: 6 additions & 3 deletions internal/extsvc/crates/client.go
Expand Up @@ -17,12 +17,15 @@ type Client struct {
limiter *ratelimit.InstrumentedLimiter
}

func NewClient(urn string, httpfactory *httpcli.Factory) *Client {
uncached, _ := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
func NewClient(urn string, httpfactory *httpcli.Factory) (*Client, error) {
uncached, err := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
if err != nil {
return nil, err
}
return &Client{
uncachedClient: uncached,
limiter: ratelimit.DefaultRegistry.Get(urn),
}
}, nil
}

func (c *Client) Get(ctx context.Context, url string) (io.ReadCloser, error) {
Expand Down
14 changes: 10 additions & 4 deletions internal/extsvc/npm/npm.go
Expand Up @@ -57,16 +57,22 @@ type HTTPClient struct {

var _ Client = &HTTPClient{}

func NewHTTPClient(urn string, registryURL string, credentials string, httpfactory *httpcli.Factory) *HTTPClient {
uncached, _ := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
cached, _ := httpfactory.Doer()
func NewHTTPClient(urn string, registryURL string, credentials string, httpfactory *httpcli.Factory) (*HTTPClient, error) {
uncached, err := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
if err != nil {
return nil, err
}
cached, err := httpfactory.Doer()
if err != nil {
return nil, err
}
return &HTTPClient{
registryURL: registryURL,
uncachedClient: uncached,
cachedClient: cached,
limiter: ratelimit.DefaultRegistry.Get(urn),
credentials: credentials,
}
}, nil
}

type PackageInfo struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/extsvc/npm/npm_test.go
Expand Up @@ -34,7 +34,7 @@ func newTestHTTPClient(t *testing.T) (client *HTTPClient, stop func()) {
t.Helper()
recorderFactory, stop := httptestutil.NewRecorderFactory(t, *updateRecordings, t.Name())

client = NewHTTPClient("urn", "https://registry.npmjs.org", "", recorderFactory)
client, _ = NewHTTPClient("urn", "https://registry.npmjs.org", "", recorderFactory)
return client, stop
}

Expand Down Expand Up @@ -73,7 +73,7 @@ func TestCredentials(t *testing.T) {
defer server.Close()

ctx := context.Background()
client := NewHTTPClient("urn", server.URL, credentials, httpcli.ExternalClientFactory)
client, _ := NewHTTPClient("urn", server.URL, credentials, httpcli.ExternalClientFactory)

presentDep, err := reposource.ParseNpmVersionedPackage("left-pad@1.3.0")
require.NoError(t, err)
Expand Down
14 changes: 10 additions & 4 deletions internal/extsvc/pypi/client.go
Expand Up @@ -56,15 +56,21 @@ type Client struct {
limiter *ratelimit.InstrumentedLimiter
}

func NewClient(urn string, urls []string, httpfactory *httpcli.Factory) *Client {
uncached, _ := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
cached, _ := httpfactory.Doer()
func NewClient(urn string, urls []string, httpfactory *httpcli.Factory) (*Client, error) {
uncached, err := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
if err != nil {
return nil, err
}
cached, err := httpfactory.Doer()
if err != nil {
return nil, err
}
return &Client{
urls: urls,
uncachedClient: uncached,
cachedClient: cached,
limiter: ratelimit.DefaultRegistry.Get(urn),
}
}, nil
}

// Project returns the Files of the simple-API /<project>/ endpoint.
Expand Down
2 changes: 1 addition & 1 deletion internal/extsvc/pypi/client_test.go
Expand Up @@ -448,6 +448,6 @@ func newTestClient(t testing.TB, name string, update bool) *Client {

doer := httpcli.NewFactory(nil, httptestutil.NewRecorderOpt(rec))

c := NewClient("urn", []string{"https://pypi.org/simple"}, doer)
c, _ := NewClient("urn", []string{"https://pypi.org/simple"}, doer)
return c
}
9 changes: 6 additions & 3 deletions internal/extsvc/rubygems/client.go
Expand Up @@ -22,13 +22,16 @@ type Client struct {
limiter *ratelimit.InstrumentedLimiter
}

func NewClient(urn string, registryURL string, httpfactory *httpcli.Factory) *Client {
uncached, _ := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
func NewClient(urn string, registryURL string, httpfactory *httpcli.Factory) (*Client, error) {
uncached, err := httpfactory.Doer(httpcli.NewCachedTransportOpt(httpcli.NoopCache{}, false))
if err != nil {
return nil, err
}
return &Client{
registryURL: registryURL,
uncachedClient: uncached,
limiter: ratelimit.DefaultRegistry.Get(urn),
}
}, nil
}

func (c *Client) GetPackageContents(ctx context.Context, dep reposource.VersionedPackage) (body io.ReadCloser, err error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/extsvc/rubygems/client_test.go
Expand Up @@ -28,7 +28,8 @@ func newTestHTTPClient(t *testing.T) (client *Client, stop func()) {
t.Helper()
recorderFactory, stop := httptestutil.NewRecorderFactory(t, *updateRecordings, t.Name())

return NewClient("rubygems_urn", "https://rubygems.org", recorderFactory), stop
client, _ = NewClient("rubygems_urn", "https://rubygems.org", recorderFactory)
return client, stop
}

func TestGetPackageContents(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions internal/httpcli/client.go
Expand Up @@ -438,10 +438,13 @@ func NewCachedTransportOpt(c httpcache.Cache, markCachedResponses bool) Opt {
cli.Transport = http.DefaultTransport
}

cli.Transport = &httpcache.Transport{
Transport: cli.Transport,
Cache: c,
MarkCachedResponses: markCachedResponses,
cli.Transport = &wrappedTransport{
RoundTripper: &httpcache.Transport{
Transport: cli.Transport,
Cache: c,
MarkCachedResponses: markCachedResponses,
},
Wrapped: cli.Transport,
Comment on lines +441 to +447
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea if this is sane, do you happen to know why this helps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked, all this concept is used for is to be able to set values on the base *http.Transport like here: https://sourcegraph.com/github.com/sourcegraph/sourcegraph@nsc/htticli-wrapped-cacher/-/blob/internal/httpcli/client.go?L683

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im thinking it worked before because of some sort of ordering assumption cc @bobheadxi

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct on being able to set values, and ordering changes could definitely make it mess up

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels fragile, is it much work to not be order dependent? 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the middleware works in a chain - each one modifies the client then passes it on to the next. There seems to be 4 middleware that depend on access to the underlying transport (calling getTransportForMutation), maybe we could make it so that they are a special kind of middleware that is applied first, before anyone has had a chance to wrap the transport? It could make the interface a bit clunkier though

Alternatively, we add some more documentation asking that implementers always use wrappedTransport if they want to change the transport type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely rather fragile though, maybe we can have a test that runs through the default external/internal transport setups?

}

return nil
Expand Down
9 changes: 6 additions & 3 deletions internal/repos/npm_packages.go
Expand Up @@ -27,14 +27,17 @@ func NewNpmPackagesSource(ctx context.Context, svc *types.ExternalService, cf *h
return nil, errors.Errorf("external service id=%d config error: %s", svc.ID, err)
}

client, err := npm.NewHTTPClient(svc.URN(), c.Registry, c.Credentials, cf)
if err != nil {
return nil, err
}

return &PackagesSource{
svc: svc,
configDeps: c.Dependencies,
scheme: dependencies.NpmPackagesScheme,
/* depsSvc initialized in SetDependenciesService */
src: &npmPackagesSource{
client: npm.NewHTTPClient(svc.URN(), c.Registry, c.Credentials, cf),
},
src: &npmPackagesSource{client},
}, nil
}

Expand Down
7 changes: 6 additions & 1 deletion internal/repos/python_packages.go
Expand Up @@ -25,11 +25,16 @@ func NewPythonPackagesSource(ctx context.Context, svc *types.ExternalService, cf
return nil, errors.Errorf("external service id=%d config error: %s", svc.ID, err)
}

client, err := pypi.NewClient(svc.URN(), c.Urls, cf)
if err != nil {
return nil, err
}

return &PackagesSource{
svc: svc,
configDeps: c.Dependencies,
scheme: dependencies.PythonPackagesScheme,
src: &pythonPackagesSource{client: pypi.NewClient(svc.URN(), c.Urls, cf)},
src: &pythonPackagesSource{client},
}, nil
}

Expand Down
7 changes: 6 additions & 1 deletion internal/repos/ruby_packages.go
Expand Up @@ -25,11 +25,16 @@ func NewRubyPackagesSource(ctx context.Context, svc *types.ExternalService, cf *
return nil, errors.Errorf("external service id=%d config error: %s", svc.ID, err)
}

client, err := rubygems.NewClient(svc.URN(), c.Repository, cf)
if err != nil {
return nil, err
}

return &PackagesSource{
svc: svc,
configDeps: c.Dependencies,
scheme: dependencies.RubyPackagesScheme,
src: &rubyPackagesSource{client: rubygems.NewClient(svc.URN(), c.Repository, cf)},
src: &rubyPackagesSource{client},
}, nil
}

Expand Down
7 changes: 6 additions & 1 deletion internal/repos/rust_packages.go
Expand Up @@ -25,11 +25,16 @@ func NewRustPackagesSource(ctx context.Context, svc *types.ExternalService, cf *
return nil, errors.Errorf("external service id=%d config error: %s", svc.ID, err)
}

client, err := crates.NewClient(svc.URN(), cf)
if err != nil {
return nil, err
}

return &PackagesSource{
svc: svc,
configDeps: c.Dependencies,
scheme: dependencies.RustPackagesScheme,
src: &rustPackagesSource{client: crates.NewClient(svc.URN(), cf)},
src: &rustPackagesSource{client},
}, nil
}

Expand Down