-
Notifications
You must be signed in to change notification settings - Fork 5.3k
common: add a thread safe token bucket #14827
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
43b002f
Make token bucket optionally thread-safe
nitgoy 9e3e285
add synch test
nitgoy b84a6f1
format fix
nitgoy 2a0a9f4
remove absl guard macros
nitgoy 27b8d4e
create separate wrapper class
nitgoy 1a104ed
fix: format, cloud build err, test class name
nitgoy d29d47a
address comments
nitgoy 8069575
Merge branch 'master' of github.com:nitgoy/envoy into tokenbucket
nitgoy 383fc73
add combined consume and next token method
nitgoy 5b0cb4c
build fix
nitgoy 4d7b7d1
clang tidy fix
nitgoy 1f4e326
test new interface by default
nitgoy 1414692
rename reset method
nitgoy 606d8b9
minor: wording
nitgoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include "common/common/shared_token_bucket_impl.h" | ||
|
|
||
| #include <chrono> | ||
|
|
||
| #include "common/common/lock_guard.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| const char SharedTokenBucketImpl::GetImplSyncPoint[] = "pre_get_impl"; | ||
| const char SharedTokenBucketImpl::ResetCheckSyncPoint[] = "post_reset_check"; | ||
|
|
||
| SharedTokenBucketImpl::SharedTokenBucketImpl(uint64_t max_tokens, TimeSource& time_source, | ||
| double fill_rate) | ||
| : impl_(max_tokens, time_source, fill_rate), reset_once_(false) {} | ||
|
|
||
| uint64_t SharedTokenBucketImpl::consume(uint64_t tokens, bool allow_partial) { | ||
| Thread::LockGuard lock(mutex_); | ||
| synchronizer_.syncPoint(GetImplSyncPoint); | ||
| return impl_.consume(tokens, allow_partial); | ||
| }; | ||
|
|
||
| uint64_t SharedTokenBucketImpl::consume(uint64_t tokens, bool allow_partial, | ||
| std::chrono::milliseconds& time_to_next_token) { | ||
| Thread::LockGuard lock(mutex_); | ||
| synchronizer_.syncPoint(GetImplSyncPoint); | ||
| return impl_.consume(tokens, allow_partial, time_to_next_token); | ||
| }; | ||
|
|
||
| std::chrono::milliseconds SharedTokenBucketImpl::nextTokenAvailable() { | ||
| Thread::LockGuard lock(mutex_); | ||
| synchronizer_.syncPoint(GetImplSyncPoint); | ||
| return impl_.nextTokenAvailable(); | ||
| }; | ||
|
|
||
| void SharedTokenBucketImpl::maybeReset(uint64_t num_tokens) { | ||
| Thread::LockGuard lock(mutex_); | ||
| // Don't reset if reset once before. | ||
| if (reset_once_) { | ||
| return; | ||
| } | ||
| reset_once_ = true; | ||
| synchronizer_.syncPoint(ResetCheckSyncPoint); | ||
| impl_.maybeReset(num_tokens); | ||
| }; | ||
|
|
||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| #pragma once | ||
|
|
||
| #include "common/common/thread.h" | ||
| #include "common/common/thread_synchronizer.h" | ||
| #include "common/common/token_bucket_impl.h" | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| /** | ||
| * A thread-safe wrapper class for TokenBucket interface. | ||
| */ | ||
| class SharedTokenBucketImpl : public TokenBucket { | ||
| public: | ||
| static const char GetImplSyncPoint[]; | ||
| static const char ResetCheckSyncPoint[]; | ||
| /** | ||
| * @param max_tokens supplies the maximum number of tokens in the bucket. | ||
| * @param time_source supplies the time source. | ||
| * @param fill_rate supplies the number of tokens that will return to the bucket on each second. | ||
| * The default is 1. | ||
| * @param mutex supplies the mutex object to be used to ensure thread-safety when the token bucket | ||
| * is shared. By default the class will be thread-unsafe. | ||
| */ | ||
| explicit SharedTokenBucketImpl(uint64_t max_tokens, TimeSource& time_source, | ||
| double fill_rate = 1); | ||
|
|
||
| SharedTokenBucketImpl(const SharedTokenBucketImpl&) = delete; | ||
| SharedTokenBucketImpl(SharedTokenBucketImpl&&) = delete; | ||
|
|
||
| // TokenBucket | ||
|
|
||
| uint64_t consume(uint64_t tokens, bool allow_partial) override; | ||
| uint64_t consume(uint64_t tokens, bool allow_partial, | ||
| std::chrono::milliseconds& time_to_next_token) override; | ||
| std::chrono::milliseconds nextTokenAvailable() override; | ||
|
|
||
| /** | ||
nitgoy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Since the token bucket is shared, only the first reset call will work. | ||
| * Subsequent calls to reset method will be ignored. | ||
| */ | ||
| void maybeReset(uint64_t num_tokens) override; | ||
|
|
||
| private: | ||
| Thread::MutexBasicLockable mutex_; | ||
| TokenBucketImpl impl_ ABSL_GUARDED_BY(mutex_); | ||
| bool reset_once_ ABSL_GUARDED_BY(mutex_); | ||
| mutable Thread::ThreadSynchronizer synchronizer_; // Used only for testing. | ||
| friend class SharedTokenBucketImplTest; | ||
| }; | ||
|
|
||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.