Skip to content

Commit 9ba5720

Browse files
committed
Optimize FieldAccessKey
1 parent a32278c commit 9ba5720

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

components/common-go/grpc/ratelimit.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,20 @@ func NewRatelimitingInterceptor(f map[string]RateLimit) RatelimitingInterceptor
8686
}
8787

8888
func fieldAccessKey(key string) keyFunc {
89+
fields := strings.Split(key, ",")
90+
paths := make([][]string, len(fields))
91+
for i, field := range fields {
92+
paths[i] = strings.Split(field, ".")
93+
}
8994
return func(req interface{}) (string, error) {
9095
msg, ok := req.(proto.Message)
9196
if !ok {
9297
return "", status.Errorf(codes.Internal, "request was not a protobuf message")
9398
}
9499

95-
fields := strings.Split(key, ",")
96100
var composite string
97-
for _, field := range fields {
98-
val, ok := getFieldValue(msg.ProtoReflect(), strings.Split(field, "."))
101+
for i, field := range fields {
102+
val, ok := getFieldValue(msg.ProtoReflect(), paths[i])
99103
if !ok {
100104
return "", status.Errorf(codes.Internal, "Field %s does not exist in message. This is a rate limiting configuration error.", field)
101105
}

0 commit comments

Comments
 (0)