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

feat(gensupport): pass in headers via context #2052

Merged
merged 6 commits into from
Jul 7, 2023

Conversation

tritone
Copy link
Contributor

@tritone tritone commented Jul 5, 2023

This allows headers added to the context metadata to be applied to the request. See googleapis/gax-go#291.

Needs a release & version bump of gax-go to work.

This allows headers added to the context metadata to be applied to
the request. See googleapis/gax-go#291.
@tritone tritone requested a review from a team as a code owner July 5, 2023 20:26
internal/gensupport/send.go Outdated Show resolved Hide resolved
internal/gensupport/send.go Outdated Show resolved Hide resolved
internal/gensupport/send_test.go Outdated Show resolved Hide resolved
Comment on lines 49 to 52
for k, vals := range headers {
for _, v := range vals {
req.Header.Add(k, v)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I see what you mean by the nested loop...alternatively, you could do this:

for k, vals := range headers {
  req.Header.Set(k, append(r.Header.Get(k), vals...))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @noahdietz , I think I'll just stick with what I have since it's slightly clearer to look at to me if that's okay.

@tritone tritone added the automerge Merge the pull request once unit tests and other checks pass. label Jul 6, 2023
@gcf-merge-on-green
Copy link
Contributor

Merge-on-green attempted to merge your PR for 6 hours, but it was not mergeable because either one of your required status checks failed, one of your required reviews was not approved, or there is a do not merge label. Learn more about your required status checks here: https://help.github.com/en/github/administering-a-repository/enabling-required-status-checks. You can remove and reapply the label to re-run the bot.

@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Jul 7, 2023
@noahdietz
Copy link
Contributor

Here is the failing test (save you a click)

--- FAIL: TestMedia (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0xbf47ff]

goroutine 21 [running]:
testing.tRunner.func1.2({0xcacd80, 0x1302a50})
	/usr/local/go/src/testing/testing.go:1209 +0x36c
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1212 +0x3b6
panic({0xcacd80, 0x1302a50})
	/usr/local/go/src/runtime/panic.go:1047 +0x266
github.com/googleapis/gax-go/v2/callctx.HeadersFromContext(...)
	/root/go/pkg/mod/github.com/googleapis/gax-go/v2@v2.12.0/callctx/callctx.go:50
google.golang.org/api/internal/gensupport.SendRequest({0x0, 0x0}, 0xd75bc2, 0xc000572000)
	/root/go/src/google.golang.org/api/internal/gensupport/send.go:48 +0x5f
google.golang.org/api/storage/v1.(*ObjectsInsertCall).doRequest(0xc000203e38, {0xd745f3, 0x4})
	/root/go/src/google.golang.org/api/storage/v1/storage-gen.go:9901 +0xf2d
google.golang.org/api/storage/v1.(*ObjectsInsertCall).Do(0xc000203e38, {0x0, 0x0, 0x0})
	/root/go/src/google.golang.org/api/storage/v1/storage-gen.go:9913 +0xc5
google.golang.org/api/google-api-go-generator.TestMedia(0xc0002ab380)
	/root/go/src/google.golang.org/api/google-api-go-generator/clients_test.go:75 +0x665
testing.tRunner(0xc0002ab380, 0xdab160)
	/usr/local/go/src/testing/testing.go:1259 +0x230
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1306 +0x727
FAIL	google.golang.org/api/google-api-go-generator	0.073s

@tritone
Copy link
Contributor Author

tritone commented Jul 7, 2023

Here is the failing test (save you a click)

--- FAIL: TestMedia (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0xbf47ff]

goroutine 21 [running]:
testing.tRunner.func1.2({0xcacd80, 0x1302a50})
	/usr/local/go/src/testing/testing.go:1209 +0x36c
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1212 +0x3b6
panic({0xcacd80, 0x1302a50})
	/usr/local/go/src/runtime/panic.go:1047 +0x266
github.com/googleapis/gax-go/v2/callctx.HeadersFromContext(...)
	/root/go/pkg/mod/github.com/googleapis/gax-go/v2@v2.12.0/callctx/callctx.go:50
google.golang.org/api/internal/gensupport.SendRequest({0x0, 0x0}, 0xd75bc2, 0xc000572000)
	/root/go/src/google.golang.org/api/internal/gensupport/send.go:48 +0x5f
google.golang.org/api/storage/v1.(*ObjectsInsertCall).doRequest(0xc000203e38, {0xd745f3, 0x4})
	/root/go/src/google.golang.org/api/storage/v1/storage-gen.go:9901 +0xf2d
google.golang.org/api/storage/v1.(*ObjectsInsertCall).Do(0xc000203e38, {0x0, 0x0, 0x0})
	/root/go/src/google.golang.org/api/storage/v1/storage-gen.go:9913 +0xc5
google.golang.org/api/google-api-go-generator.TestMedia(0xc0002ab380)
	/root/go/src/google.golang.org/api/google-api-go-generator/clients_test.go:75 +0x665
testing.tRunner(0xc0002ab380, 0xdab160)
	/usr/local/go/src/testing/testing.go:1259 +0x230
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1306 +0x727
FAIL	google.golang.org/api/google-api-go-generator	0.073s

Thanks, yeah I started looking into this and interestingly it's possible for the context to be nil here. I think the storage veneer always sets the context on the request but two tests do not. I'm wondering whether the check for a nil context should happen in this library, or in the gax function -- any thoughts?

@noahdietz
Copy link
Contributor

I'm wondering whether the check for a nil context should happen in this library, or in the gax function -- any thoughts?

The context docs say the following:

Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.

So I don't think we should handle the nil context case. Let's just fix the test?

@tritone
Copy link
Contributor Author

tritone commented Jul 7, 2023

I'm wondering whether the check for a nil context should happen in this library, or in the gax function -- any thoughts?

The context docs say the following:

Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.

So I don't think we should handle the nil context case. Let's just fix the test?

So I think it's a bit more complicated in this case because of how the surface is designed. The Apiary methods do not take a context as a required arg but rather a user would have to know to pass it in using a helper method. E.g. the veneer library does this to make an upload call:

		call := c.raw.Objects.Insert(params.bucket, rawObj).
			Media(pr, mediaOpts...).
			Projection("full").
			Context(params.ctx).
			Name(params.attrs.Name)
		// ...
		resp, err = call.Do()

The veneer library of course adds .Context() for all calls but there isn't any docs in Apiary saying that this is mandatory. So, I think I should add a nil check here probably. Does this sound right? (Honestly surprised this hasn't come up before given that it is a bit of a weird assumption.

@noahdietz
Copy link
Contributor

So, I think I should add a nil check here probably. Does this sound right? (Honestly surprised this hasn't come up before given that it is a bit of a weird assumption.

TIL! Yeah this sounds right to me, thanks for explaining.

@tritone tritone merged commit c836da9 into googleapis:main Jul 7, 2023
@tritone tritone deleted the ctx-headers branch July 7, 2023 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants