-
Notifications
You must be signed in to change notification settings - Fork 5.5k
local_ratelimit: support return x-ratelimit-reset header
#39501
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
Changes from 9 commits
72331b4
dad3403
57a5bea
75c20a1
871c82e
fc9fe5f
328d697
9c6fb40
7f32917
23ca425
4993162
6b4b3ff
f0156cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ namespace Envoy { | |||||||||||||||||||||||||||||||||||||||||||||||
| namespace { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // The minimal fill rate will be one second every year. | ||||||||||||||||||||||||||||||||||||||||||||||||
| constexpr double kMinFillRate = 1.0 / (365 * 24 * 60 * 60); | ||||||||||||||||||||||||||||||||||||||||||||||||
| constexpr auto MILLISECONDS_PER_SECOND = 1000; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } // namespace | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -59,13 +60,16 @@ void TokenBucketImpl::maybeReset(uint64_t num_tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| AtomicTokenBucketImpl::AtomicTokenBucketImpl(uint64_t max_tokens, TimeSource& time_source, | ||||||||||||||||||||||||||||||||||||||||||||||||
| std::chrono::milliseconds fill_interval, | ||||||||||||||||||||||||||||||||||||||||||||||||
| double fill_rate, bool init_fill) | ||||||||||||||||||||||||||||||||||||||||||||||||
| : AtomicTokenBucketImpl::AtomicTokenBucketImpl(max_tokens, time_source, fill_rate, | ||||||||||||||||||||||||||||||||||||||||||||||||
| (init_fill) ? max_tokens : 0) {} | ||||||||||||||||||||||||||||||||||||||||||||||||
| : AtomicTokenBucketImpl::AtomicTokenBucketImpl(max_tokens, time_source, fill_interval, | ||||||||||||||||||||||||||||||||||||||||||||||||
| fill_rate, (init_fill) ? max_tokens : 0) {} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| AtomicTokenBucketImpl::AtomicTokenBucketImpl(uint64_t max_tokens, TimeSource& time_source, | ||||||||||||||||||||||||||||||||||||||||||||||||
| std::chrono::milliseconds fill_interval, | ||||||||||||||||||||||||||||||||||||||||||||||||
| double fill_rate, uint64_t initial_tokens) | ||||||||||||||||||||||||||||||||||||||||||||||||
| : max_tokens_(max_tokens), fill_rate_(std::max(std::abs(fill_rate), kMinFillRate)), | ||||||||||||||||||||||||||||||||||||||||||||||||
| fill_interval_(std::chrono::duration<double>(fill_interval).count()), | ||||||||||||||||||||||||||||||||||||||||||||||||
| time_source_(time_source) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto time_in_seconds = timeNowInSeconds(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (initial_tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -103,4 +107,18 @@ double AtomicTokenBucketImpl::timeNowInSeconds() const { | |||||||||||||||||||||||||||||||||||||||||||||||
| return std::chrono::duration<double>(time_source_.monotonicTime().time_since_epoch()).count(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| 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 time_since_last_fill = std::fmod(current_time, fill_interval_); | ||||||||||||||||||||||||||||||||||||||||||||||||
| double time_until_next_fill = fill_interval_ - time_since_last_fill; | ||||||||||||||||||||||||||||||||||||||||||||||||
| return std::chrono::milliseconds( | ||||||||||||||||||||||||||||||||||||||||||||||||
| static_cast<uint64_t>(time_until_next_fill * MILLISECONDS_PER_SECOND)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
zirain marked this conversation as resolved.
Comment on lines
+107
to
+116
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Comment on lines
+107
to
+116
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| } // namespace Envoy | ||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.