Skip to content

Commit b33ceb2

Browse files
qiwzhangistio-testing
authored andcommitted
To share mixer client across listeners (#1972)
Signed-off-by: Wayne Zhang <[email protected]>
1 parent f2a3157 commit b33ceb2

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

src/envoy/http/mixer/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Config {
4545
// The Http client config.
4646
::istio::mixer::v1::config::client::HttpClientConfig config_pb_;
4747
};
48+
typedef std::unique_ptr<Config> ConfigPtr;
4849

4950
} // namespace Mixer
5051
} // namespace Http

src/envoy/http/mixer/control_factory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class ControlFactory : public Logger::Loggable<Logger::Id::config> {
6363
// This stats object.
6464
Utils::MixerFilterStats stats_;
6565
};
66+
typedef std::shared_ptr<ControlFactory> ControlFactorySharedPtr;
6667

6768
} // namespace Mixer
6869
} // namespace Http

src/envoy/http/mixer/filter_factory.cc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,8 @@ class MixerConfigFactory : public NamedHttpFilterConfigFactory {
7575
Http::FilterFactoryCb createFilterFactory(const HttpClientConfig& config_pb,
7676
const std::string&,
7777
FactoryContext& context) {
78-
std::unique_ptr<Http::Mixer::Config> config_obj(
79-
new Http::Mixer::Config(config_pb));
80-
auto control_factory = std::make_shared<Http::Mixer::ControlFactory>(
81-
std::move(config_obj), context);
78+
auto config_obj = std::make_unique<Http::Mixer::Config>(config_pb);
79+
auto control_factory = getControlFactory(std::move(config_obj), context);
8280
return [control_factory](
8381
Http::FilterChainFactoryCallbacks& callbacks) -> void {
8482
std::shared_ptr<Http::Mixer::Filter> instance =
@@ -87,6 +85,23 @@ class MixerConfigFactory : public NamedHttpFilterConfigFactory {
8785
callbacks.addAccessLogHandler(AccessLog::InstanceSharedPtr(instance));
8886
};
8987
}
88+
89+
Http::Mixer::ControlFactorySharedPtr getControlFactory(
90+
Http::Mixer::ConfigPtr config_obj, FactoryContext& context) {
91+
const std::string hash = config_obj->config_pb().SerializeAsString();
92+
Http::Mixer::ControlFactorySharedPtr control_factory =
93+
control_factory_maps_[hash].lock();
94+
if (!control_factory) {
95+
control_factory = std::make_shared<Http::Mixer::ControlFactory>(
96+
std::move(config_obj), context);
97+
control_factory_maps_[hash] = control_factory;
98+
}
99+
return control_factory;
100+
}
101+
102+
// A weak pointer map to share control factory across different listeners.
103+
std::unordered_map<std::string, std::weak_ptr<Http::Mixer::ControlFactory>>
104+
control_factory_maps_;
90105
};
91106

92107
static Registry::RegisterFactory<MixerConfigFactory,

src/envoy/tcp/mixer/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Config {
7878
// Time interval in milliseconds for sending periodical delta reports.
7979
std::chrono::milliseconds report_interval_ms_;
8080
};
81+
typedef std::unique_ptr<Config> ConfigPtr;
8182

8283
} // namespace Mixer
8384
} // namespace Tcp

src/envoy/tcp/mixer/control_factory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class ControlFactory : public Logger::Loggable<Logger::Id::filter> {
7070
// UUID of the Envoy TCP mixer filter.
7171
const std::string uuid_;
7272
};
73+
typedef std::shared_ptr<ControlFactory> ControlFactorySharedPtr;
7374

7475
} // namespace Mixer
7576
} // namespace Tcp

src/envoy/tcp/mixer/filter_factory.cc

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,32 @@ class FilterFactory : public NamedNetworkFilterConfigFactory {
5252
private:
5353
Network::FilterFactoryCb createFilterFactory(const TcpClientConfig& config_pb,
5454
FactoryContext& context) {
55-
std::unique_ptr<Tcp::Mixer::Config> config_obj(
56-
new Tcp::Mixer::Config(config_pb));
57-
58-
auto control_factory = std::make_shared<Tcp::Mixer::ControlFactory>(
59-
std::move(config_obj), context);
55+
auto config_obj = std::make_unique<Tcp::Mixer::Config>(config_pb);
56+
auto control_factory = getControlFactory(std::move(config_obj), context);
6057
return [control_factory](Network::FilterManager& filter_manager) -> void {
6158
std::shared_ptr<Tcp::Mixer::Filter> instance =
6259
std::make_shared<Tcp::Mixer::Filter>(control_factory->control());
6360
filter_manager.addReadFilter(Network::ReadFilterSharedPtr(instance));
6461
filter_manager.addWriteFilter(Network::WriteFilterSharedPtr(instance));
6562
};
6663
}
64+
65+
Tcp::Mixer::ControlFactorySharedPtr getControlFactory(
66+
Tcp::Mixer::ConfigPtr config_obj, FactoryContext& context) {
67+
const std::string hash = config_obj->config_pb().SerializeAsString();
68+
Tcp::Mixer::ControlFactorySharedPtr control_factory =
69+
control_factory_maps_[hash].lock();
70+
if (!control_factory) {
71+
control_factory = std::make_shared<Tcp::Mixer::ControlFactory>(
72+
std::move(config_obj), context);
73+
control_factory_maps_[hash] = control_factory;
74+
}
75+
return control_factory;
76+
}
77+
78+
// A weak pointer map to share control factory across different listeners.
79+
std::unordered_map<std::string, std::weak_ptr<Tcp::Mixer::ControlFactory>>
80+
control_factory_maps_;
6781
};
6882

6983
static Registry::RegisterFactory<FilterFactory, NamedNetworkFilterConfigFactory>

0 commit comments

Comments
 (0)