diff --git a/include/grpc++/impl/server_builder_plugin.h b/include/grpc++/impl/server_builder_plugin.h index 7cf369e3467..f792c4b321c 100644 --- a/include/grpc++/impl/server_builder_plugin.h +++ b/include/grpc++/impl/server_builder_plugin.h @@ -64,16 +64,6 @@ class ServerBuilderPlugin { } // namespace grpc -#define GRPC_DECLARE_PLUGIN(plugin_name) \ - namespace sBP##plugin_name { \ - extern std::unique_ptr Create##plugin_name(); \ - } -#define GRPC_INIT_PLUGIN(map, plugin_name) \ - { \ - std::unique_ptr plugin = \ - sBP##plugin_name::Create##plugin_name(); \ - map[plugin->name()] = std::move(plugin); \ - } #endif // GRPCXX_IMPL_SERVER_BUILDER_PLUGIN_H diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h index a47b5c71cfb..52064b14349 100644 --- a/include/grpc++/server_builder.h +++ b/include/grpc++/server_builder.h @@ -113,6 +113,9 @@ class ServerBuilder { /// Return a running server which is ready for processing calls. std::unique_ptr BuildAndStart(); + static void InternalAddPluginFactory( + std::unique_ptr (*CreatePlugin)()); + private: friend class ::grpc::testing::ServerBuilderPluginTest; diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 5dc73ed1e48..b6e48efa8dd 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -41,9 +41,21 @@ namespace grpc { +static std::vector (*)()>* plugin_list; +static gpr_once once_init_plugin_list = GPR_ONCE_INIT; + +static void do_plugin_list_init(void) { + plugin_list = new std::vector (*)()>(); +} + ServerBuilder::ServerBuilder() : max_message_size_(-1), generic_service_(nullptr) { grpc_compression_options_init(&compression_options_); + gpr_once_init(&once_init_plugin_list, do_plugin_list_init); + for (auto factory : (*plugin_list)) { + std::unique_ptr plugin = factory(); + plugins_[plugin->name()] = std::move(plugin); + } } std::unique_ptr ServerBuilder::AddCompletionQueue() { @@ -156,4 +168,10 @@ std::unique_ptr ServerBuilder::BuildAndStart() { return server; } +void ServerBuilder::InternalAddPluginFactory( + std::unique_ptr (*CreatePlugin)()) { + gpr_once_init(&once_init_plugin_list, do_plugin_list_init); + (*plugin_list).push_back(CreatePlugin); +} + } // namespace grpc diff --git a/test/cpp/end2end/server_builder_plugin_test.cc b/test/cpp/end2end/server_builder_plugin_test.cc index 9ed176d29dd..0d44999a08e 100644 --- a/test/cpp/end2end/server_builder_plugin_test.cc +++ b/test/cpp/end2end/server_builder_plugin_test.cc @@ -115,6 +115,11 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption { void UpdatePlugins( std::map>* plugins) GRPC_OVERRIDE { + auto it = plugins->begin(); + while (it != plugins->end()) { + plugins->erase(it++); + } + std::unique_ptr plugin( new TestServerBuilderPlugin()); if (register_service_) plugin->SetRegisterService(); @@ -127,13 +132,24 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption { bool register_service_; }; -namespace sBPTestServerBuilderPlugin { - std::unique_ptr CreateTestServerBuilderPlugin() { return std::unique_ptr(new TestServerBuilderPlugin()); } -} // namespace sBPTestServerBuilderPlugin +void grpc_AddServerBuilderPlugin_reflection() { + static bool already_here = false; + if (already_here) return; + already_here = true; + ::grpc::ServerBuilder::InternalAddPluginFactory( + &CreateTestServerBuilderPlugin); +} + +// Force AddServerBuilderPlugin() to be called at static initialization time. +struct StaticPluginInitializer_reflection { + StaticPluginInitializer_reflection() { + grpc_AddServerBuilderPlugin_reflection(); + } +} static_plugin_initializer_reflection_; class ServerBuilderPluginTest : public ::testing::TestWithParam { public: @@ -146,8 +162,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam { void InsertPlugin() { if (GetParam()) { - // Add ServerBuilder plugin directly - GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + // Add ServerBuilder plugin in static initialization EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); } else { // Add ServerBuilder plugin using ServerBuilder::SetOption() @@ -158,8 +173,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam { void InsertPluginWithTestService() { if (GetParam()) { - // Add ServerBuilder plugin directly - GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin); + // Add ServerBuilder plugin in static initialization EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); auto plugin = static_cast( builder_->plugins_[PLUGIN_NAME].get());