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

ratelimit: new per descriptor hits-addend support and dynamic hits addend #37567

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wbpcode
Copy link
Member

@wbpcode wbpcode commented Dec 9, 2024

Commit Message: api: new per descriptor hits-addend support and dynamic hits addend
Additional Description:

  1. Now, we could get custom hits_addend from the envoy.ratelimit.hits_addend. But if there are multiple rate limit filters that requrie custom hits_addend, the envoy.ratelimit.hits_addend couldn't meet the requirement.

  2. And we cann't also to support different hits_addend for diffferent descriptots in same request.

This API changes try to meet above two requirements.

Risk Level: low.
Testing: n/a.
Docs Changes: n/a.
Release Notes: n/a.
Platform Specific Features: n/a.

Copy link

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/).
envoyproxy/api-shepherds assignee is @abeyad
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #37567 was opened by wbpcode.

see: more, trace.

Copy link
Contributor

@abeyad abeyad left a comment

Choose a reason for hiding this comment

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

@wbpcode should we add the implementation using this API to the PR so we can see how it'll be used?

// For example, the ``%BYTES_RECEIVED%`` format string will be replaced with the number of bytes
// received in the request.
//
// Only one of the ``number`` or ``format`` fields can be set.
Copy link
Contributor

Choose a reason for hiding this comment

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

what will happen if both are set?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is oneof semantics. (although oneof is not recommend according to our style).

If both are set, the configuration will be rejected.

// Optional hits_addend for the rate limit descriptor. If set the value will override the
// request level hits_addend.
// [#not-implemented-hide:]
google.protobuf.UInt32Value hits_addend = 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

In the request level, it can be an integer or a format string. How come here it is only an integer?

Copy link
Member Author

Choose a reason for hiding this comment

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

The format string is used to extract a number based on the request and stream info. See the comments of format string comment

    // Substitution format string to extract the number of hits to add to the rate limit descriptor.
    // The same :ref:`format specifier <config_access_log_format>` as used for
    // :ref:`HTTP access logging <config_access_log>` applies here.
    //
    // .. note::
    //   The format string must contain only single valid substitution field that will be replaced
    //   with a non-negative number.
    //
    // For example, the ``%BYTES_RECEIVED%`` format string will be replaced with the number of bytes
    // received in the request.

@wbpcode
Copy link
Member Author

wbpcode commented Dec 10, 2024

@wbpcode should we add the implementation using this API to the PR so we can see how it'll be used?

SGTM.

@wbpcode wbpcode changed the title api: new per descriptor hits-addend support and dynamic hits addend ratelimit: new per descriptor hits-addend support and dynamic hits addend Dec 10, 2024
@wbpcode
Copy link
Member Author

wbpcode commented Dec 10, 2024

cc @abeyad I complete the local ratelimit version of the per descriptor custom hits addend support.

When the request is coming, we will generated a list of descriptors based on the configuration. If the hits_addend is configured, the filter will also generate a dynamic hits_addend value based on the request info and configuration.

When a descriptor matchs a rule, in the previous implementation, the fixed value 1 will be used as hits addend. Now, in the new implementation, the custom hits_addend that generated in the previous step will be used.

Comment on lines 78 to 87
/**
* A single rate limit request descriptor. See ratelimit.proto.
* This is generated from the request based on the configured rate limit actions.
*/
struct Descriptor : public DescriptorBase {
absl::optional<RateLimitOverride> limit_ = absl::nullopt;
absl::optional<uint32_t> hits_addend_ = absl::nullopt;
};

using LocalDescriptor = DescriptorBase;
Copy link
Member Author

@wbpcode wbpcode Dec 10, 2024

Choose a reason for hiding this comment

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

In the previous implementation, the class LocalDescriptor has two usages:

  1. as the key of rate limit rules that every descriptor is related to a token bucket.
  2. as the output of the descriptor populating of local rate limt filter. It will be used to find a matched rate limit rule.

But now, to support per descriptor hits_addend, we need a new class to represent the output of the descriptor populating. Finally, we choose to enhance and re-use the Descriptor class. (The Descriptor class is used as the output of global rate limit filter. Re-using it could also simplify future development when we want to add similar support for global rate limit filter in the future.)

@wbpcode wbpcode force-pushed the dev-per-descriptor-hits-adden-support branch from 2e2b2c3 to dc93699 Compare December 10, 2024 16:36
@wbpcode
Copy link
Member Author

wbpcode commented Dec 10, 2024

/retest

abeyad
abeyad previously approved these changes Dec 10, 2024
Copy link
Contributor

@abeyad abeyad left a comment

Choose a reason for hiding this comment

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

/lgtm api

Override limit = 4;

// An optional hits addend to be appended to the descriptor produced by this rate limit
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of appending to the descriptor, it should be used to populate the hits_addend field in the RLS request
https://www.envoyproxy.io/docs/envoy/latest/api-v3/service/ratelimit/v3/rls.proto#service-ratelimit-v3-ratelimitrequest

Copy link
Member Author

@wbpcode wbpcode Dec 11, 2024

Choose a reason for hiding this comment

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

Single RateLimit message is used to generate a single descriptor. The RateLimit.hits_addend field here is also a descriptor level configuration. So, the generated value of RateLimit.hits_addend field here should also be populated to the per-descriptor Descriptor.hits_addend rather than request level RateLimitRequest.hits_addend.

Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -2168,16 +2169,47 @@ message RateLimit {
}
}

message HitsAddend {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

sorry, but I didn't get what you mean. 🤣 This HitsAddend is only used to configure single fixed number of a dynamic formatter provider.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah what I was getting at, is there any anything existing thats reusable ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@wbpcode
Copy link
Member Author

wbpcode commented Dec 11, 2024

Hi, @mattklein123 , could you take a look when you get some free time? Thansk. This PR only contain local-ratelimit related change. And I will create a new PR to support global rate limit after this is done.

@wbpcode wbpcode force-pushed the dev-per-descriptor-hits-adden-support branch from 18489a3 to 774066d Compare December 11, 2024 16:32
@wbpcode
Copy link
Member Author

wbpcode commented Dec 11, 2024

Finally, reduced 50% code changes. I have tried my best to reduce the complexity of review. orz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants