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

gRPC server reflection #224

Merged
merged 4 commits into from
Jan 16, 2024
Merged

gRPC server reflection #224

merged 4 commits into from
Jan 16, 2024

Conversation

eguzki
Copy link
Contributor

@eguzki eguzki commented Nov 9, 2023

What

gRPC server reflection provided by tonic-reflection

Opt-in feature to be enabled using --grpc-reflection-service command line option.

Verification steps

Run sandbox dev environment

cd limitador-server/sandbox
make build
make deploy-in-memory

Note: make deploy-in-memory runs limitador with the --grpc-reflection-service command line option set.

In a separate shell, inspect the RLS GRPC endpoint. First, let's install grpcurl tool. You need Go SDK 1.8+ installed.

make grpcurl

Inspect RateLimitService GRPC service

bin/grpcurl -plaintext 127.0.0.1:18081 describe envoy.service.ratelimit.v3.RateLimitService

The output should be the following

envoy.service.ratelimit.v3.RateLimitService is a service:
service RateLimitService {
  // Determine whether rate limiting should take place.
  rpc ShouldRateLimit ( .envoy.service.ratelimit.v3.RateLimitRequest ) returns ( .envoy.service.ratelimit.v3.RateLimitResponse );
}

If the reflection API is not available the output looks like

Failed to resolve symbol "envoy.service.ratelimit.v3.RateLimitService": server does not support the reflection API

Make a manual request to the RLS endpoint

bin/grpcurl -plaintext -d @ 127.0.0.1:18081 envoy.service.ratelimit.v3.RateLimitService.ShouldRateLimit <<EOM
{
    "domain": "test_namespace",
    "hits_addend": 1,
    "descriptors": [
        {
            "entries": [
                {
                    "key": "req.method",
                    "value": "POST"
                }
            ]
        }
    ]
}
EOM

Note that grpcurl does not need proto files or compiled protoset files. The reflection service tells grpc how to parse request and response.

The response should be the following:

{
  "overallCode": "OK"
}

Let's check that rate limiting is working. The limits file looks like

- namespace: test_namespace
  max_value: 5
  seconds: 60
  conditions:
    - "req.method == 'POST'"
  variables: []

Thus, the limit is set to max 5 request for 60 seconds. Do repeated requests. you should see OVER_LIMIT response after 5 requests.

while :; do bin/grpcurl -plaintext -d @ 127.0.0.1:18081 envoy.service.ratelimit.v3.RateLimitService.ShouldRateLimit <<EOM; sleep 1; done
{
    "domain": "test_namespace",
    "hits_addend": 1,
    "descriptors": [
        {
            "entries": [
                {
                    "key": "req.method",
                    "value": "POST"
                }
            ]
        }
    ]
}
EOM

The expected output

{
  "overallCode": "OK"
}
{
  "overallCode": "OK"
}
{
  "overallCode": "OK"
}
{
  "overallCode": "OK"
}
{
  "overallCode": "OK"
}
{
  "overallCode": "OVER_LIMIT"
}
{
  "overallCode": "OVER_LIMIT"
}
{
  "overallCode": "OVER_LIMIT"
}

@eguzki eguzki marked this pull request as ready for review November 9, 2023 23:04
@eguzki eguzki requested a review from a team November 9, 2023 23:04
@alexsnaps alexsnaps added this to the Limitador Server v1.4.0 milestone Nov 20, 2023
Server::builder()
.add_service(svc)
.add_service(reflection_service)
Copy link
Member

Choose a reason for hiding this comment

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

Thinking we may want to add this when not building for target release. We can check for debug_assertions. Or introduce it as a feature? I guess this isn't meant for a production deployment, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, makes sense. I will give it a try

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented as command line option, which defaults to not being exposed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The downside of this implementation is that the build will include tonic-reflection crate now. The feature approach could save us doing that. I think it is handier to have a command line option instead of custom compilation to the reflection service working. Let me know what you think,

@eguzki eguzki requested a review from alexsnaps December 19, 2023 16:30
@eguzki eguzki merged commit 5e44784 into main Jan 16, 2024
18 checks passed
@eguzki eguzki deleted the grpc-reflection branch January 16, 2024 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants