Add plugins at the time of static initialization

pull/6373/head
Yuchen Zeng 9 years ago
parent c049035bed
commit 3b8f3354de
  1. 10
      include/grpc++/impl/server_builder_plugin.h
  2. 3
      include/grpc++/server_builder.h
  3. 18
      src/cpp/server/server_builder.cc
  4. 28
      test/cpp/end2end/server_builder_plugin_test.cc

@ -64,16 +64,6 @@ class ServerBuilderPlugin {
} // namespace grpc } // namespace grpc
#define GRPC_DECLARE_PLUGIN(plugin_name) \
namespace sBP##plugin_name { \
extern std::unique_ptr<ServerBuilderPlugin> Create##plugin_name(); \
}
#define GRPC_INIT_PLUGIN(map, plugin_name) \
{ \
std::unique_ptr<ServerBuilderPlugin> plugin = \
sBP##plugin_name::Create##plugin_name(); \
map[plugin->name()] = std::move(plugin); \
}
#endif // GRPCXX_IMPL_SERVER_BUILDER_PLUGIN_H #endif // GRPCXX_IMPL_SERVER_BUILDER_PLUGIN_H

@ -113,6 +113,9 @@ class ServerBuilder {
/// Return a running server which is ready for processing calls. /// Return a running server which is ready for processing calls.
std::unique_ptr<Server> BuildAndStart(); std::unique_ptr<Server> BuildAndStart();
static void InternalAddPluginFactory(
std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
private: private:
friend class ::grpc::testing::ServerBuilderPluginTest; friend class ::grpc::testing::ServerBuilderPluginTest;

@ -41,9 +41,21 @@
namespace grpc { namespace grpc {
static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>* plugin_list;
static gpr_once once_init_plugin_list = GPR_ONCE_INIT;
static void do_plugin_list_init(void) {
plugin_list = new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>();
}
ServerBuilder::ServerBuilder() ServerBuilder::ServerBuilder()
: max_message_size_(-1), generic_service_(nullptr) { : max_message_size_(-1), generic_service_(nullptr) {
grpc_compression_options_init(&compression_options_); 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<ServerBuilderPlugin> plugin = factory();
plugins_[plugin->name()] = std::move(plugin);
}
} }
std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() {
@ -156,4 +168,10 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
return server; return server;
} }
void ServerBuilder::InternalAddPluginFactory(
std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
(*plugin_list).push_back(CreatePlugin);
}
} // namespace grpc } // namespace grpc

@ -115,6 +115,11 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption {
void UpdatePlugins( void UpdatePlugins(
std::map<grpc::string, std::unique_ptr<ServerBuilderPlugin>>* plugins) std::map<grpc::string, std::unique_ptr<ServerBuilderPlugin>>* plugins)
GRPC_OVERRIDE { GRPC_OVERRIDE {
auto it = plugins->begin();
while (it != plugins->end()) {
plugins->erase(it++);
}
std::unique_ptr<TestServerBuilderPlugin> plugin( std::unique_ptr<TestServerBuilderPlugin> plugin(
new TestServerBuilderPlugin()); new TestServerBuilderPlugin());
if (register_service_) plugin->SetRegisterService(); if (register_service_) plugin->SetRegisterService();
@ -127,13 +132,24 @@ class InsertPluginServerBuilderOption : public ServerBuilderOption {
bool register_service_; bool register_service_;
}; };
namespace sBPTestServerBuilderPlugin {
std::unique_ptr<ServerBuilderPlugin> CreateTestServerBuilderPlugin() { std::unique_ptr<ServerBuilderPlugin> CreateTestServerBuilderPlugin() {
return std::unique_ptr<ServerBuilderPlugin>(new TestServerBuilderPlugin()); return std::unique_ptr<ServerBuilderPlugin>(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<bool> { class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> {
public: public:
@ -146,8 +162,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> {
void InsertPlugin() { void InsertPlugin() {
if (GetParam()) { if (GetParam()) {
// Add ServerBuilder plugin directly // Add ServerBuilder plugin in static initialization
GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin);
EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr);
} else { } else {
// Add ServerBuilder plugin using ServerBuilder::SetOption() // Add ServerBuilder plugin using ServerBuilder::SetOption()
@ -158,8 +173,7 @@ class ServerBuilderPluginTest : public ::testing::TestWithParam<bool> {
void InsertPluginWithTestService() { void InsertPluginWithTestService() {
if (GetParam()) { if (GetParam()) {
// Add ServerBuilder plugin directly // Add ServerBuilder plugin in static initialization
GRPC_INIT_PLUGIN(builder_->plugins_, TestServerBuilderPlugin);
EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr); EXPECT_TRUE(builder_->plugins_[PLUGIN_NAME] != nullptr);
auto plugin = static_cast<TestServerBuilderPlugin*>( auto plugin = static_cast<TestServerBuilderPlugin*>(
builder_->plugins_[PLUGIN_NAME].get()); builder_->plugins_[PLUGIN_NAME].get());

Loading…
Cancel
Save