-
Notifications
You must be signed in to change notification settings - Fork 5.5k
async client: introduce AsyncClientRequestTracker to keep track of inflight requests and cancel them on destruction
#10359
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
mattklein123
merged 9 commits into
envoyproxy:master
from
yskopets:feature/introduce-async-request-tracker
Mar 18, 2020
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9117dbd
async client: introduce `AsyncClientRequestTracker` to keep track of …
yskopets 943a499
Kick CI
yskopets 3136d4d
code review: use regular methods instead of operator overloading
yskopets 6c986b3
code review: use `absl::flat_hash_set` instead of `std::unordered_set`
yskopets 1d04f5f
code review: change return type to `void`
yskopets efeab01
code review: use references instead of pointers
yskopets 6f50a54
Kick CI
yskopets cdc881f
code review: add comments
yskopets 31d55a4
unit tests: add missing unit test
yskopets 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #include "common/http/async_client_utility.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Http { | ||
|
|
||
| AsyncClientRequestTracker::~AsyncClientRequestTracker() { | ||
| for (auto* active_request : active_requests_) { | ||
| active_request->cancel(); | ||
| } | ||
| } | ||
|
|
||
| void AsyncClientRequestTracker::add(AsyncClient::Request& request) { | ||
| ASSERT(active_requests_.find(&request) == active_requests_.end()); | ||
| active_requests_.insert(&request); | ||
| } | ||
|
|
||
| void AsyncClientRequestTracker::remove(const AsyncClient::Request& request) { | ||
| auto it = active_requests_.find(const_cast<AsyncClient::Request*>(&request)); | ||
| // Support a use case where request callbacks might get called prior to a request handle | ||
| // is returned from AsyncClient::send(). | ||
| if (it != active_requests_.end()) { | ||
| active_requests_.erase(it); | ||
| } | ||
| } | ||
|
|
||
| } // namespace Http | ||
| } // 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,34 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/http/async_client.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Http { | ||
|
|
||
| /** | ||
| * Keeps track of active async HTTP requests to be able to cancel them on destruction. | ||
| */ | ||
| class AsyncClientRequestTracker { | ||
| public: | ||
| /** | ||
| * Cancels all known active async HTTP requests. | ||
| */ | ||
| ~AsyncClientRequestTracker(); | ||
| /** | ||
| * Includes a given async HTTP request into a set of known active requests. | ||
| * @param request request handle | ||
| */ | ||
| void add(AsyncClient::Request& request); | ||
| /** | ||
| * Excludes a given async HTTP request from a set of known active requests. | ||
| * @param request request handle | ||
| */ | ||
| void remove(const AsyncClient::Request& request); | ||
|
|
||
| private: | ||
| // Track active async HTTP requests to be able to cancel them on destruction. | ||
| absl::flat_hash_set<AsyncClient::Request*> active_requests_; | ||
| }; | ||
|
|
||
| } // namespace Http | ||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include "common/http/async_client_utility.h" | ||
|
|
||
| #include "test/mocks/http/mocks.h" | ||
|
|
||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using testing::NiceMock; | ||
| using testing::StrictMock; | ||
|
|
||
| namespace Envoy { | ||
| namespace Http { | ||
| namespace { | ||
|
|
||
| class AsyncClientRequestTrackerTest : public testing::Test { | ||
| public: | ||
| std::unique_ptr<AsyncClientRequestTracker> active_requests_{ | ||
| std::make_unique<AsyncClientRequestTracker>()}; | ||
|
|
||
| NiceMock<MockAsyncClient> async_client_; | ||
| StrictMock<MockAsyncClientRequest> request1_{&async_client_}; | ||
| StrictMock<MockAsyncClientRequest> request2_{&async_client_}; | ||
| StrictMock<MockAsyncClientRequest> request3_{&async_client_}; | ||
| }; | ||
|
|
||
| TEST_F(AsyncClientRequestTrackerTest, OnDestructDoNothingIfThereAreNoActiveRequests) { | ||
| // Trigger destruction. | ||
| active_requests_.reset(); | ||
| } | ||
|
|
||
| TEST_F(AsyncClientRequestTrackerTest, OnDestructCancelActiveRequests) { | ||
| // Include active requests. | ||
| active_requests_->add(request1_); | ||
| active_requests_->add(request2_); | ||
| active_requests_->add(request3_); | ||
| // Exclude active requests. | ||
| active_requests_->remove(request2_); | ||
|
|
||
| // Must cancel active requests on destruction. | ||
| EXPECT_CALL(request1_, cancel()); | ||
| EXPECT_CALL(request3_, cancel()); | ||
|
|
||
| // Trigger destruction. | ||
| active_requests_.reset(); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace Http | ||
| } // namespace Envoy |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't make sense to pass a non-const object above and a const object here. I would just pass a non-const in both places and remove the const_cast. Thank you!
/wait
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AsyncClientRequestTracker::remove()is intended for use in the context ofAsyncClient::Callbacks::onSuccess(request, response)andAsyncClient::Callbacks::onFailure(request, reason).Notice that
onSuccess / onFailuremethods will now haverequestas an extra parameter to enable correlation between requests and responses.The proposed interface for
AsyncClient::Callbackslooks like this:requestwas madeconst Request&to prevent any unintended usage, e.g. callingrequest.cancel().So,
const Request&inCallbackscauses the same inAsyncClientRequestTracker::remove()(otherwise,const_castwill be a part of every call toAsyncClientRequestTracker::remove()).Do you want me to remove
const AsyncClient::Request&from bothAsyncClientRequestTrackerandCallbacks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm OK. Can you add a comment above the const_cast as to why it's required? Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added comments