server_builder.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. //
  3. // Copyright 2015-2016 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SERVER_BUILDER_H
  19. #define GRPCPP_SERVER_BUILDER_H
  20. #include <grpc/support/port_platform.h>
  21. #include <climits>
  22. #include <map>
  23. #include <memory>
  24. #include <vector>
  25. #include <grpc/compression.h>
  26. #include <grpc/support/cpu.h>
  27. #include <grpc/support/workaround_list.h>
  28. #include <grpcpp/impl/channel_argument_option.h>
  29. #include <grpcpp/impl/server_builder_option.h>
  30. #include <grpcpp/impl/server_builder_plugin.h>
  31. #include <grpcpp/security/authorization_policy_provider.h>
  32. #include <grpcpp/server.h>
  33. #include <grpcpp/support/config.h>
  34. #include <grpcpp/support/server_interceptor.h>
  35. struct grpc_resource_quota;
  36. namespace grpc {
  37. class CompletionQueue;
  38. class Server;
  39. class ServerCompletionQueue;
  40. class AsyncGenericService;
  41. class ResourceQuota;
  42. class ServerCredentials;
  43. class Service;
  44. namespace testing {
  45. class ServerBuilderPluginTest;
  46. } // namespace testing
  47. namespace internal {
  48. class ExternalConnectionAcceptorImpl;
  49. } // namespace internal
  50. class CallbackGenericService;
  51. namespace experimental {
  52. // EXPERIMENTAL API:
  53. // Interface for a grpc server to build transports with connections created out
  54. // of band.
  55. // See ServerBuilder's AddExternalConnectionAcceptor API.
  56. class ExternalConnectionAcceptor {
  57. public:
  58. struct NewConnectionParameters {
  59. int listener_fd = -1;
  60. int fd = -1;
  61. ByteBuffer read_buffer; // data intended for the grpc server
  62. };
  63. virtual ~ExternalConnectionAcceptor() {}
  64. // If called before grpc::Server is started or after it is shut down, the new
  65. // connection will be closed.
  66. virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
  67. };
  68. } // namespace experimental
  69. } // namespace grpc
  70. namespace grpc {
  71. /// A builder class for the creation and startup of \a grpc::Server instances.
  72. class ServerBuilder {
  73. public:
  74. ServerBuilder();
  75. virtual ~ServerBuilder();
  76. //////////////////////////////////////////////////////////////////////////////
  77. // Primary API's
  78. /// Return a running server which is ready for processing calls.
  79. /// Before calling, one typically needs to ensure that:
  80. /// 1. a service is registered - so that the server knows what to serve
  81. /// (via RegisterService, or RegisterAsyncGenericService)
  82. /// 2. a listening port has been added - so the server knows where to receive
  83. /// traffic (via AddListeningPort)
  84. /// 3. [for async api only] completion queues have been added via
  85. /// AddCompletionQueue
  86. ///
  87. /// Will return a nullptr on errors.
  88. virtual std::unique_ptr<grpc::Server> BuildAndStart();
  89. /// Register a service. This call does not take ownership of the service.
  90. /// The service must exist for the lifetime of the \a Server instance returned
  91. /// by \a BuildAndStart().
  92. /// Matches requests with any :authority
  93. ServerBuilder& RegisterService(grpc::Service* service);
  94. /// Enlists an endpoint \a addr (port with an optional IP address) to
  95. /// bind the \a grpc::Server object to be created to.
  96. ///
  97. /// It can be invoked multiple times.
  98. ///
  99. /// \param addr_uri The address to try to bind to the server in URI form. If
  100. /// the scheme name is omitted, "dns:///" is assumed. To bind to any address,
  101. /// please use IPv6 any, i.e., [::]:<port>, which also accepts IPv4
  102. /// connections. Valid values include dns:///localhost:1234,
  103. /// 192.168.1.1:31416, dns:///[::1]:27182, etc.
  104. /// \param creds The credentials associated with the server.
  105. /// \param[out] selected_port If not `nullptr`, gets populated with the port
  106. /// number bound to the \a grpc::Server for the corresponding endpoint after
  107. /// it is successfully bound by BuildAndStart(), 0 otherwise. AddListeningPort
  108. /// does not modify this pointer.
  109. ServerBuilder& AddListeningPort(
  110. const TString& addr_uri,
  111. std::shared_ptr<grpc::ServerCredentials> creds,
  112. int* selected_port = nullptr);
  113. /// Add a completion queue for handling asynchronous services.
  114. ///
  115. /// Best performance is typically obtained by using one thread per polling
  116. /// completion queue.
  117. ///
  118. /// Caller is required to shutdown the server prior to shutting down the
  119. /// returned completion queue. Caller is also required to drain the
  120. /// completion queue after shutting it down. A typical usage scenario:
  121. ///
  122. /// // While building the server:
  123. /// ServerBuilder builder;
  124. /// ...
  125. /// cq_ = builder.AddCompletionQueue();
  126. /// server_ = builder.BuildAndStart();
  127. ///
  128. /// // While shutting down the server;
  129. /// server_->Shutdown();
  130. /// cq_->Shutdown(); // Always *after* the associated server's Shutdown()!
  131. /// // Drain the cq_ that was created
  132. /// void* ignored_tag;
  133. /// bool ignored_ok;
  134. /// while (cq_->Next(&ignored_tag, &ignored_ok)) { }
  135. ///
  136. /// \param is_frequently_polled This is an optional parameter to inform gRPC
  137. /// library about whether this completion queue would be frequently polled
  138. /// (i.e. by calling \a Next() or \a AsyncNext()). The default value is
  139. /// 'true' and is the recommended setting. Setting this to 'false' (i.e.
  140. /// not polling the completion queue frequently) will have a significantly
  141. /// negative performance impact and hence should not be used in production
  142. /// use cases.
  143. std::unique_ptr<grpc::ServerCompletionQueue> AddCompletionQueue(
  144. bool is_frequently_polled = true);
  145. //////////////////////////////////////////////////////////////////////////////
  146. // Less commonly used RegisterService variants
  147. /// Register a service. This call does not take ownership of the service.
  148. /// The service must exist for the lifetime of the \a Server instance
  149. /// returned by \a BuildAndStart(). Only matches requests with :authority \a
  150. /// host
  151. ServerBuilder& RegisterService(const TString& host,
  152. grpc::Service* service);
  153. /// Register a generic service.
  154. /// Matches requests with any :authority
  155. /// This is mostly useful for writing generic gRPC Proxies where the exact
  156. /// serialization format is unknown
  157. ServerBuilder& RegisterAsyncGenericService(
  158. grpc::AsyncGenericService* service);
  159. //////////////////////////////////////////////////////////////////////////////
  160. // Fine control knobs
  161. /// Set max receive message size in bytes.
  162. /// The default is GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH.
  163. ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
  164. max_receive_message_size_ = max_receive_message_size;
  165. return *this;
  166. }
  167. /// Set max send message size in bytes.
  168. /// The default is GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH.
  169. ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
  170. max_send_message_size_ = max_send_message_size;
  171. return *this;
  172. }
  173. /// \deprecated For backward compatibility.
  174. ServerBuilder& SetMaxMessageSize(int max_message_size) {
  175. return SetMaxReceiveMessageSize(max_message_size);
  176. }
  177. /// Set the support status for compression algorithms. All algorithms are
  178. /// enabled by default.
  179. ///
  180. /// Incoming calls compressed with an unsupported algorithm will fail with
  181. /// \a GRPC_STATUS_UNIMPLEMENTED.
  182. ServerBuilder& SetCompressionAlgorithmSupportStatus(
  183. grpc_compression_algorithm algorithm, bool enabled);
  184. /// The default compression level to use for all channel calls in the
  185. /// absence of a call-specific level.
  186. ServerBuilder& SetDefaultCompressionLevel(grpc_compression_level level);
  187. /// The default compression algorithm to use for all channel calls in the
  188. /// absence of a call-specific level. Note that it overrides any compression
  189. /// level set by \a SetDefaultCompressionLevel.
  190. ServerBuilder& SetDefaultCompressionAlgorithm(
  191. grpc_compression_algorithm algorithm);
  192. /// Set the attached buffer pool for this server
  193. ServerBuilder& SetResourceQuota(const grpc::ResourceQuota& resource_quota);
  194. ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
  195. /// Options for synchronous servers.
  196. enum SyncServerOption {
  197. NUM_CQS, ///< Number of completion queues.
  198. MIN_POLLERS, ///< Minimum number of polling threads.
  199. MAX_POLLERS, ///< Maximum number of polling threads.
  200. CQ_TIMEOUT_MSEC ///< Completion queue timeout in milliseconds.
  201. };
  202. /// Only useful if this is a Synchronous server.
  203. ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
  204. /// Add a channel argument (an escape hatch to tuning core library parameters
  205. /// directly)
  206. template <class T>
  207. ServerBuilder& AddChannelArgument(const TString& arg, const T& value) {
  208. return SetOption(grpc::MakeChannelArgumentOption(arg, value));
  209. }
  210. /// For internal use only: Register a ServerBuilderPlugin factory function.
  211. static void InternalAddPluginFactory(
  212. std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
  213. /// Enable a server workaround. Do not use unless you know what the workaround
  214. /// does. For explanation and detailed descriptions of workarounds, see
  215. /// doc/workarounds.md.
  216. ServerBuilder& EnableWorkaround(grpc_workaround_list id);
  217. /// NOTE: class experimental_type is not part of the public API of this class.
  218. /// TODO(yashykt): Integrate into public API when this is no longer
  219. /// experimental.
  220. class experimental_type {
  221. public:
  222. explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
  223. void SetInterceptorCreators(
  224. std::vector<std::unique_ptr<
  225. grpc::experimental::ServerInterceptorFactoryInterface>>
  226. interceptor_creators) {
  227. builder_->interceptor_creators_ = std::move(interceptor_creators);
  228. }
  229. enum class ExternalConnectionType {
  230. FROM_FD = 0 // in the form of a file descriptor
  231. };
  232. /// Register an acceptor to handle the externally accepted connection in
  233. /// grpc server. The returned acceptor can be used to pass the connection
  234. /// to grpc server, where a channel will be created with the provided
  235. /// server credentials.
  236. std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
  237. AddExternalConnectionAcceptor(ExternalConnectionType type,
  238. std::shared_ptr<ServerCredentials> creds);
  239. /// Sets server authorization policy provider in
  240. /// GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER channel argument.
  241. void SetAuthorizationPolicyProvider(
  242. std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
  243. provider);
  244. /// Enables per-call load reporting. The server will automatically send the
  245. /// load metrics after each RPC. The caller can report load metrics for the
  246. /// current call to what ServerContext::ExperimentalGetCallMetricRecorder()
  247. /// returns. The server merges metrics from the optional
  248. /// server_metric_recorder when provided where the call metric recorder take
  249. /// a higher precedence. The caller owns and must ensure the server metric
  250. /// recorder outlives the server.
  251. void EnableCallMetricRecording(
  252. experimental::ServerMetricRecorder* server_metric_recorder = nullptr);
  253. private:
  254. ServerBuilder* builder_;
  255. };
  256. /// Set the allocator for creating and releasing callback server context.
  257. /// Takes the owndership of the allocator.
  258. ServerBuilder& SetContextAllocator(
  259. std::unique_ptr<grpc::ContextAllocator> context_allocator);
  260. /// Register a generic service that uses the callback API.
  261. /// Matches requests with any :authority
  262. /// This is mostly useful for writing generic gRPC Proxies where the exact
  263. /// serialization format is unknown
  264. ServerBuilder& RegisterCallbackGenericService(
  265. grpc::CallbackGenericService* service);
  266. /// NOTE: The function experimental() is not stable public API. It is a view
  267. /// to the experimental components of this class. It may be changed or removed
  268. /// at any time.
  269. experimental_type experimental() { return experimental_type(this); }
  270. protected:
  271. /// Experimental, to be deprecated
  272. struct Port {
  273. TString addr;
  274. std::shared_ptr<ServerCredentials> creds;
  275. int* selected_port;
  276. };
  277. /// Experimental, to be deprecated
  278. typedef std::unique_ptr<TString> HostString;
  279. struct NamedService {
  280. explicit NamedService(grpc::Service* s) : service(s) {}
  281. NamedService(const TString& h, grpc::Service* s)
  282. : host(new TString(h)), service(s) {}
  283. HostString host;
  284. grpc::Service* service;
  285. };
  286. /// Experimental, to be deprecated
  287. std::vector<Port> ports() { return ports_; }
  288. /// Experimental, to be deprecated
  289. std::vector<NamedService*> services() {
  290. std::vector<NamedService*> service_refs;
  291. service_refs.reserve(services_.size());
  292. for (auto& ptr : services_) {
  293. service_refs.push_back(ptr.get());
  294. }
  295. return service_refs;
  296. }
  297. /// Experimental, to be deprecated
  298. std::vector<grpc::ServerBuilderOption*> options() {
  299. std::vector<grpc::ServerBuilderOption*> option_refs;
  300. option_refs.reserve(options_.size());
  301. for (auto& ptr : options_) {
  302. option_refs.push_back(ptr.get());
  303. }
  304. return option_refs;
  305. }
  306. /// Experimental API, subject to change.
  307. void set_fetcher(grpc_server_config_fetcher* server_config_fetcher) {
  308. server_config_fetcher_ = server_config_fetcher;
  309. }
  310. /// Experimental API, subject to change.
  311. virtual ChannelArguments BuildChannelArgs();
  312. private:
  313. friend class grpc::testing::ServerBuilderPluginTest;
  314. struct SyncServerSettings {
  315. SyncServerSettings()
  316. : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
  317. /// Number of server completion queues to create to listen to incoming RPCs.
  318. int num_cqs;
  319. /// Minimum number of threads per completion queue that should be listening
  320. /// to incoming RPCs.
  321. int min_pollers;
  322. /// Maximum number of threads per completion queue that can be listening to
  323. /// incoming RPCs.
  324. int max_pollers;
  325. /// The timeout for server completion queue's AsyncNext call.
  326. int cq_timeout_msec;
  327. };
  328. int max_receive_message_size_;
  329. int max_send_message_size_;
  330. std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
  331. std::vector<std::unique_ptr<NamedService>> services_;
  332. std::vector<Port> ports_;
  333. SyncServerSettings sync_server_settings_;
  334. /// List of completion queues added via \a AddCompletionQueue method.
  335. std::vector<grpc::ServerCompletionQueue*> cqs_;
  336. std::shared_ptr<grpc::ServerCredentials> creds_;
  337. std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
  338. grpc_resource_quota* resource_quota_;
  339. grpc::AsyncGenericService* generic_service_{nullptr};
  340. std::unique_ptr<ContextAllocator> context_allocator_;
  341. grpc::CallbackGenericService* callback_generic_service_{nullptr};
  342. struct {
  343. bool is_set;
  344. grpc_compression_level level;
  345. } maybe_default_compression_level_;
  346. struct {
  347. bool is_set;
  348. grpc_compression_algorithm algorithm;
  349. } maybe_default_compression_algorithm_;
  350. uint32_t enabled_compression_algorithms_bitset_;
  351. std::vector<
  352. std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
  353. interceptor_creators_;
  354. std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
  355. acceptors_;
  356. grpc_server_config_fetcher* server_config_fetcher_ = nullptr;
  357. std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
  358. authorization_provider_;
  359. experimental::ServerMetricRecorder* server_metric_recorder_ = nullptr;
  360. };
  361. } // namespace grpc
  362. #endif // GRPCPP_SERVER_BUILDER_H