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

bandwidth_limit: make exception free #37666

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const Http::LowerCaseString DefaultResponseFilterDelayTrailer =
} // namespace

FilterConfig::FilterConfig(const BandwidthLimit& config, Stats::Scope& scope,
Runtime::Loader& runtime, TimeSource& time_source, bool per_route)
Runtime::Loader& runtime, TimeSource& time_source,
absl::Status& creation_status, bool per_route)
: runtime_(runtime), time_source_(time_source), enable_mode_(config.enable_mode()),
limit_kbps_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(config, limit_kbps, 0)),
fill_interval_(std::chrono::milliseconds(PROTOBUF_GET_MS_OR_DEFAULT(
Expand Down Expand Up @@ -57,7 +58,8 @@ FilterConfig::FilterConfig(const BandwidthLimit& config, Stats::Scope& scope,
DefaultResponseFilterDelayTrailer.get()))),
enable_response_trailers_(config.enable_response_trailers()) {
if (per_route && !config.has_limit_kbps()) {
throw EnvoyException("bandwidthlimitfilter: limit must be set for per route filter config");
creation_status = absl::InvalidArgumentError("limit must be set for per route filter config");
return;
}

// The token bucket is configured with a max token count of the number of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class FilterConfig : public ::Envoy::Router::RouteSpecificFilterConfig {

FilterConfig(const envoy::extensions::filters::http::bandwidth_limit::v3::BandwidthLimit& config,
Stats::Scope& scope, Runtime::Loader& runtime, TimeSource& time_source,
bool per_route = false);
absl::Status& creation_status, bool per_route = false);
~FilterConfig() override = default;
Runtime::Loader& runtime() { return runtime_; }
BandwidthLimitStats& stats() const { return stats_; }
Expand Down
13 changes: 9 additions & 4 deletions source/extensions/filters/http/bandwidth_limit/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ namespace Extensions {
namespace HttpFilters {
namespace BandwidthLimitFilter {

Http::FilterFactoryCb BandwidthLimitFilterConfig::createFilterFactoryFromProtoTyped(
absl::StatusOr<Http::FilterFactoryCb> BandwidthLimitFilterConfig::createFilterFactoryFromProtoTyped(
const envoy::extensions::filters::http::bandwidth_limit::v3::BandwidthLimit& proto_config,
const std::string&, Server::Configuration::FactoryContext& context) {
auto& server_context = context.serverFactoryContext();

absl::Status status;
FilterConfigSharedPtr filter_config = std::make_shared<FilterConfig>(
proto_config, context.scope(), server_context.runtime(), server_context.timeSource());
proto_config, context.scope(), server_context.runtime(), server_context.timeSource(), status);
RETURN_IF_NOT_OK_REF(status);
return [filter_config](Http::FilterChainFactoryCallbacks& callbacks) -> void {
callbacks.addStreamFilter(std::make_shared<BandwidthLimiter>(filter_config));
};
Expand All @@ -28,8 +30,11 @@ absl::StatusOr<Router::RouteSpecificFilterConfigConstSharedPtr>
BandwidthLimitFilterConfig::createRouteSpecificFilterConfigTyped(
const envoy::extensions::filters::http::bandwidth_limit::v3::BandwidthLimit& proto_config,
Server::Configuration::ServerFactoryContext& context, ProtobufMessage::ValidationVisitor&) {
return std::make_shared<const FilterConfig>(proto_config, context.scope(), context.runtime(),
context.timeSource(), true);
absl::Status status;
auto config = std::make_shared<const FilterConfig>(
proto_config, context.scope(), context.runtime(), context.timeSource(), status, true);
RETURN_IF_NOT_OK_REF(status);
return config;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions source/extensions/filters/http/bandwidth_limit/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ namespace BandwidthLimitFilter {
* Config registration for the bandwidth limit filter. @see NamedHttpFilterConfigFactory.
*/
class BandwidthLimitFilterConfig
: public Common::FactoryBase<
: public Common::ExceptionFreeFactoryBase<
envoy::extensions::filters::http::bandwidth_limit::v3::BandwidthLimit> {
public:
BandwidthLimitFilterConfig() : FactoryBase("envoy.filters.http.bandwidth_limit") {}
BandwidthLimitFilterConfig() : ExceptionFreeFactoryBase("envoy.filters.http.bandwidth_limit") {}

private:
Http::FilterFactoryCb createFilterFactoryFromProtoTyped(
absl::StatusOr<Http::FilterFactoryCb> createFilterFactoryFromProtoTyped(
const envoy::extensions::filters::http::bandwidth_limit::v3::BandwidthLimit& proto_config,
const std::string& stats_prefix, Server::Configuration::FactoryContext& context) override;

Expand Down
8 changes: 3 additions & 5 deletions test/extensions/filters/http/bandwidth_limit/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,9 @@ TEST(Factory, PerRouteConfigNoLimits) {
TestUtility::loadFromYaml(config_yaml, *proto_config);

NiceMock<Server::Configuration::MockServerFactoryContext> context;
EXPECT_THROW(factory
.createRouteSpecificFilterConfig(*proto_config, context,
ProtobufMessage::getNullValidationVisitor())
.value(),
EnvoyException);
const auto result = factory.createRouteSpecificFilterConfig(
*proto_config, context, ProtobufMessage::getNullValidationVisitor());
EXPECT_EQ(result.status().message(), "limit must be set for per route filter config");
}
} // namespace BandwidthLimitFilter
} // namespace HttpFilters
Expand Down
6 changes: 4 additions & 2 deletions test/extensions/filters/http/bandwidth_limit/filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class FilterTest : public testing::Test {
void setup(const std::string& yaml) {
envoy::extensions::filters::http::bandwidth_limit::v3::BandwidthLimit config;
TestUtility::loadFromYaml(yaml, config);
config_ =
std::make_shared<FilterConfig>(config, *stats_.rootScope(), runtime_, time_system_, true);
absl::Status status;
config_ = std::make_shared<FilterConfig>(config, *stats_.rootScope(), runtime_, time_system_,
status, true);
EXPECT_TRUE(status.ok());
filter_ = std::make_shared<BandwidthLimiter>(config_);
filter_->setDecoderFilterCallbacks(decoder_filter_callbacks_);
filter_->setEncoderFilterCallbacks(encoder_filter_callbacks_);
Expand Down
1 change: 0 additions & 1 deletion tools/code_format/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ paths:
- source/extensions/filters/http/admission_control
- source/extensions/filters/http/api_key_auth
- source/extensions/filters/http/aws_lambda
- source/extensions/filters/http/bandwidth_limit
- source/extensions/filters/http/basic_auth
- source/extensions/filters/http/cache
- source/extensions/filters/http/cdn_loop
Expand Down