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

Conversation

Strum355
Copy link
Member

@Strum355 Strum355 commented Mar 22, 2023

For some reason, we had panics in production because caching transport wasnt unwrappable. Being unwrappable seems benign, so we'll implement it for that

panic: httpcli.ExternalTransportOpt: http.Client.Transport cannot be cast as a *http.Transport: *httpcache.Transport

        goroutine 7639 [running]:
        github.com/sourcegraph/sourcegraph/internal/extsvc/crates.NewClient({0xc0024c4a68, 0x15}, 0xc0002120c0)
                /home/noah/Sourcegraph/sourcegraph/internal/extsvc/crates/client.go:23 +0x125
        github.com/sourcegraph/sourcegraph/cmd/gitserver/shared.getVCSSyncer({0x2556538, 0xc0029bc690}, {0x256ba50, 0xc0024c1ad0}, {0x25698a0, 0xc002402438}, 0x11?, {0xc002ccc378, 0x11}, {0xc00006045e, ...}, ...)
                /home/noah/Sourcegraph/sourcegraph/cmd/gitserver/shared/shared.go:518 +0x830
        github.com/sourcegraph/sourcegraph/cmd/gitserver/shared.Main.func2({0x2556538?, 0xc0029bc690?}, {0xc002ccc378?, 0xc002ccc378?})
                /home/noah/Sourcegraph/sourcegraph/cmd/gitserver/shared/shared.go:147 +0x89
        github.com/sourcegraph/sourcegraph/cmd/gitserver/server.(*Server).doBackgroundRepoUpdate(0xc00244db00, {0xc002ccc348, 0x11}, {0x0, 0x0})
                /home/noah/Sourcegraph/sourcegraph/cmd/gitserver/server/server.go:2703 +0x3e6
        github.com/sourcegraph/sourcegraph/cmd/gitserver/server.(*Server).doRepoUpdate.func1.1()
                /home/noah/Sourcegraph/sourcegraph/cmd/gitserver/server/server.go:2639 +0x1d2
        sync.(*Once).doSlow(0x0?, 0x4a34ea?)
                /nix/store/dnvng8cdwc2i6g48qxijqmch3w8l3s85-go-1.20/share/go/src/sync/once.go:74 +0xc2
        sync.(*Once).Do(...)
                /nix/store/dnvng8cdwc2i6g48qxijqmch3w8l3s85-go-1.20/share/go/src/sync/once.go:65
        github.com/sourcegraph/sourcegraph/cmd/gitserver/server.(*Server).doRepoUpdate.func1()
                /home/noah/Sourcegraph/sourcegraph/cmd/gitserver/server/server.go:2631 +0x125
        created by github.com/sourcegraph/sourcegraph/cmd/gitserver/server.(*Server).doRepoUpdate
                /home/noah/Sourcegraph/sourcegraph/cmd/gitserver/server/server.go:2629 +0x4b8

Test plan

Ran locally, no more panic

@Strum355 Strum355 requested a review from eseliger March 22, 2023 22:24
@Strum355 Strum355 self-assigned this Mar 22, 2023
@cla-bot cla-bot bot added the cla-signed label Mar 22, 2023
@sourcegraph-bot
Copy link
Contributor

sourcegraph-bot commented Mar 22, 2023

Codenotify: Notifying subscribers in CODENOTIFY files for diff 4c1785b...67dea8e.

Notify File(s)
@eseliger internal/extsvc/crates/client.go
internal/extsvc/npm/npm.go
internal/extsvc/npm/npm_test.go
internal/extsvc/pypi/client.go
internal/extsvc/pypi/client_test.go
internal/extsvc/rubygems/client.go
internal/extsvc/rubygems/client_test.go
@indradhanush cmd/gitserver/server/vcs_syncer_python_packages_test.go
internal/repos/npm_packages.go
internal/repos/python_packages.go
internal/repos/ruby_packages.go
internal/repos/rust_packages.go
@keegancsmith internal/httpcli/client.go
@sashaostrikov cmd/gitserver/server/vcs_syncer_python_packages_test.go
internal/repos/npm_packages.go
internal/repos/python_packages.go
internal/repos/ruby_packages.go
internal/repos/rust_packages.go

@Strum355 Strum355 enabled auto-merge (squash) March 22, 2023 22:27
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) {
time.Sleep(time.Second * 30)
Copy link
Member

Choose a reason for hiding this comment

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

🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

loool

Comment on lines +441 to +447
cli.Transport = &wrappedTransport{
RoundTripper: &httpcache.Transport{
Transport: cli.Transport,
Cache: c,
MarkCachedResponses: markCachedResponses,
},
Wrapped: cli.Transport,
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?

@Strum355 Strum355 merged commit 0bdcf03 into main Mar 22, 2023
20 of 21 checks passed
@Strum355 Strum355 deleted the nsc/htticli-wrapped-cacher branch March 22, 2023 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants