Skip to content

Commit

Permalink
Disable rate limit via runtime key (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccaraman authored Nov 2, 2016
1 parent 6bb5998 commit 22bc6c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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.
8 changes: 8 additions & 0 deletions source/common/http/filter/ratelimit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ FilterHeadersStatus Filter::decodeHeaders(HeaderMap& headers, bool) {

const Router::RouteEntry* route = callbacks_->routeTable().routeForRequest(headers);
if (route && route->rateLimitPolicy().doGlobalLimiting()) {
// 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(
fmt::format("ratelimit.{}.http_filter_enabled", route_key), 100)) {
return FilterHeadersStatus::Continue;
}

std::vector<::RateLimit::Descriptor> descriptors;
for (const ActionPtr& action : config_->actions()) {
action->populateDescriptors(*route, descriptors, *config_, headers, *callbacks_);
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

0 comments on commit 22bc6c1

Please sign in to comment.