blob: 9252c6a63a8b9d0700ec62f01b4f3960eda84f94 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Sree Kuchibhotla01907122016-04-21 15:09:13 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPCXX_SERVER_BUILDER_H
35#define GRPCXX_SERVER_BUILDER_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
Sree Kuchibhotla96766192016-10-13 12:42:54 -070037#include <climits>
Yuchen Zenga42ec212016-04-29 13:03:06 -070038#include <map>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <memory>
40#include <vector>
41
yang-ga23f17b2015-11-25 10:21:05 -080042#include <grpc++/impl/server_builder_option.h>
Yuchen Zenga42ec212016-04-29 13:03:06 -070043#include <grpc++/impl/server_builder_plugin.h>
yang-g9e2f90c2015-08-21 15:35:03 -070044#include <grpc++/support/config.h>
yang-ga23f17b2015-11-25 10:21:05 -080045#include <grpc/compression.h>
Sree Kuchibhotla96766192016-10-13 12:42:54 -070046#include <grpc/support/cpu.h>
47#include <grpc/support/useful.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Craig Tiller20afa3d2016-10-17 14:52:14 -070049struct grpc_resource_quota;
Craig Tillerdb1a5cc2016-09-28 14:22:12 -070050
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051namespace grpc {
52
Yang Gao49996492015-03-12 16:40:19 -070053class AsyncGenericService;
Craig Tiller20afa3d2016-10-17 14:52:14 -070054class ResourceQuota;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080055class CompletionQueue;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056class RpcService;
57class Server;
Craig Tillerf9e6adf2015-05-06 11:45:59 -070058class ServerCompletionQueue;
yangg9e21f722014-12-08 15:49:52 -080059class ServerCredentials;
Craig Tiller15f383c2016-01-07 12:45:32 -080060class Service;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Yuchen Zenga42ec212016-04-29 13:03:06 -070062namespace testing {
63class ServerBuilderPluginTest;
64} // namespace testing
65
Craig Tillerd6599a32015-09-03 09:37:02 -070066/// A builder class for the creation and startup of \a grpc::Server instances.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067class ServerBuilder {
68 public:
69 ServerBuilder();
Craig Tillerdb1a5cc2016-09-28 14:22:12 -070070 ~ServerBuilder();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
Sree Kuchibhotla96766192016-10-13 12:42:54 -070072 enum SyncServerOption { NUM_CQS, MIN_POLLERS, MAX_POLLERS, CQ_TIMEOUT_MSEC };
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -070073
Craig Tillerd6599a32015-09-03 09:37:02 -070074 /// Register a service. This call does not take ownership of the service.
75 /// The service must exist for the lifetime of the \a Server instance returned
76 /// by \a BuildAndStart().
77 /// Matches requests with any :authority
David Garcia Quintas2d024562016-05-17 23:24:39 -070078 ServerBuilder& RegisterService(Service* service);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079
Craig Tillerd6599a32015-09-03 09:37:02 -070080 /// Register a generic service.
81 /// Matches requests with any :authority
David Garcia Quintas2d024562016-05-17 23:24:39 -070082 ServerBuilder& RegisterAsyncGenericService(AsyncGenericService* service);
Craig Tillerdb57c4f2015-02-24 10:34:47 -080083
Craig Tillerd6599a32015-09-03 09:37:02 -070084 /// Register a service. This call does not take ownership of the service.
85 /// The service must exist for the lifetime of the \a Server instance returned
86 /// by BuildAndStart().
87 /// Only matches requests with :authority \a host
David Garcia Quintas2d024562016-05-17 23:24:39 -070088 ServerBuilder& RegisterService(const grpc::string& host, Service* service);
Craig Tiller822d2c72015-07-07 16:08:00 -070089
Mark D. Roth69803622016-09-06 08:15:36 -070090 /// Set max receive message size in bytes.
91 ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
92 max_receive_message_size_ = max_receive_message_size;
David Garcia Quintas2d024562016-05-17 23:24:39 -070093 return *this;
Yang Gao3921c562015-04-30 16:07:06 -070094 }
95
Mark D. Roth69803622016-09-06 08:15:36 -070096 /// Set max send message size in bytes.
97 ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
98 max_send_message_size_ = max_send_message_size;
99 return *this;
100 }
101
Mark D. Roth0e256102016-09-15 09:01:05 -0700102 /// \deprecated For backward compatibility.
Mark D. Roth69803622016-09-06 08:15:36 -0700103 ServerBuilder& SetMaxMessageSize(int max_message_size) {
104 return SetMaxReceiveMessageSize(max_message_size);
105 }
106
David Garcia Quintas2d024562016-05-17 23:24:39 -0700107 /// Set the support status for compression algorithms. All algorithms are
108 /// enabled by default.
109 ///
110 /// Incoming calls compressed with an unsupported algorithm will fail with
111 /// GRPC_STATUS_UNIMPLEMENTED.
112 ServerBuilder& SetCompressionAlgorithmSupportStatus(
113 grpc_compression_algorithm algorithm, bool enabled);
David Garcia Quintas49dd2502015-09-08 16:01:09 -0700114
David Garcia Quintas2d024562016-05-17 23:24:39 -0700115 /// The default compression level to use for all channel calls in the
116 /// absence of a call-specific level.
117 ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
118
119 /// The default compression algorithm to use for all channel calls in the
120 /// absence of a call-specific level. Note that it overrides any compression
121 /// level set by \a SetDefaultCompressionLevel.
122 ServerBuilder& SetDefaultCompressionAlgorithm(
123 grpc_compression_algorithm algorithm);
124
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700125 /// Set the attached buffer pool for this server
Craig Tiller20afa3d2016-10-17 14:52:14 -0700126 ServerBuilder& SetResourceQuota(const ResourceQuota& resource_quota);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700127
David Garcia Quintas2d024562016-05-17 23:24:39 -0700128 ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
yang-ga23f17b2015-11-25 10:21:05 -0800129
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700130 /// Only useful if this is a Synchronous server.
131 ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700132
Craig Tillerd6599a32015-09-03 09:37:02 -0700133 /// Tries to bind \a server to the given \a addr.
134 ///
135 /// It can be invoked multiple times.
136 ///
137 /// \param addr The address to try to bind to the server (eg, localhost:1234,
138 /// 192.168.1.1:31416, [::1]:27182, etc.).
139 /// \params creds The credentials associated with the server.
140 /// \param selected_port[out] Upon success, updated to contain the port
141 /// number. \a nullptr otherwise.
142 ///
143 // TODO(dgq): the "port" part seems to be a misnomer.
David Garcia Quintas2d024562016-05-17 23:24:39 -0700144 ServerBuilder& AddListeningPort(const grpc::string& addr,
145 std::shared_ptr<ServerCredentials> creds,
146 int* selected_port = nullptr);
yangg9e21f722014-12-08 15:49:52 -0800147
David Garcia Quintas058c9de2016-06-13 19:04:43 -0700148 /// Add a completion queue for handling asynchronous services.
149 ///
150 /// Caller is required to shutdown the server prior to shutting down the
151 /// returned completion queue. A typical usage scenario:
152 ///
153 /// // While building the server:
154 /// ServerBuilder builder;
155 /// ...
156 /// cq_ = builder.AddCompletionQueue();
157 /// server_ = builder.BuildAndStart();
158 ///
159 /// // While shutting down the server;
160 /// server_->Shutdown();
161 /// cq_->Shutdown(); // Always *after* the associated server's Shutdown()!
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700162 ///
163 /// \param is_frequently_polled This is an optional parameter to inform GRPC
164 /// library about whether this completion queue would be frequently polled
165 /// (i.e by calling Next() or AsyncNext()). The default value is 'true' and is
166 /// the recommended setting. Setting this to 'false' (i.e not polling the
167 /// completion queue frequently) will have a significantly negative
168 /// performance impact and hence should not be used in production use cases.
169 std::unique_ptr<ServerCompletionQueue> AddCompletionQueue(
170 bool is_frequently_polled = true);
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700171
Craig Tillerd6599a32015-09-03 09:37:02 -0700172 /// Return a running server which is ready for processing calls.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800173 std::unique_ptr<Server> BuildAndStart();
174
Yuchen Zeng7d099a52016-05-06 13:21:36 -0700175 /// For internal use only: Register a ServerBuilderPlugin factory function.
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700176 static void InternalAddPluginFactory(
177 std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)());
178
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800179 private:
Yuchen Zenga42ec212016-04-29 13:03:06 -0700180 friend class ::grpc::testing::ServerBuilderPluginTest;
181
Craig Tiller42bc87c2015-02-23 08:50:19 -0800182 struct Port {
183 grpc::string addr;
184 std::shared_ptr<ServerCredentials> creds;
185 int* selected_port;
186 };
187
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700188 struct SyncServerSettings {
189 SyncServerSettings()
190 : num_cqs(GPR_MAX(gpr_cpu_num_cores(), 4)),
191 min_pollers(1),
192 max_pollers(INT_MAX),
Sree Kuchibhotlac37a8a52016-10-13 15:40:15 -0700193 cq_timeout_msec(1000) {}
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700194
195 // Number of server completion queues to create to listen to incoming RPCs.
196 int num_cqs;
197
198 // Minimum number of threads per completion queue that should be listening
199 // to incoming RPCs.
200 int min_pollers;
201
202 // Maximum number of threads per completion queue that can be listening to
203 // incoming RPCs.
204 int max_pollers;
205
206 // The timeout for server completion queue's AsyncNext call.
207 int cq_timeout_msec;
208 };
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700209
Craig Tiller822d2c72015-07-07 16:08:00 -0700210 typedef std::unique_ptr<grpc::string> HostString;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700211 struct NamedService {
Craig Tiller15f383c2016-01-07 12:45:32 -0800212 explicit NamedService(Service* s) : service(s) {}
213 NamedService(const grpc::string& h, Service* s)
Craig Tiller822d2c72015-07-07 16:08:00 -0700214 : host(new grpc::string(h)), service(s) {}
215 HostString host;
Craig Tiller15f383c2016-01-07 12:45:32 -0800216 Service* service;
Craig Tiller822d2c72015-07-07 16:08:00 -0700217 };
218
Mark D. Roth69803622016-09-06 08:15:36 -0700219 int max_receive_message_size_;
220 int max_send_message_size_;
yang-ga23f17b2015-11-25 10:21:05 -0800221 std::vector<std::unique_ptr<ServerBuilderOption>> options_;
Craig Tiller15f383c2016-01-07 12:45:32 -0800222 std::vector<std::unique_ptr<NamedService>> services_;
Craig Tiller42bc87c2015-02-23 08:50:19 -0800223 std::vector<Port> ports_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700224
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700225 SyncServerSettings sync_server_settings_;
226
Sree Kuchibhotla385c9b22016-10-18 16:26:38 -0700227 // List of completion queues added via AddCompletionQueue() method
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700228 std::vector<ServerCompletionQueue*> cqs_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700229
yangg9e21f722014-12-08 15:49:52 -0800230 std::shared_ptr<ServerCredentials> creds_;
Vijay Pai15855f32016-06-10 12:25:32 -0700231 std::vector<std::unique_ptr<ServerBuilderPlugin>> plugins_;
Craig Tiller20afa3d2016-10-17 14:52:14 -0700232 grpc_resource_quota* resource_quota_;
Yang Gao49996492015-03-12 16:40:19 -0700233 AsyncGenericService* generic_service_;
David Garcia Quintas2d024562016-05-17 23:24:39 -0700234 struct {
235 bool is_set;
236 grpc_compression_level level;
237 } maybe_default_compression_level_;
238 struct {
239 bool is_set;
240 grpc_compression_algorithm algorithm;
241 } maybe_default_compression_algorithm_;
242 uint32_t enabled_compression_algorithms_bitset_;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243};
244
245} // namespace grpc
246
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100247#endif // GRPCXX_SERVER_BUILDER_H