GRPC C++  1.39.1
server_builder.h
Go to the documentation of this file.
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 
19 #ifndef GRPCPP_SERVER_BUILDER_H
20 #define GRPCPP_SERVER_BUILDER_H
21 
22 #include <climits>
23 #include <map>
24 #include <memory>
25 #include <vector>
26 
28 
29 #include <grpc/compression.h>
30 #include <grpc/support/cpu.h>
37 #include <grpcpp/server.h>
38 #include <grpcpp/support/config.h>
39 
40 struct grpc_resource_quota;
41 
42 namespace grpc {
43 
44 class CompletionQueue;
45 class Server;
46 class ServerCompletionQueue;
47 class AsyncGenericService;
48 class ResourceQuota;
49 class ServerCredentials;
50 class Service;
51 namespace testing {
52 class ServerBuilderPluginTest;
53 } // namespace testing
54 
55 namespace internal {
56 class ExternalConnectionAcceptorImpl;
57 } // namespace internal
58 
59 class CallbackGenericService;
60 
61 namespace experimental {
62 // EXPERIMENTAL API:
63 // Interface for a grpc server to build transports with connections created out
64 // of band.
65 // See ServerBuilder's AddExternalConnectionAcceptor API.
67  public:
69  int listener_fd = -1;
70  int fd = -1;
71  ByteBuffer read_buffer; // data intended for the grpc server
72  };
74  // If called before grpc::Server is started or after it is shut down, the new
75  // connection will be closed.
77 };
78 
79 } // namespace experimental
80 } // namespace grpc
81 
82 namespace grpc {
83 
86  public:
87  ServerBuilder();
88  virtual ~ServerBuilder();
89 
91  // Primary API's
92 
103  virtual std::unique_ptr<grpc::Server> BuildAndStart();
104 
110 
127  const std::string& addr_uri,
128  std::shared_ptr<grpc::ServerCredentials> creds,
129  int* selected_port = nullptr);
130 
161  std::unique_ptr<grpc::ServerCompletionQueue> AddCompletionQueue(
162  bool is_frequently_polled = true);
163 
165  // Less commonly used RegisterService variants
166 
171  ServerBuilder& RegisterService(const std::string& host,
172  grpc::Service* service);
173 
179  grpc::AsyncGenericService* service);
180 
182  // Fine control knobs
183 
186  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
187  max_receive_message_size_ = max_receive_message_size;
188  return *this;
189  }
190 
193  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
194  max_send_message_size_ = max_send_message_size;
195  return *this;
196  }
197 
199  ServerBuilder& SetMaxMessageSize(int max_message_size) {
200  return SetMaxReceiveMessageSize(max_message_size);
201  }
202 
209  grpc_compression_algorithm algorithm, bool enabled);
210 
214 
220 
222  ServerBuilder& SetResourceQuota(const grpc::ResourceQuota& resource_quota);
223 
224  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
225 
232  };
233 
236 
239  template <class T>
240  ServerBuilder& AddChannelArgument(const std::string& arg, const T& value) {
241  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
242  }
243 
245  static void InternalAddPluginFactory(
246  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
247 
252 
257  public:
258  explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
259 
261  std::vector<std::unique_ptr<
263  interceptor_creators) {
264  builder_->interceptor_creators_ = std::move(interceptor_creators);
265  }
266 
268  FROM_FD = 0 // in the form of a file descriptor
269  };
270 
275  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
277  std::shared_ptr<ServerCredentials> creds);
278 
282  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
283  provider);
284 
285  private:
286  ServerBuilder* builder_;
287  };
288 
292  std::unique_ptr<grpc::ContextAllocator> context_allocator);
293 
300 
305 
306  protected:
308  struct Port {
309  std::string addr;
310  std::shared_ptr<ServerCredentials> creds;
312  };
313 
315  typedef std::unique_ptr<std::string> HostString;
316  struct NamedService {
317  explicit NamedService(grpc::Service* s) : service(s) {}
318  NamedService(const std::string& h, grpc::Service* s)
319  : host(new std::string(h)), service(s) {}
322  };
323 
325  std::vector<Port> ports() { return ports_; }
326 
328  std::vector<NamedService*> services() {
329  std::vector<NamedService*> service_refs;
330  for (auto& ptr : services_) {
331  service_refs.push_back(ptr.get());
332  }
333  return service_refs;
334  }
335 
337  std::vector<grpc::ServerBuilderOption*> options() {
338  std::vector<grpc::ServerBuilderOption*> option_refs;
339  for (auto& ptr : options_) {
340  option_refs.push_back(ptr.get());
341  }
342  return option_refs;
343  }
344 
346  void set_fetcher(grpc_server_config_fetcher* server_config_fetcher) {
347  server_config_fetcher_ = server_config_fetcher;
348  }
349 
352 
353  private:
354  friend class ::grpc::testing::ServerBuilderPluginTest;
355 
356  struct SyncServerSettings {
357  SyncServerSettings()
358  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
359 
361  int num_cqs;
362 
365  int min_pollers;
366 
369  int max_pollers;
370 
372  int cq_timeout_msec;
373  };
374 
375  int max_receive_message_size_;
376  int max_send_message_size_;
377  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
378  std::vector<std::unique_ptr<NamedService>> services_;
379  std::vector<Port> ports_;
380 
381  SyncServerSettings sync_server_settings_;
382 
384  std::vector<grpc::ServerCompletionQueue*> cqs_;
385 
386  std::shared_ptr<grpc::ServerCredentials> creds_;
387  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
388  grpc_resource_quota* resource_quota_;
389  grpc::AsyncGenericService* generic_service_{nullptr};
390  std::unique_ptr<ContextAllocator> context_allocator_;
391  grpc::CallbackGenericService* callback_generic_service_{nullptr};
392 
393  struct {
394  bool is_set;
396  } maybe_default_compression_level_;
397  struct {
398  bool is_set;
400  } maybe_default_compression_algorithm_;
401  uint32_t enabled_compression_algorithms_bitset_;
402  std::vector<
403  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
404  interceptor_creators_;
405  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
406  acceptors_;
407  grpc_server_config_fetcher* server_config_fetcher_ = nullptr;
408  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
409  authorization_provider_;
410 };
411 
412 } // namespace grpc
413 
414 #endif // GRPCPP_SERVER_BUILDER_H
Definition: async_generic_service.h:68
A sequence of bytes.
Definition: byte_buffer.h:60
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: async_generic_service.h:102
Options for channel creation.
Definition: channel_arguments.h:39
ResourceQuota represents a bound on memory and thread usage by the gRPC library.
Definition: resource_quota.h:34
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder.h:256
std::unique_ptr< grpc::experimental::ExternalConnectionAcceptor > AddExternalConnectionAcceptor(ExternalConnectionType type, std::shared_ptr< ServerCredentials > creds)
Register an acceptor to handle the externally accepted connection in grpc server.
Definition: server_builder.cc:125
void SetInterceptorCreators(std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder.h:260
experimental_type(ServerBuilder *builder)
Definition: server_builder.h:258
ExternalConnectionType
Definition: server_builder.h:267
void SetAuthorizationPolicyProvider(std::shared_ptr< experimental::AuthorizationPolicyProviderInterface > provider)
Sets server authorization policy provider in GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER channel argument.
Definition: server_builder.cc:137
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:85
ServerBuilder & SetContextAllocator(std::unique_ptr< grpc::ContextAllocator > context_allocator)
Set the allocator for creating and releasing callback server context.
Definition: server_builder.cc:118
ServerBuilder & SetSyncServerOption(SyncServerOption option, int value)
Only useful if this is a Synchronous server.
Definition: server_builder.cc:149
ServerBuilder & SetCompressionAlgorithmSupportStatus(grpc_compression_algorithm algorithm, bool enabled)
Set the support status for compression algorithms.
Definition: server_builder.cc:168
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:199
ServerBuilder & AddChannelArgument(const std::string &arg, const T &value)
Add a channel argument (an escape hatch to tuning core library parameters directly)
Definition: server_builder.h:240
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Return a running server which is ready for processing calls.
Definition: server_builder.cc:255
ServerBuilder()
Definition: server_builder.cc:46
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder.h:304
grpc_compression_algorithm algorithm
Definition: server_builder.h:399
ServerBuilder & RegisterCallbackGenericService(grpc::CallbackGenericService *service)
Register a generic service that uses the callback API.
Definition: server_builder.cc:105
ServerBuilder & RegisterService(grpc::Service *service)
Register a service.
Definition: server_builder.cc:81
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:328
ServerBuilder & AddListeningPort(const std::string &addr_uri, std::shared_ptr< grpc::ServerCredentials > creds, int *selected_port=nullptr)
Enlists an endpoint addr (port with an optional IP address) to bind the grpc::Server object to be cre...
Definition: server_builder.cc:202
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:325
std::vector< grpc::ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:337
ServerBuilder & SetDefaultCompressionAlgorithm(grpc_compression_algorithm algorithm)
The default compression algorithm to use for all channel calls in the absence of a call-specific leve...
Definition: server_builder.cc:185
ServerBuilder & EnableWorkaround(grpc_workaround_list id)
Enable a server workaround.
Definition: server_builder.cc:437
std::unique_ptr< std::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:315
virtual ~ServerBuilder()
Definition: server_builder.cc:65
virtual ChannelArguments BuildChannelArgs()
Experimental API, subject to change.
Definition: server_builder.cc:217
static void InternalAddPluginFactory(std::unique_ptr< grpc::ServerBuilderPlugin >(*CreatePlugin)())
For internal use only: Register a ServerBuilderPlugin factory function.
Definition: server_builder.cc:431
grpc_compression_level level
Definition: server_builder.h:395
bool is_set
Definition: server_builder.h:394
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:186
std::unique_ptr< grpc::ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
Definition: server_builder.cc:71
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:193
ServerBuilder & SetResourceQuota(const grpc::ResourceQuota &resource_quota)
Set the attached buffer pool for this server.
Definition: server_builder.cc:192
ServerBuilder & RegisterAsyncGenericService(grpc::AsyncGenericService *service)
Register a generic service.
Definition: server_builder.cc:92
ServerBuilder & SetOption(std::unique_ptr< grpc::ServerBuilderOption > option)
Definition: server_builder.cc:143
ServerBuilder & SetDefaultCompressionLevel(grpc_compression_level level)
The default compression level to use for all channel calls in the absence of a call-specific level.
Definition: server_builder.cc:178
void set_fetcher(grpc_server_config_fetcher *server_config_fetcher)
Experimental API, subject to change.
Definition: server_builder.h:346
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:227
@ MAX_POLLERS
Maximum number of polling threads.
Definition: server_builder.h:230
@ MIN_POLLERS
Minimum number of polling threads.
Definition: server_builder.h:229
@ NUM_CQS
Number of completion queues.
Definition: server_builder.h:228
@ CQ_TIMEOUT_MSEC
Completion queue timeout in milliseconds.
Definition: server_builder.h:231
Desriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
Definition: server_builder.h:66
virtual ~ExternalConnectionAcceptor()
Definition: server_builder.h:73
virtual void HandleNewConnection(NewConnectionParameters *p)=0
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:71
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:57
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const std::string &name, const std::string &value)
Definition: channel_argument_option.cc:23
Definition: async_unary_call.h:398
Definition: server_builder.h:316
HostString host
Definition: server_builder.h:320
NamedService(const std::string &h, grpc::Service *s)
Definition: server_builder.h:318
NamedService(grpc::Service *s)
Definition: server_builder.h:317
grpc::Service * service
Definition: server_builder.h:321
Experimental, to be deprecated.
Definition: server_builder.h:308
std::shared_ptr< ServerCredentials > creds
Definition: server_builder.h:310
int * selected_port
Definition: server_builder.h:311
std::string addr
Definition: server_builder.h:309
Definition: resource_quota.cc:127
Definition: server.h:460
grpc_workaround_list
Definition: workaround_list.h:26
DiscoveryMechanismType type
Definition: xds_cluster_resolver.cc:73