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

metadata: move FromOutgoingContextRaw() to internal #6765

Merged
merged 5 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ var (
ExitIdleModeForTesting any // func(*grpc.ClientConn) error

ChannelzTurnOffForTesting func()

// Gets the function definition from metadata package
Copy link
Member

Choose a reason for hiding this comment

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

Please fix the formatting.

FromOutgoingContextRaw any // func(context.Context) (metadata.MD, [][]string, bool)
)

// HealthChecker defines the signature of the client-side LB channel health checking function.
Expand Down
3 changes: 2 additions & 1 deletion internal/transport/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-trace-bin", Value: encodeBinHeader(b)})
}

if md, added, ok := metadata.FromOutgoingContextRaw(ctx); ok {
fromOutgoingCtxRaw := internal.FromOutgoingContextRaw.(func(context.Context) (metadata.MD, [][]string, bool))
Copy link
Member

Choose a reason for hiding this comment

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

Please instead do:

package transport

var metadataFromOutgoingContextRaw = internal.FromOutgoingContextRaw.(func(context.Context) (metadata.MD, [][]string, bool))

and then use metadataFromOutgoingContextRaw directly. (This way we only do the type assertion once.)

if md, added, ok := fromOutgoingCtxRaw(ctx); ok {
var k string
for k, vv := range md {
// HTTP doesn't allow you to set pseudoheaders after non pseudoheaders were set.
Expand Down
10 changes: 8 additions & 2 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ import (
"context"
"fmt"
"strings"

"google.golang.org/grpc/internal"
)

func init() {
internal.FromOutgoingContextRaw = fromOutgoingContextRaw
}

// DecodeKeyValue returns k, v, nil.
//
// Deprecated: use k and v directly instead.
Expand Down Expand Up @@ -238,7 +244,7 @@ func copyOf(v []string) []string {
return vals
}

// FromOutgoingContextRaw returns the un-merged, intermediary contents of rawMD.
// fromOutgoingContextRaw returns the un-merged, intermediary contents of rawMD.
//
// Remember to perform strings.ToLower on the keys, for both the returned MD (MD
// is a map, there's no guarantee it's created using our helper functions) and
Expand All @@ -247,7 +253,7 @@ func copyOf(v []string) []string {
//
// This is intended for gRPC-internal use ONLY. Users should use
// FromOutgoingContext instead.
Copy link
Member

Choose a reason for hiding this comment

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

This can be removed now.

func FromOutgoingContextRaw(ctx context.Context) (MD, [][]string, bool) {
func fromOutgoingContextRaw(ctx context.Context) (MD, [][]string, bool) {
raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD)
if !ok {
return nil, nil, false
Expand Down
3 changes: 2 additions & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
// when the RPC completes.
opts = append([]CallOption{OnFinish(func(error) { cc.idlenessMgr.OnCallEnd() })}, opts...)

if md, added, ok := metadata.FromOutgoingContextRaw(ctx); ok {
fromOutgoingCtxRaw := internal.FromOutgoingContextRaw.(func(context.Context) (metadata.MD, [][]string, bool))
Copy link
Member

Choose a reason for hiding this comment

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

Same as above with the transport package.

if md, added, ok := fromOutgoingCtxRaw(ctx); ok {
// validate md
if err := imetadata.Validate(md); err != nil {
return nil, status.Error(codes.Internal, err.Error())
Expand Down