GRPC C++  1.85.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 <grpc/compression.h>
24 #include <grpc/passive_listener.h>
25 #include <grpc/support/cpu.h>
34 #include <grpcpp/server.h>
35 #include <grpcpp/support/config.h>
37 
38 #include <climits>
39 #include <map>
40 #include <memory>
41 #include <vector>
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 
228  std::shared_ptr<grpc_event_engine::experimental::EventEngine>
229  event_engine);
230 
231  ServerBuilder& SetOption(std::unique_ptr<grpc::ServerBuilderOption> option);
232 
239  };
240 
243 
246  template <class T>
247  ServerBuilder& AddChannelArgument(const std::string& arg, const T& value) {
248  return SetOption(grpc::MakeChannelArgumentOption(arg, value));
249  }
250 
252 
254  static void InternalAddPluginFactory(
255  std::unique_ptr<grpc::ServerBuilderPlugin> (*CreatePlugin)());
256 
261 
266  public:
267  explicit experimental_type(ServerBuilder* builder) : builder_(builder) {}
268 
270  std::vector<std::unique_ptr<
272  interceptor_creators) {
273  builder_->interceptor_creators_ = std::move(interceptor_creators);
274  }
275 
277  FROM_FD = 0 // in the form of a file descriptor
278  };
279 
284  std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor>
286  std::shared_ptr<ServerCredentials> creds);
287 
291  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
292  provider);
293 
302  experimental::ServerMetricRecorder* server_metric_recorder = nullptr);
303 
304  // Creates a passive listener for Server Endpoint injection.
313  std::shared_ptr<grpc::ServerCredentials> creds,
314  std::unique_ptr<grpc::experimental::PassiveListener>& passive_listener);
315 
316  private:
317  ServerBuilder* builder_;
318  };
319 
323  std::unique_ptr<grpc::ContextAllocator> context_allocator);
324 
331 
336 
337  protected:
339  struct Port {
340  std::string addr;
341  std::shared_ptr<ServerCredentials> creds;
343  };
344 
346  typedef std::unique_ptr<std::string> HostString;
347  struct NamedService {
348  explicit NamedService(grpc::Service* s) : service(s) {}
349  NamedService(const std::string& h, grpc::Service* s)
350  : host(new std::string(h)), service(s) {}
353  };
354 
356  std::vector<Port> ports() { return ports_; }
357 
359  std::vector<NamedService*> services() {
360  std::vector<NamedService*> service_refs;
361  service_refs.reserve(services_.size());
362  for (auto& ptr : services_) {
363  service_refs.push_back(ptr.get());
364  }
365  return service_refs;
366  }
367 
369  std::vector<grpc::ServerBuilderOption*> options() {
370  std::vector<grpc::ServerBuilderOption*> option_refs;
371  option_refs.reserve(options_.size());
372  for (auto& ptr : options_) {
373  option_refs.push_back(ptr.get());
374  }
375  return option_refs;
376  }
377 
380 
381  private:
383 
384  struct UnstartedPassiveListener {
385  std::weak_ptr<grpc_core::experimental::PassiveListenerImpl>
386  passive_listener;
387  std::shared_ptr<grpc::ServerCredentials> credentials;
388  UnstartedPassiveListener(
389  std::weak_ptr<grpc_core::experimental::PassiveListenerImpl> listener,
390  std::shared_ptr<grpc::ServerCredentials> creds)
391  : passive_listener(std::move(listener)),
392  credentials(std::move(creds)) {}
393  };
394 
395  struct SyncServerSettings {
396  SyncServerSettings()
397  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
398 
400  int num_cqs;
401 
404  int min_pollers;
405 
408  int max_pollers;
409 
411  int cq_timeout_msec;
412  };
413 
414  int max_receive_message_size_;
415  int max_send_message_size_;
416  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
417  std::vector<std::unique_ptr<NamedService>> services_;
418  std::vector<Port> ports_;
419  std::vector<UnstartedPassiveListener> unstarted_passive_listeners_;
420 
421  SyncServerSettings sync_server_settings_;
422 
424  std::vector<grpc::ServerCompletionQueue*> cqs_;
425 
426  std::shared_ptr<grpc::ServerCredentials> creds_;
427  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
428  grpc_resource_quota* resource_quota_;
429  grpc::AsyncGenericService* generic_service_{nullptr};
430  std::unique_ptr<ContextAllocator> context_allocator_;
431  grpc::CallbackGenericService* callback_generic_service_{nullptr};
432  std::shared_ptr<grpc_event_engine::experimental::EventEngine> event_engine_ =
433  nullptr;
434 
435  struct {
436  bool is_set;
438  } maybe_default_compression_level_;
439  struct {
440  bool is_set;
442  } maybe_default_compression_algorithm_;
443  uint32_t enabled_compression_algorithms_bitset_;
444  std::vector<
445  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
446  interceptor_creators_;
447  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
448  acceptors_;
449  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
450  authorization_provider_;
451  experimental::ServerMetricRecorder* server_metric_recorder_ = nullptr;
452 };
453 
454 } // namespace grpc
455 
456 #endif // GRPCPP_SERVER_BUILDER_H
grpc::ServerBuilder::level
grpc_compression_level level
Definition: server_builder.h:437
grpc::ServerBuilder::NamedService
Definition: server_builder.h:347
compression.h
grpc::ServerBuilder::BuildAndStart
virtual std::unique_ptr< grpc::Server > BuildAndStart()
Return a running server which is ready for processing calls.
grpc::ServerBuilder::SetEventEngine
ServerBuilder & SetEventEngine(std::shared_ptr< grpc_event_engine::experimental::EventEngine > event_engine)
grpc::ServerBuilder::algorithm
grpc_compression_algorithm algorithm
Definition: server_builder.h:441
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:399
grpc::ServerBuilder::ports
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:356
event_engine.h
grpc::ServerBuilder::NamedService::NamedService
NamedService(const std::string &h, grpc::Service *s)
Definition: server_builder.h:349
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:61
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:340
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:66
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:339
authorization_policy_provider.h
passive_listener.h
grpc::ServerBuilder::BuildChannelArgs
virtual ChannelArguments BuildChannelArgs()
Experimental API, subject to change.
grpc::ServerBuilder::Port::selected_port
int * selected_port
Definition: server_builder.h:342
grpc::ServerBuilder::ServerBuilderPluginTest
friend class grpc::testing::ServerBuilderPluginTest
Definition: server_builder.h:382
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:235
server_builder_plugin.h
grpc::ServerBuilder::experimental_type::ExternalConnectionType
ExternalConnectionType
Definition: server_builder.h:276
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:237
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:67
grpc::ServerBuilder::experimental_type::experimental_type
experimental_type(ServerBuilder *builder)
Definition: server_builder.h:267
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:346
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:265
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:238
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)
PassiveListener 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:234
grpc::ServerBuilder::services
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:359
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:45
server_credentials.h
grpc::ServerBuilder::NamedService::service
grpc::Service * service
Definition: server_builder.h:352
grpc::ServerBuilder::is_set
bool is_set
Definition: server_builder.h:436
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:341
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:351
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:73
std
Definition: async_unary_call.h:410
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:269
grpc::ServerBuilder::NamedService::NamedService
NamedService(grpc::Service *s)
Definition: server_builder.h:348
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:335
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:369
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:247
channel_argument_option.h
grpc::ServerBuilder::MIN_POLLERS
@ MIN_POLLERS
Minimum number of polling threads.
Definition: server_builder.h:236
grpc::ServerBuilder::SetMaxSendMessageSize
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:196
grpc::ServerBuilder::SetChildChannelArgs
ServerBuilder & SetChildChannelArgs(const ChannelArguments &args)
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