Skip to content

local_ratelimit: support return x-ratelimit-reset header#39501

Merged
wbpcode merged 13 commits intoenvoyproxy:mainfrom
zirain:local_ratelimit/header
May 22, 2025
Merged

local_ratelimit: support return x-ratelimit-reset header#39501
wbpcode merged 13 commits intoenvoyproxy:mainfrom
zirain:local_ratelimit/header

Conversation

@zirain
Copy link
Member

@zirain zirain commented May 16, 2025

Commit Message: local_ratelimit: support return x-ratelimit-reset header
Additional Description:
Risk Level:
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]

zirain added 4 commits May 16, 2025 10:29
Signed-off-by: zirain <zirain2009@gmail.com>
Signed-off-by: zirain <zirain2009@gmail.com>
Signed-off-by: zirain <zirain2009@gmail.com>
Signed-off-by: zirain <zirain2009@gmail.com>
@zirain
Copy link
Member Author

zirain commented May 16, 2025

flaky:

2025-05-16T03:11:49.9787190Z Execution result: https://mordenite.cluster.engflow.com/actions/executions/ChAmXP45qDJWrpR0b37XcUqXEgdkZWZhdWx0GiUKIHCjYFbT3RbiiSvr5Yjew1YkofvFTdls8M5FGwrTOpagEMQD
2025-05-16T03:11:49.9787963Z ================================================================================
2025-05-16T03:19:36.0289328Z �[32mINFO: �[0mFound 1290 test targets...
2025-05-16T03:19:36.2123657Z �[32mINFO: �[0mElapsed time: 1359.914s, Critical Path: 1031.41s
2025-05-16T03:19:36.2125177Z �[32mINFO: �[0m24897 processes: 11124 remote cache hit, 12686 internal, 6 processwrapper-sandbox, 1081 remote.
2025-05-16T03:19:36.2157057Z �[32mINFO: �[0mBuild completed, 1 test FAILED, 24897 total actions
2025-05-16T03:19:36.2355727Z //test/extensions/filters/http/dynamic_forward_proxy:legacy_proxy_filter_integration_test �[0m�[31m�[1mFAILED�[0m in 25.7s
2025-05-16T03:19:36.2363118Z   /build/bazel_root/base/execroot/envoy/bazel-out/k8-fastbuild/testlogs/test/extensions/filters/http/dynamic_forward_proxy/legacy_proxy_filter_integration_test/test.log

@zirain
Copy link
Member Author

zirain commented May 16, 2025

/retest

Signed-off-by: zirain <zirain2009@gmail.com>
Copy link
Member

@agrawroh agrawroh left a comment

Choose a reason for hiding this comment

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

Left some comments, looks good otherwise.

zirain added 2 commits May 16, 2025 21:02
Signed-off-by: zirain <zirain2009@gmail.com>
Signed-off-by: zirain <zirain2009@gmail.com>
@zirain zirain requested review from tyxia and yanavlasov as code owners May 16, 2025 14:32
zirain added 2 commits May 17, 2025 07:41
Signed-off-by: zirain <zirain2009@gmail.com>
Signed-off-by: zirain <zirain2009@gmail.com>
@zirain
Copy link
Member Author

zirain commented May 17, 2025

/retest

1 similar comment
@zirain
Copy link
Member Author

zirain commented May 17, 2025

/retest

agrawroh
agrawroh previously approved these changes May 17, 2025
Copy link
Member

@agrawroh agrawroh left a comment

Choose a reason for hiding this comment

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

Looks good! Should we also add a blurb on the Local RateLimit Docs to capture this?

@agrawroh
Copy link
Member

/assign @wbpcode

Signed-off-by: zirain <zirain2009@gmail.com>
@repokitteh-read-only
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: #39501 was synchronize by zirain.

see: more, trace.

Signed-off-by: zirain <zirain2009@gmail.com>
@zirain zirain requested a review from wbpcode May 20, 2025 11:21
@tyxia
Copy link
Member

tyxia commented May 20, 2025

/wait

Signed-off-by: zirain <zirain2009@gmail.com>
@zirain
Copy link
Member Author

zirain commented May 21, 2025

/retest

1 similar comment
@zirain
Copy link
Member Author

zirain commented May 21, 2025

/retest

@zirain zirain requested a review from wbpcode May 21, 2025 05:02
Comment on lines +107 to +118
std::chrono::milliseconds AtomicTokenBucketImpl::nextTokenAvailable() const {
// If there are tokens available, return immediately.
if (remainingTokens() >= 1) {
return std::chrono::milliseconds(0);
}

// Calculate time since the last fill.
double current_time = timeNowInSeconds();
double last_time = time_in_seconds_.load();
return std::chrono::milliseconds(
static_cast<uint64_t>(((1 / fill_rate_ - (current_time - last_time)) * 1000)));
}
Copy link
Member

Choose a reason for hiding this comment

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

Although it's will be a very very very trival case, but it's possible when we check remainingTokens(), the remaining tokens are less then 1. Then, when we calculate the next available token, time passes, the 1 / fill_rate_ - (current_time - last_time) may return a minus value.

Comment on lines +107 to +118
std::chrono::milliseconds AtomicTokenBucketImpl::nextTokenAvailable() const {
// If there are tokens available, return immediately.
if (remainingTokens() >= 1) {
return std::chrono::milliseconds(0);
}

// Calculate time since the last fill.
double current_time = timeNowInSeconds();
double last_time = time_in_seconds_.load();
return std::chrono::milliseconds(
static_cast<uint64_t>(((1 / fill_rate_ - (current_time - last_time)) * 1000)));
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
std::chrono::milliseconds AtomicTokenBucketImpl::nextTokenAvailable() const {
// If there are tokens available, return immediately.
if (remainingTokens() >= 1) {
return std::chrono::milliseconds(0);
}
// Calculate time since the last fill.
double current_time = timeNowInSeconds();
double last_time = time_in_seconds_.load();
return std::chrono::milliseconds(
static_cast<uint64_t>(((1 / fill_rate_ - (current_time - last_time)) * 1000)));
}
std::chrono::milliseconds AtomicTokenBucketImpl::nextTokenAvailable() const {
// If there are tokens available, return immediately.
const double remaining_tokens = remainingTokens();
if (remaining_tokens >= 1) {
return std::chrono::milliseconds(0);
}
// Calculate time since the last fill.
return std::chrono::milliseconds(
static_cast<uint64_t>(((1 - remaining_tokens) / fill_rate_) * 1000));
}

Signed-off-by: zirain <zirain2009@gmail.com>
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.

LGTM. Thanks.

@wbpcode wbpcode enabled auto-merge (squash) May 22, 2025 03:02
@wbpcode wbpcode merged commit a40cebe into envoyproxy:main May 22, 2025
26 checks passed
@zirain zirain deleted the local_ratelimit/header branch May 22, 2025 04:13
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