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: add ValueFromIncomingContext to more efficiently retrieve a single value #5596

Merged
merged 6 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type MD map[string][]string
// Keys beginning with "grpc-" are reserved for grpc-internal use only and may
// result in errors if set in metadata.
func New(m map[string]string) MD {
md := MD{}
md := make(MD, len(m))
for k, val := range m {
key := strings.ToLower(k)
md[key] = append(md[key], val)
Expand All @@ -74,7 +74,7 @@ func Pairs(kv ...string) MD {
if len(kv)%2 == 1 {
panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
}
md := MD{}
md := make(MD, len(kv)/2)
for i := 0; i < len(kv); i += 2 {
key := strings.ToLower(kv[i])
md[key] = append(md[key], kv[i+1])
Expand Down Expand Up @@ -182,7 +182,7 @@ func FromIncomingContext(ctx context.Context) (MD, bool) {
if !ok {
return nil, false
}
out := MD{}
out := make(MD, len(md))
for k, v := range md {
// We need to manually convert all keys to lower case, because MD is a
// map, and there's no guarantee that the MD attached to the context is
Expand All @@ -195,6 +195,33 @@ func FromIncomingContext(ctx context.Context) (MD, bool) {
return out, true
}

// ValueFromIncomingContext returns the metadata value corresponding to the metadata
horpto marked this conversation as resolved.
Show resolved Hide resolved
// key from the incoming metadata if it exists.
func ValueFromIncomingContext(ctx context.Context, key string) []string {
md, ok := ctx.Value(mdIncomingKey{}).(MD)
if !ok {
return nil
}

if v, ok := md[key]; ok {
vals := make([]string, len(v))
copy(vals, v)
return vals
horpto marked this conversation as resolved.
Show resolved Hide resolved
}
key = strings.ToLower(key)
horpto marked this conversation as resolved.
Show resolved Hide resolved
for k, v := range md {
// We need to manually convert all keys to lower case, because MD is a
// map, and there's no guarantee that the MD attached to the context is
// created using our helper functions.
if strings.ToLower(k) == key {
vals := make([]string, len(v))
copy(vals, v)
return vals
}
}
return nil
}

// FromOutgoingContextRaw returns the un-merged, intermediary contents of rawMD.
//
// Remember to perform strings.ToLower on the keys, for both the returned MD (MD
Expand Down Expand Up @@ -222,7 +249,7 @@ func FromOutgoingContext(ctx context.Context) (MD, bool) {
return nil, false
}

out := MD{}
out := make(MD, len(raw.md)+len(raw.added))
horpto marked this conversation as resolved.
Show resolved Hide resolved
for k, v := range raw.md {
// We need to manually convert all keys to lower case, because MD is a
// map, and there's no guarantee that the MD attached to the context is
Expand Down
36 changes: 36 additions & 0 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,42 @@ func (s) TestDelete(t *testing.T) {
}
}

func (s) TestValueFromIncomingContext(t *testing.T) {
md := Pairs(
"X-My-Header-1", "42",
"X-My-Header-2", "43-1",
"X-My-Header-2", "43-2",
)
ctx := NewIncomingContext(context.Background(), md)

for _, test := range []struct {
key string
want []string
}{
{
key: "X-My-Header-1",
want: []string{"42"},
},
{
key: "x-my-header-1",
want: []string{"42"},
},
{
key: "x-my-header-2",
want: []string{"43-1", "43-2"},
},
{
key: "x-unknown",
want: nil,
},
} {
v := ValueFromIncomingContext(ctx, test.key)
if !reflect.DeepEqual(v, test.want) {
t.Errorf("value of metadata is %v, want %v", v, test.want)
}
}
}

func (s) TestAppendToOutgoingContext(t *testing.T) {
// Pre-existing metadata
tCtx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
Expand Down