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 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPCXX_SERVER_BUILDER_H |
| 35 | #define GRPCXX_SERVER_BUILDER_H |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 37 | #include <climits> |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 38 | #include <map> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <memory> |
| 40 | #include <vector> |
| 41 | |
Craig Tiller | 4751c28 | 2017-01-10 14:29:00 -0800 | [diff] [blame] | 42 | #include <grpc++/impl/channel_argument_option.h> |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 43 | #include <grpc++/impl/server_builder_option.h> |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 44 | #include <grpc++/impl/server_builder_plugin.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 45 | #include <grpc++/support/config.h> |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 46 | #include <grpc/compression.h> |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 47 | #include <grpc/support/cpu.h> |
| 48 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 50 | struct grpc_resource_quota; |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 51 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | namespace grpc { |
| 53 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 54 | class AsyncGenericService; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 55 | class ResourceQuota; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 56 | class CompletionQueue; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | class RpcService; |
| 58 | class Server; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 59 | class ServerCompletionQueue; |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 60 | class ServerCredentials; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 61 | class Service; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 63 | namespace testing { |
| 64 | class ServerBuilderPluginTest; |
| 65 | } // namespace testing |
| 66 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 67 | /// A builder class for the creation and startup of \a grpc::Server instances. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 68 | class ServerBuilder { |
| 69 | public: |
| 70 | ServerBuilder(); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 71 | ~ServerBuilder(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 73 | enum SyncServerOption { NUM_CQS, MIN_POLLERS, MAX_POLLERS, CQ_TIMEOUT_MSEC }; |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 74 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 75 | /// Register a service. This call does not take ownership of the service. |
| 76 | /// The service must exist for the lifetime of the \a Server instance returned |
| 77 | /// by \a BuildAndStart(). |
| 78 | /// Matches requests with any :authority |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 79 | ServerBuilder& RegisterService(Service* service); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 81 | /// Register a generic service. |
| 82 | /// Matches requests with any :authority |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 83 | ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service); |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 84 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 85 | /// Register a service. This call does not take ownership of the service. |
| 86 | /// The service must exist for the lifetime of the \a Server instance returned |
| 87 | /// by BuildAndStart(). |
| 88 | /// Only matches requests with :authority \a host |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 89 | ServerBuilder& RegisterService(const grpc::string& host, Service* service); |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 90 | |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 91 | /// Set max receive message size in bytes. |
| 92 | ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) { |
| 93 | max_receive_message_size_ = max_receive_message_size; |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 94 | return *this; |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 97 | /// Set max send message size in bytes. |
| 98 | ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) { |
| 99 | max_send_message_size_ = max_send_message_size; |
| 100 | return *this; |
| 101 | } |
| 102 | |
Mark D. Roth | 0e25610 | 2016-09-15 09:01:05 -0700 | [diff] [blame] | 103 | /// \deprecated For backward compatibility. |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 104 | ServerBuilder& SetMaxMessageSize(int max_message_size) { |
| 105 | return SetMaxReceiveMessageSize(max_message_size); |
| 106 | } |
| 107 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 108 | /// Set the support status for compression algorithms. All algorithms are |
| 109 | /// enabled by default. |
| 110 | /// |
| 111 | /// Incoming calls compressed with an unsupported algorithm will fail with |
| 112 | /// GRPC_STATUS_UNIMPLEMENTED. |
| 113 | ServerBuilder& SetCompressionAlgorithmSupportStatus( |
| 114 | grpc_compression_algorithm algorithm, bool enabled); |
David Garcia Quintas | 49dd250 | 2015-09-08 16:01:09 -0700 | [diff] [blame] | 115 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 116 | /// The default compression level to use for all channel calls in the |
| 117 | /// absence of a call-specific level. |
| 118 | ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level); |
| 119 | |
| 120 | /// The default compression algorithm to use for all channel calls in the |
| 121 | /// absence of a call-specific level. Note that it overrides any compression |
| 122 | /// level set by \a SetDefaultCompressionLevel. |
| 123 | ServerBuilder& SetDefaultCompressionAlgorithm( |
| 124 | grpc_compression_algorithm algorithm); |
| 125 | |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 126 | /// Set the attached buffer pool for this server |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 127 | ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota); |
Craig Tiller | db1a5cc | 2016-09-28 14:22:12 -0700 | [diff] [blame] | 128 | |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 129 | ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option); |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 130 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 131 | /// Only useful if this is a Synchronous server. |
| 132 | ServerBuilder& SetSyncServerOption(SyncServerOption option, int value); |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 133 | |
Craig Tiller | 4751c28 | 2017-01-10 14:29:00 -0800 | [diff] [blame] | 134 | /// Add a channel argument (an escape hatch to tuning core library parameters |
| 135 | /// directly) |
| 136 | template <class T> |
| 137 | ServerBuilder& AddChannelArgument(const grpc::string& arg, const T& value) { |
| 138 | return SetOption(MakeChannelArgumentOption(arg, value)); |
| 139 | } |
| 140 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 141 | /// Tries to bind \a server to the given \a addr. |
| 142 | /// |
| 143 | /// It can be invoked multiple times. |
| 144 | /// |
| 145 | /// \param addr The address to try to bind to the server (eg, localhost:1234, |
| 146 | /// 192.168.1.1:31416, [::1]:27182, etc.). |
| 147 | /// \params creds The credentials associated with the server. |
| 148 | /// \param selected_port[out] Upon success, updated to contain the port |
| 149 | /// number. \a nullptr otherwise. |
| 150 | /// |
| 151 | // TODO(dgq): the "port" part seems to be a misnomer. |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 152 | ServerBuilder& AddListeningPort(const grpc::string& addr, |
| 153 | std::shared_ptr<ServerCredentials> creds, |
| 154 | int* selected_port = nullptr); |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 155 | |
David Garcia Quintas | 058c9de | 2016-06-13 19:04:43 -0700 | [diff] [blame] | 156 | /// Add a completion queue for handling asynchronous services. |
| 157 | /// |
| 158 | /// Caller is required to shutdown the server prior to shutting down the |
| 159 | /// returned completion queue. A typical usage scenario: |
| 160 | /// |
| 161 | /// // While building the server: |
| 162 | /// ServerBuilder builder; |
| 163 | /// ... |
| 164 | /// cq_ = builder.AddCompletionQueue(); |
| 165 | /// server_ = builder.BuildAndStart(); |
| 166 | /// |
| 167 | /// // While shutting down the server; |
| 168 | /// server_->Shutdown(); |
| 169 | /// cq_->Shutdown(); // Always *after* the associated server's Shutdown()! |
Sree Kuchibhotla | 1f5e262 | 2016-04-21 12:28:09 -0700 | [diff] [blame] | 170 | /// |
| 171 | /// \param is_frequently_polled This is an optional parameter to inform GRPC |
| 172 | /// library about whether this completion queue would be frequently polled |
| 173 | /// (i.e by calling Next() or AsyncNext()). The default value is 'true' and is |
| 174 | /// the recommended setting. Setting this to 'false' (i.e not polling the |
| 175 | /// completion queue frequently) will have a significantly negative |
| 176 | /// performance impact and hence should not be used in production use cases. |
| 177 | std::unique_ptr<ServerCompletionQueue> AddCompletionQueue( |
| 178 | bool is_frequently_polled = true); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 179 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 180 | /// Return a running server which is ready for processing calls. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 181 | std::unique_ptr<Server> BuildAndStart(); |
| 182 | |
Yuchen Zeng | 7d099a5 | 2016-05-06 13:21:36 -0700 | [diff] [blame] | 183 | /// For internal use only: Register a ServerBuilderPlugin factory function. |
Yuchen Zeng | 3b8f335 | 2016-05-03 12:18:13 -0700 | [diff] [blame] | 184 | static void InternalAddPluginFactory( |
| 185 | std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()); |
| 186 | |
Muxi Yan | cdc0d03 | 2017-05-09 16:04:10 -0700 | [diff] [blame] | 187 | /// Enable a server workaround. Do not use unless you know what the workaround |
| 188 | /// does. For explanation and detailed descriptions of workarounds, see |
| 189 | /// docs/workarounds.md. |
| 190 | ServerBuilder& EnableWorkaround(uint32_t id); |
| 191 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 192 | private: |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 193 | friend class ::grpc::testing::ServerBuilderPluginTest; |
| 194 | |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 195 | struct Port { |
| 196 | grpc::string addr; |
| 197 | std::shared_ptr<ServerCredentials> creds; |
| 198 | int* selected_port; |
| 199 | }; |
| 200 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 201 | struct SyncServerSettings { |
| 202 | SyncServerSettings() |
Sree Kuchibhotla | 9229016 | 2017-04-21 15:40:27 -0700 | [diff] [blame] | 203 | : num_cqs(gpr_cpu_num_cores()), |
| 204 | min_pollers(1), |
| 205 | max_pollers(2), |
| 206 | cq_timeout_msec(10000) {} |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 207 | |
| 208 | // Number of server completion queues to create to listen to incoming RPCs. |
| 209 | int num_cqs; |
| 210 | |
| 211 | // Minimum number of threads per completion queue that should be listening |
| 212 | // to incoming RPCs. |
| 213 | int min_pollers; |
| 214 | |
| 215 | // Maximum number of threads per completion queue that can be listening to |
| 216 | // incoming RPCs. |
| 217 | int max_pollers; |
| 218 | |
| 219 | // The timeout for server completion queue's AsyncNext call. |
| 220 | int cq_timeout_msec; |
| 221 | }; |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 222 | |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 223 | typedef std::unique_ptr<grpc::string> HostString; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 224 | struct NamedService { |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 225 | explicit NamedService(Service* s) : service(s) {} |
| 226 | NamedService(const grpc::string& h, Service* s) |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 227 | : host(new grpc::string(h)), service(s) {} |
| 228 | HostString host; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 229 | Service* service; |
Craig Tiller | 822d2c7 | 2015-07-07 16:08:00 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 232 | int max_receive_message_size_; |
| 233 | int max_send_message_size_; |
yang-g | a23f17b | 2015-11-25 10:21:05 -0800 | [diff] [blame] | 234 | std::vector<std::unique_ptr<ServerBuilderOption>> options_; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 235 | std::vector<std::unique_ptr<NamedService>> services_; |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 236 | std::vector<Port> ports_; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 237 | |
Sree Kuchibhotla | 9676619 | 2016-10-13 12:42:54 -0700 | [diff] [blame] | 238 | SyncServerSettings sync_server_settings_; |
| 239 | |
Sree Kuchibhotla | 385c9b2 | 2016-10-18 16:26:38 -0700 | [diff] [blame] | 240 | // List of completion queues added via AddCompletionQueue() method |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 241 | std::vector<ServerCompletionQueue*> cqs_; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 242 | |
yangg | 9e21f72 | 2014-12-08 15:49:52 -0800 | [diff] [blame] | 243 | std::shared_ptr<ServerCredentials> creds_; |
Vijay Pai | 15855f3 | 2016-06-10 12:25:32 -0700 | [diff] [blame] | 244 | std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 245 | grpc_resource_quota* resource_quota_; |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 246 | AsyncGenericService* generic_service_; |
David Garcia Quintas | 2d02456 | 2016-05-17 23:24:39 -0700 | [diff] [blame] | 247 | struct { |
| 248 | bool is_set; |
| 249 | grpc_compression_level level; |
| 250 | } maybe_default_compression_level_; |
| 251 | struct { |
| 252 | bool is_set; |
| 253 | grpc_compression_algorithm algorithm; |
| 254 | } maybe_default_compression_algorithm_; |
| 255 | uint32_t enabled_compression_algorithms_bitset_; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | } // namespace grpc |
| 259 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 260 | #endif // GRPCXX_SERVER_BUILDER_H |