Skip to content

Commit

Permalink
To share mixer client across listeners (envoyproxy#1972)
Browse files Browse the repository at this point in the history
Signed-off-by: Wayne Zhang <[email protected]>
  • Loading branch information
qiwzhang authored and istio-testing committed Sep 14, 2018
1 parent f2a3157 commit b33ceb2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/envoy/http/mixer/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Config {
// The Http client config.
::istio::mixer::v1::config::client::HttpClientConfig config_pb_;
};
typedef std::unique_ptr<Config> ConfigPtr;

} // namespace Mixer
} // namespace Http
Expand Down
1 change: 1 addition & 0 deletions src/envoy/http/mixer/control_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ControlFactory : public Logger::Loggable<Logger::Id::config> {
// This stats object.
Utils::MixerFilterStats stats_;
};
typedef std::shared_ptr<ControlFactory> ControlFactorySharedPtr;

} // namespace Mixer
} // namespace Http
Expand Down
23 changes: 19 additions & 4 deletions src/envoy/http/mixer/filter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,8 @@ class MixerConfigFactory : public NamedHttpFilterConfigFactory {
Http::FilterFactoryCb createFilterFactory(const HttpClientConfig& config_pb,
const std::string&,
FactoryContext& context) {
std::unique_ptr<Http::Mixer::Config> config_obj(
new Http::Mixer::Config(config_pb));
auto control_factory = std::make_shared<Http::Mixer::ControlFactory>(
std::move(config_obj), context);
auto config_obj = std::make_unique<Http::Mixer::Config>(config_pb);
auto control_factory = getControlFactory(std::move(config_obj), context);
return [control_factory](
Http::FilterChainFactoryCallbacks& callbacks) -> void {
std::shared_ptr<Http::Mixer::Filter> instance =
Expand All @@ -87,6 +85,23 @@ class MixerConfigFactory : public NamedHttpFilterConfigFactory {
callbacks.addAccessLogHandler(AccessLog::InstanceSharedPtr(instance));
};
}

Http::Mixer::ControlFactorySharedPtr getControlFactory(
Http::Mixer::ConfigPtr config_obj, FactoryContext& context) {
const std::string hash = config_obj->config_pb().SerializeAsString();
Http::Mixer::ControlFactorySharedPtr control_factory =
control_factory_maps_[hash].lock();
if (!control_factory) {
control_factory = std::make_shared<Http::Mixer::ControlFactory>(
std::move(config_obj), context);
control_factory_maps_[hash] = control_factory;
}
return control_factory;
}

// A weak pointer map to share control factory across different listeners.
std::unordered_map<std::string, std::weak_ptr<Http::Mixer::ControlFactory>>
control_factory_maps_;
};

static Registry::RegisterFactory<MixerConfigFactory,
Expand Down
1 change: 1 addition & 0 deletions src/envoy/tcp/mixer/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Config {
// Time interval in milliseconds for sending periodical delta reports.
std::chrono::milliseconds report_interval_ms_;
};
typedef std::unique_ptr<Config> ConfigPtr;

} // namespace Mixer
} // namespace Tcp
Expand Down
1 change: 1 addition & 0 deletions src/envoy/tcp/mixer/control_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ControlFactory : public Logger::Loggable<Logger::Id::filter> {
// UUID of the Envoy TCP mixer filter.
const std::string uuid_;
};
typedef std::shared_ptr<ControlFactory> ControlFactorySharedPtr;

} // namespace Mixer
} // namespace Tcp
Expand Down
24 changes: 19 additions & 5 deletions src/envoy/tcp/mixer/filter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,32 @@ class FilterFactory : public NamedNetworkFilterConfigFactory {
private:
Network::FilterFactoryCb createFilterFactory(const TcpClientConfig& config_pb,
FactoryContext& context) {
std::unique_ptr<Tcp::Mixer::Config> config_obj(
new Tcp::Mixer::Config(config_pb));

auto control_factory = std::make_shared<Tcp::Mixer::ControlFactory>(
std::move(config_obj), context);
auto config_obj = std::make_unique<Tcp::Mixer::Config>(config_pb);
auto control_factory = getControlFactory(std::move(config_obj), context);
return [control_factory](Network::FilterManager& filter_manager) -> void {
std::shared_ptr<Tcp::Mixer::Filter> instance =
std::make_shared<Tcp::Mixer::Filter>(control_factory->control());
filter_manager.addReadFilter(Network::ReadFilterSharedPtr(instance));
filter_manager.addWriteFilter(Network::WriteFilterSharedPtr(instance));
};
}

Tcp::Mixer::ControlFactorySharedPtr getControlFactory(
Tcp::Mixer::ConfigPtr config_obj, FactoryContext& context) {
const std::string hash = config_obj->config_pb().SerializeAsString();
Tcp::Mixer::ControlFactorySharedPtr control_factory =
control_factory_maps_[hash].lock();
if (!control_factory) {
control_factory = std::make_shared<Tcp::Mixer::ControlFactory>(
std::move(config_obj), context);
control_factory_maps_[hash] = control_factory;
}
return control_factory;
}

// A weak pointer map to share control factory across different listeners.
std::unordered_map<std::string, std::weak_ptr<Tcp::Mixer::ControlFactory>>
control_factory_maps_;
};

static Registry::RegisterFactory<FilterFactory, NamedNetworkFilterConfigFactory>
Expand Down

0 comments on commit b33ceb2

Please sign in to comment.