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: optimize metadata delivery #11

Merged
merged 2 commits into from
Feb 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/xstrings/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package xstrings

func Distinct(values []string) []string {
m := make(map[string]struct{}, len(values))
newValues := make([]string, 0, len(values))
for _, value := range values {
_, ok := m[value]
if ok {
continue
}
newValues = append(newValues, value)
m[value] = struct{}{}
}

return newValues
}
10 changes: 3 additions & 7 deletions md/grpc_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"google.golang.org/grpc/metadata"
)

const zeroFlowFieldsKey = "zero-flow-fields"

var _ Carrier = (*GrpcMetadataCarrier)(nil)

type GrpcMetadataCarrier metadata.MD
Expand All @@ -30,11 +32,5 @@ func (h GrpcMetadataCarrier) Set(key string, value ...string) {
}

func (h GrpcMetadataCarrier) Keys() []string {
keys := make([]string, 0, len(h))
for k := range h {
k = strings.ToLower(k)
keys = append(keys, k)
}

return keys
return h[zeroFlowFieldsKey]
}
29 changes: 24 additions & 5 deletions md/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net/url"
"strings"

"github.com/chenquan/zero-flow/internal/xstrings"
"google.golang.org/grpc/attributes"
"google.golang.org/grpc/metadata"
)

var _ Carrier = (*Metadata)(nil)
Expand Down Expand Up @@ -91,11 +93,17 @@ func (m Metadata) Clone() Metadata {

func (m Metadata) Merge(metadata Metadata) {
metadata.Range(func(key string, values ...string) bool {
m.Append(strings.ToLower(key), copyOf(values)...)
m.Append(strings.ToLower(key), copyOf(xstrings.Distinct(values))...)
return true
})
}

func (m Metadata) Distinct() {
for k, v := range m {
m[k] = xstrings.Distinct(v)
}
}

func FromContext(ctx context.Context) (Metadata, bool) {
value := ctx.Value(metadataKey{})
if value == nil {
Expand All @@ -114,18 +122,29 @@ func NewContext(ctx context.Context, carrier Carrier) context.Context {
return context.WithValue(ctx, metadataKey{}, md)
}

func ToGrpcMetadata(m Metadata) metadata.MD {
md := metadata.MD{}
keys := m.Keys()
for _, key := range keys {
md.Set(key, m.Get(key)...)
}
m.Set(zeroFlowFieldsKey, keys...)

return md
}

func NewMetaDataFromContext(ctx context.Context, carrier Carrier) (context.Context, Metadata) {
metadata, ok := FromContext(ctx)
if !ok {
metadata = Metadata{}
} else {
metadata = metadata.Clone()
}

for _, key := range carrier.Keys() {
metadata.Append(strings.ToLower(key), carrier.Get(key)...)
keys := carrier.Keys()
for _, key := range keys {
metadata.Append(strings.ToLower(key), xstrings.Distinct(carrier.Get(key))...)
}

metadata.Distinct()
return context.WithValue(ctx, metadataKey{}, metadata), metadata
}

Expand Down
6 changes: 3 additions & 3 deletions zrpc/internal/clientinterceptors/mdinterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ func injectionMd(ctx context.Context, defaultMd md.Metadata) context.Context {
outgoingMd = metadata.MD{}
}

m.Range(func(key string, values ...string) bool {
grpcMetadata := md.ToGrpcMetadata(m)
for key, values := range grpcMetadata {
outgoingMd.Append(key, values...)
return true
})
}
ctx = metadata.NewOutgoingContext(ctx, outgoingMd)

m, ok = md.FromContext(ctx)
Expand Down
1 change: 1 addition & 0 deletions zrpc/internal/clientinterceptors/mdinterceptor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package clientinterceptors
1 change: 1 addition & 0 deletions zrpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func MustNewClient(c RpcClientConf, options ...zrpc.ClientOption) zrpc.Client {
}
metadata := md.Metadata(query)
metadata.Merge(selector.DefaultSelectorMd)
metadata.Distinct()
svcCfg := fmt.Sprintf(`{"loadBalancingPolicy":"%s"}`, p2c.Name)
options = append(options,
zrpc.WithUnaryClientInterceptor(clientinterceptors.UnaryMdInterceptor(metadata.Clone())),
Expand Down