GRPC C++  1.62.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 
23 
24 #include <climits>
25 #include <map>
26 #include <memory>
27 #include <vector>
28 
29 #include <grpc/compression.h>
30 #include <grpc/support/cpu.h>
36 #include <grpcpp/server.h>
37 #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.
76  virtual void HandleNewConnection(NewConnectionParameters* p) = 0;
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 
293  experimental::ServerMetricRecorder* server_metric_recorder = nullptr);
294 
295  private:
296  ServerBuilder* builder_;
297  };
298 
302  std::unique_ptr<grpc::ContextAllocator> context_allocator);
303 
310 
315 
316  protected:
318  struct Port {
319  std::string addr;
320  std::shared_ptr<ServerCredentials> creds;
322  };
323 
325  typedef std::unique_ptr<std::string> HostString;
326  struct NamedService {
327  explicit NamedService(grpc::Service* s) : service(s) {}
328  NamedService(const std::string& h, grpc::Service* s)
329  : host(new std::string(h)), service(s) {}
332  };
333 
335  std::vector<Port> ports() { return ports_; }
336 
338  std::vector<NamedService*> services() {
339  std::vector<NamedService*> service_refs;
340  service_refs.reserve(services_.size());
341  for (auto& ptr : services_) {
342  service_refs.push_back(ptr.get());
343  }
344  return service_refs;
345  }
346 
348  std::vector<grpc::ServerBuilderOption*> options() {
349  std::vector<grpc::ServerBuilderOption*> option_refs;
350  option_refs.reserve(options_.size());
351  for (auto& ptr : options_) {
352  option_refs.push_back(ptr.get());
353  }
354  return option_refs;
355  }
356 
358  void set_fetcher(grpc_server_config_fetcher* server_config_fetcher) {
359  server_config_fetcher_ = server_config_fetcher;
360  }
361 
364 
365  private:
367 
368  struct SyncServerSettings {
369  SyncServerSettings()
370  : num_cqs(1), min_pollers(1), max_pollers(2), cq_timeout_msec(10000) {}
371 
373  int num_cqs;
374 
377  int min_pollers;
378 
381  int max_pollers;
382 
384  int cq_timeout_msec;
385  };
386 
387  int max_receive_message_size_;
388  int max_send_message_size_;
389  std::vector<std::unique_ptr<grpc::ServerBuilderOption>> options_;
390  std::vector<std::unique_ptr<NamedService>> services_;
391  std::vector<Port> ports_;
392 
393  SyncServerSettings sync_server_settings_;
394 
396  std::vector<grpc::ServerCompletionQueue*> cqs_;
397 
398  std::shared_ptr<grpc::ServerCredentials> creds_;
399  std::vector<std::unique_ptr<grpc::ServerBuilderPlugin>> plugins_;
400  grpc_resource_quota* resource_quota_;
401  grpc::AsyncGenericService* generic_service_{nullptr};
402  std::unique_ptr<ContextAllocator> context_allocator_;
403  grpc::CallbackGenericService* callback_generic_service_{nullptr};
404 
405  struct {
406  bool is_set;
408  } maybe_default_compression_level_;
409  struct {
410  bool is_set;
412  } maybe_default_compression_algorithm_;
413  uint32_t enabled_compression_algorithms_bitset_;
414  std::vector<
415  std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface>>
416  interceptor_creators_;
417  std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl>>
418  acceptors_;
419  grpc_server_config_fetcher* server_config_fetcher_ = nullptr;
420  std::shared_ptr<experimental::AuthorizationPolicyProviderInterface>
421  authorization_provider_;
422  experimental::ServerMetricRecorder* server_metric_recorder_ = nullptr;
423 };
424 
425 } // namespace grpc
426 
427 #endif // GRPCPP_SERVER_BUILDER_H
grpc::ServerBuilder::level
grpc_compression_level level
Definition: server_builder.h:407
grpc::ServerBuilder::NamedService
Definition: server_builder.h:326
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:411
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:401
grpc::ServerBuilder::ports
std::vector< Port > ports()
Experimental, to be deprecated.
Definition: server_builder.h:335
grpc::ServerBuilder::NamedService::NamedService
NamedService(const std::string &h, grpc::Service *s)
Definition: server_builder.h:328
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:319
grpc::ServerBuilder::set_fetcher
void set_fetcher(grpc_server_config_fetcher *server_config_fetcher)
Experimental API, subject to change.
Definition: server_builder.h:358
grpc::Service
Descriptor of an RPC service and its various RPC methods.
Definition: service_type.h:56
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:318
authorization_policy_provider.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:321
grpc::ServerBuilder::ServerBuilderPluginTest
friend class grpc::testing::ServerBuilderPluginTest
Definition: server_builder.h:366
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:228
server_builder_plugin.h
grpc::ServerBuilder::experimental_type::ExternalConnectionType
ExternalConnectionType
Definition: server_builder.h:267
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::read_buffer
ByteBuffer read_buffer
Definition: server_builder.h:71
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters
Definition: server_builder.h:68
grpc::experimental::ExternalConnectionAcceptor::NewConnectionParameters::fd
int fd
Definition: server_builder.h:70
grpc::ServerBuilder::MAX_POLLERS
@ MAX_POLLERS
Maximum number of polling threads.
Definition: server_builder.h:230
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:258
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:325
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:256
grpc::AsyncGenericService
Definition: async_generic_service.h:68
grpc::experimental::ExternalConnectionAcceptor
Definition: server_builder.h:66
cpu.h
grpc::ServerBuilder::CQ_TIMEOUT_MSEC
@ CQ_TIMEOUT_MSEC
Completion queue timeout in milliseconds.
Definition: server_builder.h:231
grpc::ServerBuilder::SetMaxMessageSize
ServerBuilder & SetMaxMessageSize(int max_message_size)
Definition: server_builder.h:199
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::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:227
grpc::ServerBuilder::services
std::vector< NamedService * > services()
Experimental, to be deprecated.
Definition: server_builder.h:338
grpc::experimental::ServerInterceptorFactoryInterface
Definition: server_interceptor.h:44
grpc::ServerBuilder::NamedService::service
grpc::Service * service
Definition: server_builder.h:331
grpc::ServerBuilder::is_set
bool is_set
Definition: server_builder.h:406
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:320
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:330
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:405
grpc::experimental::ExternalConnectionAcceptor::~ExternalConnectionAcceptor
virtual ~ExternalConnectionAcceptor()
Definition: server_builder.h:73
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:260
grpc::ServerBuilder::NamedService::NamedService
NamedService(grpc::Service *s)
Definition: server_builder.h:327
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:69
grpc::ServerBuilder::experimental
experimental_type experimental()
NOTE: The function experimental() is not stable public API.
Definition: server_builder.h:314
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: server_builder.h:85
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:186
grpc::ServerBuilder::~ServerBuilder
virtual ~ServerBuilder()
grpc::ServerBuilder::options
std::vector< grpc::ServerBuilderOption * > options()
Experimental, to be deprecated.
Definition: server_builder.h:348
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:240
channel_argument_option.h
grpc::ServerBuilder::MIN_POLLERS
@ MIN_POLLERS
Minimum number of polling threads.
Definition: server_builder.h:229
grpc::ServerBuilder::SetMaxSendMessageSize
ServerBuilder & SetMaxSendMessageSize(int max_send_message_size)
Set max send message size in bytes.
Definition: server_builder.h:193
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: async_generic_service.h:102
port_platform.h