Skip to content

Commit 9bd36a1

Browse files
committed
Optimize FieldAccessKey
1 parent cac2221 commit 9bd36a1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

components/common-go/grpc/ratelimit.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,27 @@ 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
}
102106
// It's technically possible that `|` is part of one of the field values, and therefore could cause collisions
103107
// in composite keys, e.g. values (`a|`, `b`), and (`a`, `|b`) would result in the same composite key `a||b`
104-
// and share the rate limit. This is currently not possible though given the fields we rate limit on.
108+
// and share the rate limit. This is highly unlikely though given the current fields we rate limit on and
109+
// otherwise unlikely to cause issues.
105110
composite += "|" + val
106111
}
107112

0 commit comments

Comments
 (0)