Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Sree Kuchibhotla | 0190712 | 2016-04-21 15:09:13 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
| 34 | #include <grpc++/server_builder.h> |
| 35 | |
Craig Tiller | 14a65f9 | 2015-02-09 13:13:14 -0800 | [diff] [blame] | 36 | #include <grpc++/impl/service_type.h> |
Craig Tiller | afcc875 | 2016-10-18 16:10:06 -0700 | [diff] [blame] | 37 | #include <grpc++/resource_quota.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <grpc++/server.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 39 | #include <grpc/support/cpu.h> |
| 40 | #include <grpc/support/log.h> |
David Garcia Quintas | 468e16d | 2016-08-31 13:29:40 +0200 | [diff] [blame] | 41 | #include <grpc/support/useful.h> |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 42 | |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 43 | #include "src/cpp/server/thread_pool_interface.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | |
| 45 | namespace grpc { |
| 46 | |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 47 | static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>* |
| 48 | g_plugin_factory_list; |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 49 | static gpr_once once_init_plugin_list = GPR_ONCE_INIT; |
| 50 | |
| 51 | static void do_plugin_list_init(void) { |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 52 | g_plugin_factory_list = |
| 53 | new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>(); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 56 | ServerBuilder::ServerBuilder() |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 57 | : max_receive_message_size_(-1), |
| 58 | max_send_message_size_(-1), |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 59 | sync_server_settings_(SyncServerSettings()), |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 60 | resource_quota_(nullptr), |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 61 | generic_service_(nullptr) { |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 62 | gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
Vijay Pai | eac07c3 | 2016-06-10 01:36:53 -0700 | [diff] [blame] | 63 | for (auto it = g_plugin_factory_list->begin(); |
| 64 | it != g_plugin_factory_list->end(); it++) { |
Vijay Pai | b645a2d | 2016-06-09 18:39:06 -0700 | [diff] [blame] | 65 | auto& factory = *it; |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 66 | plugins_.emplace_back(factory()); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 67 | } |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 68 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 69 | // all compression algorithms enabled by default. |
| 70 | enabled_compression_algorithms_bitset_ = |
| 71 | (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
| 72 | memset(&maybe_default_compression_level_, 0, |
| 73 | sizeof(maybe_default_compression_level_)); |
| 74 | memset(&maybe_default_compression_algorithm_, 0, |
| 75 | sizeof(maybe_default_compression_algorithm_)); |
David Garcia Quintas | beac88c | 2015-08-10 13:39:52 -0700 | [diff] [blame] | 76 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 78 | ServerBuilder::~ServerBuilder() { |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 79 | if (resource_quota_ != nullptr) { |
| 80 | grpc_resource_quota_unref(resource_quota_); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 84 | std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue( |
| 85 | bool is_frequently_polled) { |
Craig Tiller | 334c467 | 2017-04-11 16:26:07 -0700 | [diff] [blame] | 86 | ServerCompletionQueue* cq = new ServerCompletionQueue( |
| 87 | is_frequently_polled ? GRPC_CQ_DEFAULT_POLLING : GRPC_CQ_NON_LISTENING); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 88 | cqs_.push_back(cq); |
| 89 | return std::unique_ptr<ServerCompletionQueue>(cq); |
| 90 | } |
| 91 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 92 | ServerBuilder& ServerBuilder::RegisterService(Service* service) { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 93 | services_.emplace_back(new NamedService(service)); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 94 | return *this; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 95 | } |
| 96 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 97 | ServerBuilder& ServerBuilder::RegisterService(const grpc::string& addr, |
| 98 | Service* service) { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 99 | services_.emplace_back(new NamedService(addr, service)); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 100 | return *this; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 101 | } |
| 102 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 103 | ServerBuilder& ServerBuilder::RegisterAsyncGenericService( |
| 104 | AsyncGenericService* service) { |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 105 | if (generic_service_) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 106 | gpr_log(GPR_ERROR, |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 107 | "Adding multiple AsyncGenericService is unsupported for now. " |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 108 | "Dropping the service %p", |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 109 | (void*)service); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 110 | } else { |
| 111 | generic_service_ = service; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 112 | } |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 113 | return *this; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 114 | } |
| 115 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 116 | ServerBuilder& ServerBuilder::SetOption( |
| 117 | std::unique_ptr<ServerBuilderOption> option) { |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 118 | options_.push_back(std::move(option)); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 119 | return *this; |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 122 | ServerBuilder& ServerBuilder::SetSyncServerOption( |
| 123 | ServerBuilder::SyncServerOption option, int val) { |
| 124 | switch (option) { |
| 125 | case NUM_CQS: |
| 126 | sync_server_settings_.num_cqs = val; |
| 127 | break; |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 128 | case MIN_POLLERS: |
| 129 | sync_server_settings_.min_pollers = val; |
| 130 | break; |
| 131 | case MAX_POLLERS: |
| 132 | sync_server_settings_.max_pollers = val; |
| 133 | break; |
| 134 | case CQ_TIMEOUT_MSEC: |
| 135 | sync_server_settings_.cq_timeout_msec = val; |
| 136 | break; |
| 137 | } |
| 138 | return *this; |
| 139 | } |
| 140 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 141 | ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus( |
| 142 | grpc_compression_algorithm algorithm, bool enabled) { |
| 143 | if (enabled) { |
| 144 | GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm); |
| 145 | } else { |
| 146 | GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm); |
| 147 | } |
| 148 | return *this; |
| 149 | } |
| 150 | |
| 151 | ServerBuilder& ServerBuilder::SetDefaultCompressionLevel( |
| 152 | grpc_compression_level level) { |
| 153 | maybe_default_compression_level_.level = level; |
| 154 | return *this; |
| 155 | } |
| 156 | |
| 157 | ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm( |
| 158 | grpc_compression_algorithm algorithm) { |
| 159 | maybe_default_compression_algorithm_.is_set = true; |
| 160 | maybe_default_compression_algorithm_.algorithm = algorithm; |
| 161 | return *this; |
| 162 | } |
| 163 | |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 164 | ServerBuilder& ServerBuilder::SetResourceQuota( |
| 165 | const grpc::ResourceQuota& resource_quota) { |
| 166 | if (resource_quota_ != nullptr) { |
| 167 | grpc_resource_quota_unref(resource_quota_); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 168 | } |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 169 | resource_quota_ = resource_quota.c_resource_quota(); |
| 170 | grpc_resource_quota_ref(resource_quota_); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 171 | return *this; |
| 172 | } |
| 173 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 174 | ServerBuilder& ServerBuilder::AddListeningPort( |
| 175 | const grpc::string& addr, std::shared_ptr<ServerCredentials> creds, |
| 176 | int* selected_port) { |
Nicolas Noble | 3086203 | 2015-04-24 18:17:45 -0700 | [diff] [blame] | 177 | Port port = {addr, creds, selected_port}; |
| 178 | ports_.push_back(port); |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 179 | return *this; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 182 | std::unique_ptr<Server> ServerBuilder::BuildAndStart() { |
Sree Kuchibhotla | 2d08f5b | 2016-09-26 16:11:02 -0700 | [diff] [blame] | 183 | ChannelArguments args; |
| 184 | for (auto option = options_.begin(); option != options_.end(); ++option) { |
| 185 | (*option)->UpdateArguments(&args); |
| 186 | (*option)->UpdatePlugins(&plugins_); |
| 187 | } |
| 188 | |
| 189 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
| 190 | (*plugin)->UpdateChannelArguments(&args); |
| 191 | } |
| 192 | |
| 193 | if (max_receive_message_size_ >= 0) { |
| 194 | args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, max_receive_message_size_); |
| 195 | } |
| 196 | |
| 197 | if (max_send_message_size_ >= 0) { |
| 198 | args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, max_send_message_size_); |
| 199 | } |
| 200 | |
| 201 | args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, |
| 202 | enabled_compression_algorithms_bitset_); |
| 203 | if (maybe_default_compression_level_.is_set) { |
| 204 | args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL, |
| 205 | maybe_default_compression_level_.level); |
| 206 | } |
| 207 | if (maybe_default_compression_algorithm_.is_set) { |
| 208 | args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, |
| 209 | maybe_default_compression_algorithm_.algorithm); |
| 210 | } |
| 211 | |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 212 | if (resource_quota_ != nullptr) { |
Craig Tiller | 153eaa7 | 2016-10-21 13:52:36 -0700 | [diff] [blame] | 213 | args.SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA, resource_quota_, |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 214 | grpc_resource_quota_arg_vtable()); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 215 | } |
Sree Kuchibhotla | 1e088b4 | 2016-10-28 17:34:55 -0700 | [diff] [blame] | 216 | |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 217 | // == Determine if the server has any syncrhonous methods == |
Craig Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame] | 218 | bool has_sync_methods = false; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 219 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 220 | if ((*it)->service->has_synchronous_methods()) { |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 221 | has_sync_methods = true; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if (!has_sync_methods) { |
| 227 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
| 228 | if ((*plugin)->has_sync_methods()) { |
Craig Tiller | 8a7fe1a | 2016-05-20 11:17:20 -0700 | [diff] [blame] | 229 | has_sync_methods = true; |
yang-g | bef0d87 | 2016-01-13 15:27:33 -0800 | [diff] [blame] | 230 | break; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 233 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 234 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 235 | // If this is a Sync server, i.e a server expositing sync API, then the server |
| 236 | // needs to create some completion queues to listen for incoming requests. |
| 237 | // 'sync_server_cqs' are those internal completion queues. |
| 238 | // |
| 239 | // This is different from the completion queues added to the server via |
| 240 | // ServerBuilder's AddCompletionQueue() method (those completion queues |
| 241 | // are in 'cqs_' member variable of ServerBuilder object) |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 242 | std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>> |
Sree Kuchibhotla | 6135535 | 2016-10-20 11:17:22 -0700 | [diff] [blame] | 243 | sync_server_cqs(std::make_shared< |
| 244 | std::vector<std::unique_ptr<ServerCompletionQueue>>>()); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 245 | |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 246 | int num_frequently_polled_cqs = 0; |
| 247 | for (auto it = cqs_.begin(); it != cqs_.end(); ++it) { |
| 248 | if ((*it)->IsFrequentlyPolled()) { |
| 249 | num_frequently_polled_cqs++; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | const bool is_hybrid_server = |
| 254 | has_sync_methods && num_frequently_polled_cqs > 0; |
| 255 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 256 | if (has_sync_methods) { |
Sree Kuchibhotla | a7a21d2 | 2016-09-28 12:38:01 -0700 | [diff] [blame] | 257 | // This is a Sync server |
| 258 | gpr_log(GPR_INFO, |
| 259 | "Synchronous server. Num CQs: %d, Min pollers: %d, Max Pollers: " |
| 260 | "%d, CQ timeout (msec): %d", |
| 261 | sync_server_settings_.num_cqs, sync_server_settings_.min_pollers, |
| 262 | sync_server_settings_.max_pollers, |
| 263 | sync_server_settings_.cq_timeout_msec); |
| 264 | |
Craig Tiller | 334c467 | 2017-04-11 16:26:07 -0700 | [diff] [blame] | 265 | grpc_cq_polling_type polling_type = |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 266 | is_hybrid_server ? GRPC_CQ_NON_POLLING : GRPC_CQ_DEFAULT_POLLING; |
Craig Tiller | 334c467 | 2017-04-11 16:26:07 -0700 | [diff] [blame] | 267 | |
Sree Kuchibhotla | a7a21d2 | 2016-09-28 12:38:01 -0700 | [diff] [blame] | 268 | // Create completion queues to listen to incoming rpc requests |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 269 | for (int i = 0; i < sync_server_settings_.num_cqs; i++) { |
Craig Tiller | 334c467 | 2017-04-11 16:26:07 -0700 | [diff] [blame] | 270 | sync_server_cqs->emplace_back(new ServerCompletionQueue(polling_type)); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 274 | std::unique_ptr<Server> server(new Server( |
Sree Kuchibhotla | e4eb51f | 2016-10-18 11:51:28 -0700 | [diff] [blame] | 275 | max_receive_message_size_, &args, sync_server_cqs, |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 276 | sync_server_settings_.min_pollers, sync_server_settings_.max_pollers, |
| 277 | sync_server_settings_.cq_timeout_msec)); |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 278 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 279 | ServerInitializer* initializer = server->initializer(); |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 280 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 281 | // Register all the completion queues with the server. i.e |
| 282 | // 1. sync_server_cqs: internal completion queues created IF this is a sync |
| 283 | // server |
| 284 | // 2. cqs_: Completion queues added via AddCompletionQueue() call |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 285 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 286 | for (auto it = sync_server_cqs->begin(); it != sync_server_cqs->end(); ++it) { |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 287 | grpc_server_register_completion_queue(server->server_, (*it)->cq(), |
| 288 | nullptr); |
Craig Tiller | 75bfb97 | 2017-04-11 17:55:12 -0700 | [diff] [blame] | 289 | num_frequently_polled_cqs++; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // cqs_ contains the completion queue added by calling the ServerBuilder's |
| 293 | // AddCompletionQueue() API. Some of them may not be frequently polled (i.e by |
| 294 | // calling Next() or AsyncNext()) and hence are not safe to be used for |
| 295 | // listening to incoming channels. Such completion queues must be registered |
| 296 | // as non-listening queues |
| 297 | for (auto it = cqs_.begin(); it != cqs_.end(); ++it) { |
Craig Tiller | 11c5832 | 2017-04-12 08:21:17 -0700 | [diff] [blame] | 298 | grpc_server_register_completion_queue(server->server_, (*it)->cq(), |
| 299 | nullptr); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 300 | } |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 301 | |
| 302 | if (num_frequently_polled_cqs == 0) { |
| 303 | gpr_log(GPR_ERROR, |
Craig Tiller | aae6c2c | 2016-05-20 08:58:30 -0700 | [diff] [blame] | 304 | "At least one of the completion queues must be frequently polled"); |
Sree Kuchibhotla | 7def036 | 2016-04-21 14:54:32 -0700 | [diff] [blame] | 305 | return nullptr; |
| 306 | } |
| 307 | |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 308 | for (auto service = services_.begin(); service != services_.end(); |
| 309 | service++) { |
Craig Tiller | d9b6fcf | 2015-07-07 16:28:20 -0700 | [diff] [blame] | 310 | if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 311 | return nullptr; |
| 312 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 313 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 314 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 315 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 316 | (*plugin)->InitServer(initializer); |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 317 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 318 | |
Yang Gao | 005eb88 | 2015-03-11 22:17:13 -0700 | [diff] [blame] | 319 | if (generic_service_) { |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 320 | server->RegisterAsyncGenericService(generic_service_); |
yang-g | 1ac6f45 | 2016-01-15 11:46:40 -0800 | [diff] [blame] | 321 | } else { |
| 322 | for (auto it = services_.begin(); it != services_.end(); ++it) { |
| 323 | if ((*it)->service->has_generic_methods()) { |
| 324 | gpr_log(GPR_ERROR, |
| 325 | "Some methods were marked generic but there is no " |
| 326 | "generic service registered."); |
| 327 | return nullptr; |
| 328 | } |
| 329 | } |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 330 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 331 | |
Craig Tiller | 12352b2 | 2017-01-10 15:16:14 -0800 | [diff] [blame] | 332 | bool added_port = false; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 333 | for (auto port = ports_.begin(); port != ports_.end(); port++) { |
| 334 | int r = server->AddListeningPort(port->addr, port->creds.get()); |
Craig Tiller | 12352b2 | 2017-01-10 15:16:14 -0800 | [diff] [blame] | 335 | if (!r) { |
| 336 | if (added_port) server->Shutdown(); |
| 337 | return nullptr; |
| 338 | } |
| 339 | added_port = true; |
Vijay Pai | 82dd80a | 2015-03-24 10:36:08 -0700 | [diff] [blame] | 340 | if (port->selected_port != nullptr) { |
| 341 | *port->selected_port = r; |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 342 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 343 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 344 | |
yang-g | 61e461e | 2015-09-03 13:08:59 -0700 | [diff] [blame] | 345 | auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0]; |
Craig Tiller | 9d9313c | 2017-04-11 15:05:46 -0700 | [diff] [blame] | 346 | server->Start(cqs_data, cqs_.size()); |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 347 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 348 | for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 349 | (*plugin)->Finish(initializer); |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 350 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 351 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 352 | return server; |
| 353 | } |
| 354 | |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 355 | void ServerBuilder::InternalAddPluginFactory( |
| 356 | std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) { |
| 357 | gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 358 | (*g_plugin_factory_list).push_back(CreatePlugin); |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 361 | } // namespace grpc |