GRPC C++  1.66.0
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 
27 #include <grpc/compression.h>
29 #include <grpc/passive_listener.h>
30 #include <grpc/support/cpu.h>
39 #include <grpcpp/server.h>
40 #include <grpcpp/support/config.h>
42 
43 struct grpc_resource_quota;
44 
45 namespace grpc {
46 
47 class CompletionQueue;
48 class Server;
49 class ServerCompletionQueue;
50 class AsyncGenericService;
51 class ResourceQuota;
52 class ServerCredentials;
53 class Service;
54 namespace testing {
55 class ServerBuilderPluginTest;
56 } // namespace testing
57 
58 namespace internal {
59 class ExternalConnectionAcceptorImpl;
60 } // namespace internal
61 
62 class CallbackGenericService;
63 
64 namespace experimental {
65 // EXPERIMENTAL API:
66 // Interface for a grpc server to build transports with connections created out
67 // of band.
68 // See ServerBuilder's AddExternalConnectionAcceptor API.
70  public:
72  int listener_fd = -1;
73  int fd = -1;
74  ByteBuffer read_buffer; // data intended for the grpc server
75  };
77  // If called before grpc::Server is started or after it is shut down, the new
78  // connection will be closed.
79  virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
80 };
81 
82 } // namespace experimental
83 } // namespace grpc
84 
85 namespace grpc {
86 
89  public:
90  ServerBuilder();
91  virtual ~ServerBuilder();
92 
94  // Primary API's
95 
106  virtual std::unique_ptr<grpc::Server> BuildAndStart();
107 
113 
130  const std::string& addr_uri,
131  std::shared_ptr<grpc::ServerCredentials> creds,
132  int* selected_port = nullptr);
133 
164  std::unique_ptr<grpc::ServerCompletionQueue> AddCompletionQueue(
165  bool is_frequently_polled = true);
166 
168  // Less commonly used RegisterService variants
169 
174  ServerBuilder& RegisterService(const std::string& host,
175  grpc::Service* service);
176 
182  grpc::AsyncGenericService* service);
183 
185  // Fine control knobs
186 
189  ServerBuilder& SetMaxReceiveMessageSize(int max_receive_message_size) {
190  max_receive_message_size_ = max_receive_message_size;
191  return *this;
192  }
193 
196  ServerBuilder& SetMaxSendMessageSize(int max_send_message_size) {
197  max_send_message_size_ = max_send_message_size;
198  return *this;
199  }
200 
202  ServerBuilder& SetMaxMessageSize(int max_message_size) {
203  return SetMaxReceiveMessageSize(max_message_size);
204  }
205 
212  grpc_compression_algorithm algorithm, bool enabled);
213 
217 
223 
225  ServerBuilder& SetResourceQuota(const grpc::ResourceQuota& resource_quota);
226 
227  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
228 
235  };
236 
239 
242  template <class T>
243  ServerBuilder& AddChannelArgument(const std::string& arg, const T& value) {
244  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
245  }
246 
248  static void InternalAddPluginFactory(
249  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
250 
255 
260  public:
261  explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
262 
264  std::vector<std::unique_ptr<
266  interceptor_creators) {
267  builder_->interceptor_creators_ = std::move(interceptor_creators);
268  }
269 
271  FROM_FD = 0 // in the form of a file descriptor
272  };
273 
278  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
280  std::shared_ptr<ServerCredentials> creds);
281 
285  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
286  provider);
287 
296  experimental::ServerMetricRecorder* server_metric_recorder = nullptr);
297 
298  // Creates a passive listener for Server Endpoint injection.
307  std::shared_ptr<grpc::ServerCredentials> creds,
308  std::unique_ptr<grpc::experimental::PassiveListener>& passive_listener);
309 
310  private:
311  ServerBuilder* builder_;
312  };
313 
317  std::unique_ptr<grpc::ContextAllocator> context_allocator);
318 
325 
330 
331  protected:
333  struct Port {
334  std::string addr;
335  std::shared_ptr<ServerCredentials> creds;
337  };
338 
340  typedef std::unique_ptr<std::string> HostString;
341  struct NamedService {
342  explicit NamedService(grpc::Service* s) : service(s) {}
343  NamedService(const std::string& h, grpc::Service* s)
344  : host(new std::string(h)), service(s) {}
347  };
348 
350  std::vector<Port> ports() { return ports_; }
351 
353  std::vector<NamedService*> services() {
354  std::vector<NamedService*> service_refs;
355  service_refs.reserve(services_.size());
356  for (auto& ptr : services_) {
357  service_refs.push_back(ptr.get());
358  }
359  return service_refs;
360  }
361 
363  std::vector<grpc::ServerBuilderOption*> options() {
364  std::vector<grpc::ServerBuilderOption*> option_refs;
365  option_refs.reserve(options_.size());
366  for (auto& ptr : options_) {
367  option_refs.push_back(ptr.get());
368  }
369  return option_refs;
370  }
371 
373  void set_fetcher(grpc_server_config_fetcher* server_config_fetcher) {
374  server_config_fetcher_ = server_config_fetcher;
375  }
376 
379 
380  private:
382 
383  struct UnstartedPassiveListener {
384  std::weak_ptr<grpc_core::experimental::PassiveListenerImpl>
385  passive_listener;
386  std::shared_ptr<grpc::ServerCredentials> credentials;
387  UnstartedPassiveListener(
388  std::weak_ptr<grpc_core::experimental::PassiveListenerImpl> listener,
389  std::shared_ptr<grpc::ServerCredentials> creds)
390  : passive_listener(std::move(listener)),
391  credentials(std::move(creds)) {}
392  };
393 
394  struct SyncServerSettings {
395  SyncServerSettings()
396  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
397 
399  int num_cqs;
400 
403  int min_pollers;
404 
407  int max_pollers;
408 
410  int cq_timeout_msec;
411  };
412 
413  int max_receive_message_size_;
414  int max_send_message_size_;
415  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
416  std::vector<std::unique_ptr<NamedService>> services_;
417  std::vector<Port> ports_;
418  std::vector<UnstartedPassiveListener> unstarted_passive_listeners_;
419 
420  SyncServerSettings sync_server_settings_;
421 
423  std::vector<grpc::ServerCompletionQueue*> cqs_;
424 
425  std::shared_ptr<grpc::ServerCredentials> creds_;
426  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
427  grpc_resource_quota* resource_quota_;
428  grpc::AsyncGenericService* generic_service_{nullptr};
429  std::unique_ptr<ContextAllocator> context_allocator_;
430  grpc::CallbackGenericService* callback_generic_service_{nullptr};
431 
432  struct {
433  bool is_set;
435  } maybe_default_compression_level_;
436  struct {
437  bool is_set;
439  } maybe_default_compression_algorithm_;
440  uint32_t enabled_compression_algorithms_bitset_;
441  std::vector<
442  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
443  interceptor_creators_;
444  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
445  acceptors_;
446  grpc_server_config_fetcher* server_config_fetcher_ = nullptr;
447  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
448  authorization_provider_;
449  experimental::ServerMetricRecorder* server_metric_recorder_ = nullptr;
450 };
451 
452 } // namespace grpc
453 
454 #endif // GRPCPP_SERVER_BUILDER_H
grpc::ServerBuilder::level
grpc_compression_level level
Definition: server_builder.h:434
grpc::ServerBuilder::NamedService
Definition: server_builder.h:341
compression.h
grpc::ServerBuilder::BuildAndStart
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Return a running server which is ready for processing calls.
grpc::ServerBuilder::algorithm
grpc_compression_algorithm algorithm
Definition: server_builder.h:438
grpc::MakeChannelArgumentOption
std::unique_ptr< ServerBuilderOption > MakeChannelArgumentOption(const std::string &name, const std::string &value)
grpc::ServerBuilder::ServerBuilder
ServerBuilder()
grpc::experimental::ServerMetricRecorder
Records server wide metrics to be reported to the client.
Definition: server_metric_recorder.h:42
grpc::ServerBuilder::SetDefaultCompressionAlgorithm
ServerBuilder & SetDefaultCompressionAlgorithm(grpc_compression_algorithm algorithm)
The default compression algorithm to use for all channel calls in the absence of a call-specific leve...
grpc::ServerBuilder::RegisterCallbackGenericService
ServerBuilder & RegisterCallbackGenericService(grpc::CallbackGenericService *service)
Register a generic service that uses the callback API.
grpc::ServerBuilder::EnableWorkaround
ServerBuilder & EnableWorkaround(grpc_workaround_list id)
Enable a server workaround.
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:400
grpc::ServerBuilder::ports
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:350
event_engine.h
grpc::ServerBuilder::NamedService::NamedService
NamedService(const std::string &h, grpc::Service *s)
Definition: server_builder.h:343
workaround_list.h
grpc_compression_algorithm
grpc_compression_algorithm
The various compression algorithms supported by gRPC (not sorted by compression level)
Definition: compression_types.h:60
grpc::ServerBuilder::experimental_type::AddExternalConnectionAcceptor
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.
grpc::ServerBuilder::Port::addr
std::string addr
Definition: server_builder.h:334
grpc::ServerBuilder::set_fetcher
void set_fetcher(grpc_server_config_fetcher *server_config_fetcher)
Experimental API, subject to change.
Definition: server_builder.h:373
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:58
grpc::ServerBuilder::InternalAddPluginFactory
static void InternalAddPluginFactory(std::unique_ptr< grpc::ServerBuilderPlugin >(*CreatePlugin)())
For internal use only: Register a ServerBuilderPlugin factory function.
grpc::ServerBuilder::Port
Experimental, to be deprecated.
Definition: server_builder.h:333
authorization_policy_provider.h
passive_listener.h
grpc::ServerBuilder::BuildChannelArgs
virtual ChannelArguments BuildChannelArgs()
Experimental API, subject to change.
grpc_server_config_fetcher
struct grpc_server_config_fetcher grpc_server_config_fetcher
Definition: grpc.h:448
grpc::ServerBuilder::Port::selected_port
int * selected_port
Definition: server_builder.h:336
grpc::ServerBuilder::ServerBuilderPluginTest
friend class grpc::testing::ServerBuilderPluginTest
Definition: server_builder.h:381
grpc::ChannelArguments
Options for channel creation.
Definition: channel_arguments.h:39
grpc::ServerBuilder::NUM_CQS
@ NUM_CQS
Number of completion queues.
Definition: server_builder.h:231
server_builder_plugin.h
grpc::ServerBuilder::experimental_type::ExternalConnectionType
ExternalConnectionType
Definition: server_builder.h:270
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::read_buffer
ByteBuffer read_buffer
Definition: server_builder.h:74
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters
Definition: server_builder.h:71
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::fd
int fd
Definition: server_builder.h:73
grpc::ServerBuilder::MAX_POLLERS
@ MAX_POLLERS
Maximum number of polling threads.
Definition: server_builder.h:233
grpc::ServerBuilder::SetDefaultCompressionLevel
ServerBuilder & SetDefaultCompressionLevel(grpc_compression_level level)
The default compression level to use for all channel calls in the absence of a call-specific level.
grpc::ServerBuilder::experimental_type::EnableCallMetricRecording
void EnableCallMetricRecording(experimental::ServerMetricRecorder *server_metric_recorder=nullptr)
Enables per-call load reporting.
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:60
grpc::ServerBuilder::experimental_type::experimental_type
experimental_type(ServerBuilder *builder)
Definition: server_builder.h:261
grpc::ResourceQuota
ResourceQuota represents a bound on memory and thread usage by the gRPC library.
Definition: resource_quota.h:34
grpc::ServerBuilder::HostString
std::unique_ptr< std::string > HostString
Experimental, to be deprecated.
Definition: server_builder.h:340
grpc::ServerBuilder::RegisterAsyncGenericService
ServerBuilder & RegisterAsyncGenericService(grpc::AsyncGenericService *service)
Register a generic service.
server_builder_option.h
grpc::ServerBuilder::experimental_type
NOTE: class experimental_type is not part of the public API of this class.
Definition: server_builder.h:259
grpc::AsyncGenericService
Definition: async_generic_service.h:66
grpc::experimental::ExternalConnectionAcceptor
Definition: server_builder.h:69
cpu.h
grpc::ServerBuilder::CQ_TIMEOUT_MSEC
@ CQ_TIMEOUT_MSEC
Completion queue timeout in milliseconds.
Definition: server_builder.h:234
grpc::ServerBuilder::SetMaxMessageSize
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:202
grpc::ServerBuilder::SetSyncServerOption
ServerBuilder & SetSyncServerOption(SyncServerOption option, int value)
Only useful if this is a Synchronous server.
grpc::ServerBuilder::SetContextAllocator
ServerBuilder & SetContextAllocator(std::unique_ptr< grpc::ContextAllocator > context_allocator)
Set the allocator for creating and releasing callback server context.
server_interceptor.h
grpc::ServerBuilder::experimental_type::AddPassiveListener
ServerBuilder & AddPassiveListener(std::shared_ptr< grpc::ServerCredentials > creds, std::unique_ptr< grpc::experimental::PassiveListener > &passive_listener)
PasiveListener lets applications provide pre-established connections to gRPC Servers.
grpc::ServerBuilder::SetCompressionAlgorithmSupportStatus
ServerBuilder & SetCompressionAlgorithmSupportStatus(grpc_compression_algorithm algorithm, bool enabled)
Set the support status for compression algorithms.
grpc::ServerBuilder::SyncServerOption
SyncServerOption
Options for synchronous servers.
Definition: server_builder.h:230
grpc::ServerBuilder::services
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:353
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:46
server_credentials.h
grpc::ServerBuilder::NamedService::service
grpc::Service * service
Definition: server_builder.h:346
grpc::ServerBuilder::is_set
bool is_set
Definition: server_builder.h:433
grpc::ServerBuilder::experimental_type::ExternalConnectionType::FROM_FD
@ FROM_FD
grpc::ServerBuilder::AddCompletionQueue
std::unique_ptr< grpc::ServerCompletionQueue > AddCompletionQueue(bool is_frequently_polled=true)
Add a completion queue for handling asynchronous services.
config.h
grpc::ServerBuilder::Port::creds
std::shared_ptr< ServerCredentials > creds
Definition: server_builder.h:335
grpc::ServerBuilder::experimental_type::SetAuthorizationPolicyProvider
void SetAuthorizationPolicyProvider(std::shared_ptr< experimental::AuthorizationPolicyProviderInterface > provider)
Sets server authorization policy provider in GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER channel argument.
grpc::ServerBuilder::NamedService::host
HostString host
Definition: server_builder.h:345
grpc_workaround_list
grpc_workaround_list
Definition: workaround_list.h:26
grpc_compression_level
grpc_compression_level
Compression levels allow a party with knowledge of its peer's accepted encodings to request compressi...
Definition: compression_types.h:72
std
Definition: async_unary_call.h:407
grpc::experimental::ExternalConnectionAcceptor::~ExternalConnectionAcceptor
virtual ~ExternalConnectionAcceptor()
Definition: server_builder.h:76
passive_listener.h
grpc::ServerBuilder::SetResourceQuota
ServerBuilder & SetResourceQuota(const grpc::ResourceQuota &resource_quota)
Set the attached buffer pool for this server.
grpc::ServerBuilder::experimental_type::SetInterceptorCreators
void SetInterceptorCreators(std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface >> interceptor_creators)
Definition: server_builder.h:263
grpc::ServerBuilder::NamedService::NamedService
NamedService(grpc::Service *s)
Definition: server_builder.h:342
server.h
grpc::ServerBuilder::AddListeningPort
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...
grpc::experimental::ExternalConnectionAcceptor::HandleNewConnection
virtual void HandleNewConnection(NewConnectionParameters *p)=0
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::listener_fd
int listener_fd
Definition: server_builder.h:72
grpc::ServerBuilder::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder.h:329
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:88
grpc::ServerBuilder::SetOption
ServerBuilder & SetOption(std::unique_ptr< grpc::ServerBuilderOption > option)
grpc::ServerBuilder::SetMaxReceiveMessageSize
ServerBuilder & SetMaxReceiveMessageSize(int max_receive_message_size)
Set max receive message size in bytes.
Definition: server_builder.h:189
grpc::ServerBuilder::~ServerBuilder
virtual ~ServerBuilder()
grpc::ServerBuilder::options
std::vector< grpc::ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:363
grpc::ServerBuilder::AddChannelArgument
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:243
channel_argument_option.h
grpc::ServerBuilder::MIN_POLLERS
@ MIN_POLLERS
Minimum number of polling threads.
Definition: server_builder.h:232
grpc::ServerBuilder::SetMaxSendMessageSize
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:196
grpc::ServerBuilder::RegisterService
ServerBuilder & RegisterService(grpc::Service *service)
Register a service.
grpc::CallbackGenericService
CallbackGenericService is the base class for generic services implemented using the callback API and ...
Definition: callback_generic_service.h:51
port_platform.h