diff options
author | Yuchen Zeng <zyc@google.com> | 2016-05-03 12:18:13 -0700 |
---|---|---|
committer | Yuchen Zeng <zyc@google.com> | 2016-05-03 12:18:13 -0700 |
commit | 3b8f3354de5af07ea595713623bcc19cd19d6dfe (patch) | |
tree | 8cda8524a4dae95846d1a6cf08689a216470b7e2 /src/cpp/server | |
parent | c049035bedad8e013a439bf2934d01e616da4227 (diff) |
Add plugins at the time of static initialization
Diffstat (limited to 'src/cpp/server')
-rw-r--r-- | src/cpp/server/server_builder.cc | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 5dc73ed1e4..b6e48efa8d 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -41,9 +41,21 @@ 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() : 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<ServerBuilderPlugin> plugin = factory(); + plugins_[plugin->name()] = std::move(plugin); + } } std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { @@ -156,4 +168,10 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() { 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 |