common: add a thread safe token bucket#14827
Conversation
Signed-off-by: Nitin <nigoyal@microsoft.com>
Signed-off-by: Nitin <nigoyal@microsoft.com>
Signed-off-by: Nitin <nigoyal@microsoft.com>
|
Hi @nitgoy, welcome and thank you for your contribution. We will try to review your Pull Request as quickly as possible. In the meantime, please take a look at the contribution guidelines if you have not done so already. |
Signed-off-by: Nitin <nigoyal@microsoft.com>
|
I think it would be cleaner to add a wrapper class around the existing token bucket class to add locking, instead of inserting optional locking into it. |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Signed-off-by: Nitin <nigoyal@microsoft.com>
Done. It looks like ABSL_EXCLUSIVE_LOCKS_REQUIRED is not acquiring lock for me locally but it does in the CodeQL-build. That indicates the exclusive_locks_required ABSL attribute is not set. Shouldn't it be automatically set for the bazel workspace? |
Signed-off-by: Nitin <nigoyal@microsoft.com>
That annotation allows the compiler to verify that the correct lock is held everytime you access a field. It does not automatically acquire the lock for you. It's only used for static code analysis. |
I thought so too but I see below CodeQL-build errors if I keep it along with the actual LockGuard (see previous iteration). The build passes if I remove the macro. Is this the right macro? source/common/common/shared_token_bucket_impl.cc:18:21: error: acquiring mutex 'mutex_' that is already held [-Werror,-Wthread-safety-analysis] |
|
Before we go further with this review, can you explain what this code will be used for? Oftentimes it's easier to make changes like this in the context that it will be used, so the requirements/use-case are clearer. |
This is the PR for that will use it: #14096. Essentially, I need it to represent token bucket for per filter-chain/listener limit for bandwidth limit filter that will span across multiple http streams. |
ggreenway
left a comment
There was a problem hiding this comment.
High-level question: is this wrapper needed, or would it be better to just have a mutex and a TokenBucketImpl in the class that needs it? Will the owning class ever need to manipulate the token bucket and some other state atomically under the same lock?
/wait
Signed-off-by: Nitin <nigoyal@microsoft.com>
The interface methods will be called by the common class |
|
Looking at this, and how it's used in the stream limit filter, I think you need to change the API to make I'm not sure that just adding a mutex around each call on the TokenBucket interface is sufficient for what you're trying to accomplish. /wait-any |
Signed-off-by: Nitin <nigoyal@microsoft.com>
Thanks for catching this issue. I'll try adding a test around it. Does it make sense to change the check right after The other approach would be to have the mutex in streamratelimiter class as you suggested but has other downsides as I stated before. |
|
I think it makes most sense to add a method to the interface to get the two values atomically. |
Signed-off-by: Nitin <nigoyal@microsoft.com>
Signed-off-by: Nitin <nigoyal@microsoft.com>
Made the suggested changes. Can you please take a look? Working on fixing these other issues. |
Signed-off-by: Nitin <nigoyal@microsoft.com>
Signed-off-by: Nitin Goyal <nigoyal@microsoft.com>
|
Commenter does not have sufficient privileges for PR 14827 in repo envoyproxy/envoy |
|
Retrying Azure Pipelines: |
Signed-off-by: Nitin Goyal <nigoyal@microsoft.com>
Commit Message: Add a thread-safe token bucket wrapper class to be shared across threads. The other token bucket implementation was not thread safe which made it unusable for certain cases. This wrapper class calls the thread-unsafe class for the core token bucket functionality.
Additional Description: New class, not used anywhere else yet except UTs.
Risk Level: Low
Testing: Ran existing and newly added UTs.
Docs Changes: None
Release Notes:
Platform Specific Features: