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

http ratelimit: option to reduce budget on stream done #37548

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

mathetake
Copy link
Member

@mathetake mathetake commented Dec 6, 2024

Commit Message: ratelimit: option to excute action on stream done

Additional Description:
This adds a new option apply_on_stream_done to the Action
message of the http ratelimit. This basically allows to configure
actions to be executed in a response content-aware way and do not
enforce the rate limit (in other words "fire-and-forget"). Since addend
can be currently controlled via envoy.ratelimit.hits_addend metadata,
another filter can be used to set the value to reflect their intent there,
for example, by using Lua or Ext Proc filters.

This use case arises from the LLM API services which usually return
the usage statistics in the response body. More specifically,
they have "streaming" APIs whose response is a line-by-line event
stream where the very last line of the response line contains the
usage statistics. The lazy nature of this action is perfectly fine
as in these use cases, the rate limit happens like "you are forbidden
from the next time".

Besides the LLM specific, I've also encountered the use case from the
data center resource allocation case where the operators want to
"block the computation from the next time since you used this much
resources in this request".

Ref: envoyproxy/gateway#4756

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

Copy link

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #37548 was opened by mathetake.

see: more, trace.

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 @wbpcode
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #37548 was opened by mathetake.

see: more, trace.

@mathetake mathetake marked this pull request as ready for review December 6, 2024 22:10
@mathetake
Copy link
Member Author

mathetake commented Dec 6, 2024

i guess the impl can be a bit large, so I might do that in separate PRs - anyways will think about it after the API gets approved

Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
@wbpcode
Copy link
Member

wbpcode commented Dec 7, 2024

wow, we have a similar requirement internally and I finally figured out a similar way. It is super surprised and happy to see this.

@mathetake
Copy link
Member Author

cool glad to hear that you came to the similar idea!

Signed-off-by: Takeshi Yoneda <[email protected]>
…nd for future extension

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake
Copy link
Member Author

@wbpcode thank you for the valuable feedback offline! I think I will go ahead and try implementing the idea - i don't think the change won't be that huge

Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake mathetake changed the title http ratelimit: option to excute action on stream done ratelimit: option to excute action on stream done Dec 9, 2024
@mathetake

This comment was marked as outdated.

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake mathetake changed the title ratelimit: option to excute action on stream done http ratelimit: option to excute action on stream done Dec 9, 2024
@mathetake

This comment was marked as outdated.

@mathetake mathetake changed the title http ratelimit: option to excute action on stream done http ratelimit: option to reduce budget on stream done Dec 9, 2024
Copy link
Contributor

@arkodg arkodg left a comment

Choose a reason for hiding this comment

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

from an API perspective, LGTM thanks ! This would help users be able to also count based response attributes into the same RL bucket

@mathetake
Copy link
Member Author

now i am working on polishing the impl...

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake
Copy link
Member Author

@wbpcode i feel like the impl is ready for review- much simpler than I thought. Could you review that? and meanwhile I will add more tests

Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Copy link
Member

@wbpcode wbpcode left a comment

Choose a reason for hiding this comment

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

Thanks for this great contribution and sorry for the delay. Add a comment to the API.

/wait

Comment on lines +137 to +156

// If true, rate limit requests will also be sent to the rate limit service when the stream completes.
// This is useful when the rate limit budget needs to reflect the response context that is not available
// on the request path.
//
// On the stream completion, the filter will reuse the exact same descriptors matched during the request path.
// In other words, the descriptors are not recalculated on the stream completion, but the rate limit requests
// are sent with the same descriptors as the original request sent during the request path.
// For example, request header matching descriptors are available on the stream completion.
//
// For example, let's say the upstream service calculates the usage statistics, returns them in the response body
// and we want to utilize these numbers to apply the rate limit action for the subsequent requests.
// Combined with another filter that can set ``envoy.ratelimit.hits_addend`` based on the response (e.g. Lua filter),
// this can be used to subtract the usage statistics from the rate limit budget.
//
// The rate limit requests sent on the stream completion are "fire-and-forget" by nature, and rate limit is not enforced
// on the current HTTP stream being completed. The filter will only update the budget for the subsequent requests at
// that point. Hence the effect of the rate limit requests made during the stream completion is not visible in the current
// but only in the subsequent requests.
bool apply_on_stream_done = 14;
Copy link
Member

Choose a reason for hiding this comment

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

  1. Rather than a filter level flag, I think it should be per-descriptor level (in the route.v3.RateLimit). Then different routes could have different choice. And more important, different resource could also have different choice. Like the qps limit need only to works at request path but only the ai tokens limit need to works at response.

  2. This semantics should be clear: request or response, but not for both. When we populate the descritpors, we can tell the the populateDescritors() what phase is and lets the populateDescritpors() only populate related descritpros. This bring a complexity that the users need to configure two descritpors: one for request with 0 or 1 hits addend for check only, one for response with CUSTOM_HITS_ADDEND or CUSTOM_HITS_ADDEND - 1 for report. But the semantics is more clear and behavior is more predictable. If we let same descritpor works on both request and response, we must ensure we can get correct different values for hits_addend from same source, that's weird.

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

Successfully merging this pull request may close these issues.

3 participants