You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

server_builder.h 18 kB

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