Skip to content
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

Disable rate limit via runtime key #182

Merged
merged 5 commits into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/configuration/http_filters/rate_limit_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ ratelimit.http_filter_enabled
ratelimit.http_filter_enforcing
% of requests that will call the rate limit service and enforce the decision. Defaults to 100.
This can be used to test what would happen before fully enforcing the outcome.

ratelimit.<route_key>.http_filter_enabled
% of requests that will call the rate limit service for a given *route_key* specified in the
:ref:`route <config_http_conn_man_route_table_route_rate_limit>`. Defaults to 100.
35 changes: 22 additions & 13 deletions source/common/http/filter/ratelimit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,32 @@ FilterHeadersStatus Filter::decodeHeaders(HeaderMap& headers, bool) {
}

const Router::RouteEntry* route = callbacks_->routeTable().routeForRequest(headers);
if (route && route->rateLimitPolicy().doGlobalLimiting()) {
std::vector<::RateLimit::Descriptor> descriptors;
for (const ActionPtr& action : config_->actions()) {
action->populateDescriptors(*route, descriptors, *config_, headers, *callbacks_);
if (route) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do this in the doGlobalLimiting() block below. We don't need to do the runtime check if there is no way we are going to call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved.

// Check if the route_key is enabled for rate limiting.
const std::string& route_key = route->rateLimitPolicy().routeKey();
if (!route_key.empty() &&
!config_->runtime().snapshot().featureEnabled(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for missing docs :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

fmt::format("ratelimit.{}.http_filter_enabled", route_key), 100)) {
return FilterHeadersStatus::Continue;
}

if (!descriptors.empty()) {
cluster_stat_prefix_ = fmt::format("cluster.{}.", route->clusterName());
cluster_ratelimit_stat_prefix_ = fmt::format("{}ratelimit.", cluster_stat_prefix_);

state_ = State::Calling;
initiating_call_ = true;
client_->limit(*this, config_->domain(), descriptors);
initiating_call_ = false;
if (route->rateLimitPolicy().doGlobalLimiting()) {
std::vector<::RateLimit::Descriptor> descriptors;
for (const ActionPtr& action : config_->actions()) {
action->populateDescriptors(*route, descriptors, *config_, headers, *callbacks_);
}

if (!descriptors.empty()) {
cluster_stat_prefix_ = fmt::format("cluster.{}.", route->clusterName());
cluster_ratelimit_stat_prefix_ = fmt::format("{}ratelimit.", cluster_stat_prefix_);

state_ = State::Calling;
initiating_call_ = true;
client_->limit(*this, config_->domain(), descriptors);
initiating_call_ = false;
}
}
}

return (state_ == State::Calling || state_ == State::Responded)
? FilterHeadersStatus::StopIteration
: FilterHeadersStatus::Continue;
Expand Down
17 changes: 17 additions & 0 deletions test/common/http/filter/ratelimit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class HttpRateLimitFilterTest : public testing::Test {
.WillByDefault(Return(true));
ON_CALL(runtime_.snapshot_, featureEnabled("ratelimit.http_filter_enforcing", 100))
.WillByDefault(Return(true));
ON_CALL(runtime_.snapshot_, featureEnabled("ratelimit.test_key.http_filter_enabled", 100))
.WillByDefault(Return(true));
}

void SetUpTest(const std::string json) {
Expand Down Expand Up @@ -388,5 +390,20 @@ TEST_F(HttpRateLimitFilterTest, NoAddressRateLimiting) {
EXPECT_EQ(FilterTrailersStatus::Continue, filter_->decodeTrailers(request_headers_));
}

TEST_F(HttpRateLimitFilterTest, RateLimitDisabledForRouteKey) {
SetUpTest(request_headers_json);

filter_callbacks_.route_table_.route_entry_.rate_limit_policy_.do_global_limiting_ = true;
filter_callbacks_.route_table_.route_entry_.rate_limit_policy_.route_key_ = "test_key";
ON_CALL(runtime_.snapshot_, featureEnabled("ratelimit.test_key.http_filter_enabled", 100))
.WillByDefault(Return(false));

EXPECT_CALL(*client_, limit(_, _, _)).Times(0);

EXPECT_EQ(FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers_, false));
EXPECT_EQ(FilterDataStatus::Continue, filter_->decodeData(data_, false));
EXPECT_EQ(FilterTrailersStatus::Continue, filter_->decodeTrailers(request_headers_));
}

} // RateLimit
} // Http