-
Notifications
You must be signed in to change notification settings - Fork 5.3k
grpc: implement TTL manager to re-buffer pending messages for bidirectional gRPC #18987
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
snowp
merged 12 commits into
envoyproxy:main
from
Shikugawa:buffered-message-ttl-manager
Feb 8, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1fca9d1
grpc: implement TTL manager to track buffered bidirectional gRPC mess…
Shikugawa 3fe2bbd
add test
Shikugawa ffd38ce
add test
Shikugawa 5843d85
fix
Shikugawa dad969a
fix
Shikugawa 0d6de8b
fix
Shikugawa 0fdedac
fix
Shikugawa 8fb11f7
fix
Shikugawa 709afff
tidy
Shikugawa ed4434b
fix
Shikugawa a6445b8
fix
Shikugawa 2b6919e
fix
Shikugawa 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,74 @@ | ||
| #pragma once | ||
|
|
||
| #include <queue> | ||
|
|
||
| #include "envoy/event/dispatcher.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Grpc { | ||
|
|
||
| using BufferedMessageExpirationCallback = std::function<void(uint64_t)>; | ||
|
|
||
| // This class is used to manage the TTL for pending uploads within a BufferedAsyncClient. Multiple | ||
| // IDs can be inserted into the TTL manager at once, all with the same TTL (specified in the | ||
| // constructor). Upon expiry, the TTL manager will invoke the provided expiry callback for each ID. | ||
| // Note that there is no way to disable the expiration, and so it's up to the recipient of the | ||
| // callback to handle this. BufferedAsyncClient will do the right thing here: if the expired ID is | ||
| // still in flight it will be returned to the buffer, otherwise it does nothing. The TTL manager is | ||
| // designed to handle multiple sets of IDs inserted at various times, backing this with a single | ||
| // Timer. This allows us to track a large amount of IDs inserted at different times without using a | ||
| // lot of different timers, which could put undue pressure on the event loop. | ||
| class BufferedMessageTtlManager { | ||
| public: | ||
| BufferedMessageTtlManager(Event::Dispatcher& dispatcher, | ||
| BufferedMessageExpirationCallback&& expiry_callback, | ||
| std::chrono::milliseconds message_ack_timeout) | ||
| : dispatcher_(dispatcher), message_ack_timeout_(message_ack_timeout), | ||
| expiry_callback_(expiry_callback), | ||
| timer_(dispatcher_.createTimer([this] { checkExpiredMessages(); })) {} | ||
|
|
||
| ~BufferedMessageTtlManager() { timer_->disableTimer(); } | ||
|
|
||
| void addDeadlineEntry(const absl::flat_hash_set<uint64_t>& ids) { | ||
| const auto expires_at = dispatcher_.timeSource().monotonicTime() + message_ack_timeout_; | ||
| deadline_.emplace(expires_at, std::move(ids)); | ||
|
|
||
| if (!timer_->enabled()) { | ||
| timer_->enableTimer(message_ack_timeout_); | ||
| } | ||
| } | ||
|
|
||
| const std::queue<std::pair<MonotonicTime, absl::flat_hash_set<uint64_t>>>& deadlineForTest() { | ||
| return deadline_; | ||
| } | ||
|
|
||
| private: | ||
| void checkExpiredMessages() { | ||
| const auto now = dispatcher_.timeSource().monotonicTime(); | ||
|
|
||
| while (!deadline_.empty()) { | ||
| auto& it = deadline_.front(); | ||
| if (it.first > now) { | ||
| break; | ||
| } | ||
| for (auto&& id : it.second) { | ||
| expiry_callback_(id); | ||
| } | ||
| deadline_.pop(); | ||
| } | ||
|
|
||
| if (!deadline_.empty()) { | ||
| const auto earliest_timepoint = deadline_.front().first; | ||
| timer_->enableTimer( | ||
| std::chrono::duration_cast<std::chrono::milliseconds>(earliest_timepoint - now)); | ||
| } | ||
| } | ||
|
|
||
| Event::Dispatcher& dispatcher_; | ||
| std::chrono::milliseconds message_ack_timeout_; | ||
| BufferedMessageExpirationCallback expiry_callback_; | ||
| Event::TimerPtr timer_; | ||
| std::queue<std::pair<MonotonicTime, absl::flat_hash_set<uint64_t>>> deadline_; | ||
| }; | ||
| } // namespace Grpc | ||
| } // 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
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.