-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Fix matcher-tree creation happening per filter instance #37797
Open
bsurber
wants to merge
1
commit into
envoyproxy:main
Choose a base branch
from
bsurber:fix-matcher-build-rlqs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+54
−49
Open
Changes from all commits
Commits
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 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 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 |
---|---|---|
|
@@ -155,54 +155,39 @@ Http::FilterHeadersStatus RateLimitQuotaFilter::decodeHeaders(Http::RequestHeade | |
StreamInfo::CoreResponseFlag::ResponseFromCacheFilter); | ||
} | ||
|
||
void RateLimitQuotaFilter::createMatcher() { | ||
RateLimitOnMatchActionContext context; | ||
Matcher::MatchTreeFactory<Http::HttpMatchingData, RateLimitOnMatchActionContext> factory( | ||
context, factory_context_.serverFactoryContext(), visitor_); | ||
if (config_->has_bucket_matchers()) { | ||
matcher_ = factory.create(config_->bucket_matchers())(); | ||
} | ||
} | ||
|
||
// TODO(tyxia) Currently request matching is only performed on the request | ||
// header. | ||
absl::StatusOr<Matcher::ActionPtr> | ||
RateLimitQuotaFilter::requestMatching(const Http::RequestHeaderMap& headers) { | ||
// Initialize the data pointer on first use and reuse it for subsequent | ||
// requests. This avoids creating the data object for every request, which | ||
// is expensive. | ||
if (data_ptr_ == nullptr) { | ||
if (callbacks_ != nullptr) { | ||
data_ptr_ = std::make_unique<Http::Matching::HttpMatchingDataImpl>(callbacks_->streamInfo()); | ||
} else { | ||
if (!data_ptr_) { | ||
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. i actually prefer |
||
if (!callbacks_) { | ||
return absl::InternalError("Filter callback has not been initialized successfully yet."); | ||
} | ||
data_ptr_ = std::make_unique<Http::Matching::HttpMatchingDataImpl>(callbacks_->streamInfo()); | ||
} | ||
|
||
if (matcher_ == nullptr) { | ||
if (!matcher_) { | ||
return absl::InternalError("Matcher tree has not been initialized yet."); | ||
} else { | ||
// Populate the request header. | ||
if (!headers.empty()) { | ||
data_ptr_->onRequestHeaders(headers); | ||
} | ||
|
||
// Perform the matching. | ||
auto match_result = Matcher::evaluateMatch<Http::HttpMatchingData>(*matcher_, *data_ptr_); | ||
} | ||
// Populate the request header. | ||
if (!headers.empty()) { | ||
data_ptr_->onRequestHeaders(headers); | ||
} | ||
|
||
if (match_result.match_state_ == Matcher::MatchState::MatchComplete) { | ||
if (match_result.result_) { | ||
// Return the matched result for `on_match` case. | ||
return match_result.result_(); | ||
} else { | ||
return absl::NotFoundError("Matching completed but no match result was found."); | ||
} | ||
} else { | ||
// The returned state from `evaluateMatch` function is | ||
// `MatchState::UnableToMatch` here. | ||
return absl::InternalError("Unable to match due to the required data not being available."); | ||
} | ||
// Perform the matching. | ||
auto match_result = Matcher::evaluateMatch<Http::HttpMatchingData>(*matcher_, *data_ptr_); | ||
if (match_result.match_state_ != Matcher::MatchState::MatchComplete) { | ||
// The returned state from `evaluateMatch` function is `MatchState::UnableToMatch` here. | ||
return absl::InternalError("Unable to match due to the required data not being available."); | ||
} | ||
if (!match_result.result_) { | ||
return absl::NotFoundError("Matching completed but no match result was found."); | ||
} | ||
// Return the matched result for `on_match` case. | ||
return match_result.result_(); | ||
} | ||
|
||
void RateLimitQuotaFilter::onDestroy() { | ||
|
This file contains 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 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
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.
for the
matcher
shared pointer here, you only need to make copy (i.e., increase ref count once) once.For example, in the RateLimitQuotaFilter constructor, it is passed by value which will achieve the need of copy above. Other places can just be move