Skip to content

[common-go] Support rate limiting by enum key #16899

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

Merged
merged 1 commit into from
Mar 17, 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
13 changes: 9 additions & 4 deletions components/common-go/grpc/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package grpc

import (
"context"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -110,12 +111,16 @@ func getFieldValue(msg protoreflect.Message, path []string) (val string, ok bool
return getFieldValue(child, path[1:])
}

if field.Kind() != protoreflect.StringKind {
// we only support string fields
switch field.Kind() {
case protoreflect.StringKind:
return msg.Get(field).String(), true
case protoreflect.EnumKind:
enumNum := msg.Get(field).Enum()
return strconv.Itoa(int(enumNum)), true
default:
// we only support string and enum fields
return "", false
}

return msg.Get(field).String(), true
}

// RatelimitingInterceptor limits how often a gRPC function may be called. If the limit has been
Expand Down
16 changes: 15 additions & 1 deletion components/common-go/grpc/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package grpc

import (
"strconv"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/apipb"
"google.golang.org/protobuf/types/known/sourcecontextpb"
"google.golang.org/protobuf/types/known/typepb"
)

func TestGetFieldValue(t *testing.T) {
Expand All @@ -32,11 +34,23 @@ func TestGetFieldValue(t *testing.T) {
Expectation: Expectation{Found: true, Val: "bar"},
},
{
Name: "empty field",
Name: "empty string field",
Message: &apipb.Api{},
Path: "name",
Expectation: Expectation{Found: true},
},
{
Name: "enum field",
Copy link
Member

Choose a reason for hiding this comment

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

👍

Message: &apipb.Api{Syntax: typepb.Syntax_SYNTAX_PROTO3},
Path: "syntax",
Expectation: Expectation{Found: true, Val: strconv.Itoa(int(typepb.Syntax_SYNTAX_PROTO3))},
},
{
Name: "empty enum field",
Message: &apipb.Api{},
Path: "syntax",
Expectation: Expectation{Found: true, Val: "0"},
},
{
Name: "non-existent field",
Message: &apipb.Api{},
Expand Down