server_builder.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. *
  3. * Copyright 2015-2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc++/server_builder.h>
  34. #include <grpc++/impl/service_type.h>
  35. #include <grpc++/server.h>
  36. #include <grpc/support/cpu.h>
  37. #include <grpc/support/log.h>
  38. #include "include/grpc/support/useful.h"
  39. #include "src/cpp/server/thread_pool_interface.h"
  40. namespace grpc {
  41. static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>*
  42. g_plugin_factory_list;
  43. static gpr_once once_init_plugin_list = GPR_ONCE_INIT;
  44. static void do_plugin_list_init(void) {
  45. g_plugin_factory_list =
  46. new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>();
  47. }
  48. ServerBuilder::ServerBuilder()
  49. : max_message_size_(-1), generic_service_(nullptr) {
  50. gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
  51. for (auto it = g_plugin_factory_list->begin();
  52. it != g_plugin_factory_list->end(); it++) {
  53. auto& factory = *it;
  54. plugins_.emplace_back(factory());
  55. }
  56. // all compression algorithms enabled by default.
  57. enabled_compression_algorithms_bitset_ =
  58. (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
  59. memset(&maybe_default_compression_level_, 0,
  60. sizeof(maybe_default_compression_level_));
  61. memset(&maybe_default_compression_algorithm_, 0,
  62. sizeof(maybe_default_compression_algorithm_));
  63. }
  64. std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue(
  65. bool is_frequently_polled) {
  66. ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled);
  67. cqs_.push_back(cq);
  68. return std::unique_ptr<ServerCompletionQueue>(cq);
  69. }
  70. ServerBuilder& ServerBuilder::RegisterService(Service* service) {
  71. services_.emplace_back(new NamedService(service));
  72. return *this;
  73. }
  74. ServerBuilder& ServerBuilder::RegisterService(const grpc::string& addr,
  75. Service* service) {
  76. services_.emplace_back(new NamedService(addr, service));
  77. return *this;
  78. }
  79. ServerBuilder& ServerBuilder::RegisterAsyncGenericService(
  80. AsyncGenericService* service) {
  81. if (generic_service_) {
  82. gpr_log(GPR_ERROR,
  83. "Adding multiple AsyncGenericService is unsupported for now. "
  84. "Dropping the service %p",
  85. service);
  86. } else {
  87. generic_service_ = service;
  88. }
  89. return *this;
  90. }
  91. ServerBuilder& ServerBuilder::SetOption(
  92. std::unique_ptr<ServerBuilderOption> option) {
  93. options_.push_back(std::move(option));
  94. return *this;
  95. }
  96. ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus(
  97. grpc_compression_algorithm algorithm, bool enabled) {
  98. if (enabled) {
  99. GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm);
  100. } else {
  101. GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm);
  102. }
  103. return *this;
  104. }
  105. ServerBuilder& ServerBuilder::SetDefaultCompressionLevel(
  106. grpc_compression_level level) {
  107. maybe_default_compression_level_.level = level;
  108. return *this;
  109. }
  110. ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm(
  111. grpc_compression_algorithm algorithm) {
  112. maybe_default_compression_algorithm_.is_set = true;
  113. maybe_default_compression_algorithm_.algorithm = algorithm;
  114. return *this;
  115. }
  116. ServerBuilder& ServerBuilder::AddListeningPort(
  117. const grpc::string& addr, std::shared_ptr<ServerCredentials> creds,
  118. int* selected_port) {
  119. Port port = {addr, creds, selected_port};
  120. ports_.push_back(port);
  121. return *this;
  122. }
  123. std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
  124. std::unique_ptr<ThreadPoolInterface> thread_pool;
  125. bool has_sync_methods = false;
  126. for (auto it = services_.begin(); it != services_.end(); ++it) {
  127. if ((*it)->service->has_synchronous_methods()) {
  128. if (!thread_pool) {
  129. thread_pool.reset(CreateDefaultThreadPool());
  130. has_sync_methods = true;
  131. break;
  132. }
  133. }
  134. }
  135. ChannelArguments args;
  136. for (auto option = options_.begin(); option != options_.end(); ++option) {
  137. (*option)->UpdateArguments(&args);
  138. (*option)->UpdatePlugins(&plugins_);
  139. }
  140. if (!thread_pool) {
  141. for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
  142. if ((*plugin)->has_sync_methods()) {
  143. thread_pool.reset(CreateDefaultThreadPool());
  144. has_sync_methods = true;
  145. break;
  146. }
  147. }
  148. }
  149. if (max_message_size_ > 0) {
  150. args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_);
  151. }
  152. args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
  153. enabled_compression_algorithms_bitset_);
  154. if (maybe_default_compression_level_.is_set) {
  155. args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL,
  156. maybe_default_compression_level_.level);
  157. }
  158. if (maybe_default_compression_algorithm_.is_set) {
  159. args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM,
  160. maybe_default_compression_algorithm_.algorithm);
  161. }
  162. std::unique_ptr<Server> server(
  163. new Server(thread_pool.release(), true, max_message_size_, &args));
  164. ServerInitializer* initializer = server->initializer();
  165. // If the server has atleast one sync methods, we know that this is a Sync
  166. // server or a Hybrid server and the completion queue (server->cq_) would be
  167. // frequently polled.
  168. int num_frequently_polled_cqs = has_sync_methods ? 1 : 0;
  169. for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) {
  170. // A completion queue that is not polled frequently (by calling Next() or
  171. // AsyncNext()) is not safe to use for listening to incoming channels.
  172. // Register all such completion queues as non-listening completion queues
  173. // with the GRPC core library.
  174. if ((*cq)->IsFrequentlyPolled()) {
  175. grpc_server_register_completion_queue(server->server_, (*cq)->cq(),
  176. nullptr);
  177. num_frequently_polled_cqs++;
  178. } else {
  179. grpc_server_register_non_listening_completion_queue(server->server_,
  180. (*cq)->cq(), nullptr);
  181. }
  182. }
  183. if (num_frequently_polled_cqs == 0) {
  184. gpr_log(GPR_ERROR,
  185. "At least one of the completion queues must be frequently polled");
  186. return nullptr;
  187. }
  188. for (auto service = services_.begin(); service != services_.end();
  189. service++) {
  190. if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
  191. return nullptr;
  192. }
  193. }
  194. for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
  195. (*plugin)->InitServer(initializer);
  196. }
  197. if (generic_service_) {
  198. server->RegisterAsyncGenericService(generic_service_);
  199. } else {
  200. for (auto it = services_.begin(); it != services_.end(); ++it) {
  201. if ((*it)->service->has_generic_methods()) {
  202. gpr_log(GPR_ERROR,
  203. "Some methods were marked generic but there is no "
  204. "generic service registered.");
  205. return nullptr;
  206. }
  207. }
  208. }
  209. for (auto port = ports_.begin(); port != ports_.end(); port++) {
  210. int r = server->AddListeningPort(port->addr, port->creds.get());
  211. if (!r) return nullptr;
  212. if (port->selected_port != nullptr) {
  213. *port->selected_port = r;
  214. }
  215. }
  216. auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
  217. if (!server->Start(cqs_data, cqs_.size())) {
  218. return nullptr;
  219. }
  220. for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
  221. (*plugin)->Finish(initializer);
  222. }
  223. return server;
  224. }
  225. void ServerBuilder::InternalAddPluginFactory(
  226. std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
  227. gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
  228. (*g_plugin_factory_list).push_back(CreatePlugin);
  229. }
  230. } // namespace grpc