Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Private Attributes | Friends | List of all members
grpc::ServerBuilder Class Reference

A builder class for the creation and startup of grpc::Server instances. More...

#include <server_builder.h>

Inheritance diagram for grpc::ServerBuilder:
Inheritance graph
[legend]

Classes

class  experimental_type
 
struct  NamedService
 
struct  Port
 Experimental, to be deprecated. More...
 
struct  SyncServerSettings
 

Public Types

enum  SyncServerOption { NUM_CQS, MIN_POLLERS, MAX_POLLERS, CQ_TIMEOUT_MSEC }
 Options for synchronous servers. More...
 

Public Member Functions

template<class T >
ServerBuilderAddChannelArgument (const std::string &arg, const T &value)
 
std::unique_ptr< grpc::ServerCompletionQueueAddCompletionQueue (bool is_frequently_polled=true)
 
ServerBuilderAddListeningPort (const std::string &addr_uri, std::shared_ptr< grpc::ServerCredentials > creds, int *selected_port=nullptr)
 
virtual std::unique_ptr< grpc::ServerBuildAndStart ()
 
ServerBuilderEnableWorkaround (grpc_workaround_list id)
 
experimental_type experimental ()
 
ServerBuilderRegisterAsyncGenericService (grpc::AsyncGenericService *service)
 
ServerBuilderRegisterCallbackGenericService (grpc::CallbackGenericService *service)
 
ServerBuilderRegisterService (const std::string &host, grpc::Service *service)
 
ServerBuilderRegisterService (grpc::Service *service)
 
 ServerBuilder ()
 
ServerBuilderSetCompressionAlgorithmSupportStatus (grpc_compression_algorithm algorithm, bool enabled)
 
ServerBuilderSetContextAllocator (std::unique_ptr< grpc::ContextAllocator > context_allocator)
 
ServerBuilderSetDefaultCompressionAlgorithm (grpc_compression_algorithm algorithm)
 
ServerBuilderSetDefaultCompressionLevel (grpc_compression_level level)
 
ServerBuilderSetMaxMessageSize (int max_message_size)
 
ServerBuilderSetMaxReceiveMessageSize (int max_receive_message_size)
 
ServerBuilderSetMaxSendMessageSize (int max_send_message_size)
 
ServerBuilderSetOption (std::unique_ptr< grpc::ServerBuilderOption > option)
 
ServerBuilderSetResourceQuota (const grpc::ResourceQuota &resource_quota)
 Set the attached buffer pool for this server. More...
 
ServerBuilderSetSyncServerOption (SyncServerOption option, int value)
 Only useful if this is a Synchronous server. More...
 
virtual ~ServerBuilder ()
 

Static Public Member Functions

static void InternalAddPluginFactory (std::unique_ptr< grpc::ServerBuilderPlugin >(*CreatePlugin)())
 For internal use only: Register a ServerBuilderPlugin factory function. More...
 

Protected Types

typedef std::unique_ptr< std::string > HostString
 Experimental, to be deprecated. More...
 

Protected Member Functions

virtual ChannelArguments BuildChannelArgs ()
 Experimental API, subject to change. More...
 
std::vector< grpc::ServerBuilderOption * > options ()
 Experimental, to be deprecated. More...
 
std::vector< Portports ()
 Experimental, to be deprecated. More...
 
std::vector< NamedService * > services ()
 Experimental, to be deprecated. More...
 
void set_fetcher (grpc_server_config_fetcher *server_config_fetcher)
 Experimental API, subject to change. More...
 

Private Attributes

std::vector< std::shared_ptr< grpc::internal::ExternalConnectionAcceptorImpl > > acceptors_
 
std::shared_ptr< experimental::AuthorizationPolicyProviderInterfaceauthorization_provider_
 
grpc::CallbackGenericServicecallback_generic_service_ {nullptr}
 
std::unique_ptr< ContextAllocatorcontext_allocator_
 
std::vector< grpc::ServerCompletionQueue * > cqs_
 List of completion queues added via AddCompletionQueue method. More...
 
std::shared_ptr< grpc::ServerCredentialscreds_
 
uint32_t enabled_compression_algorithms_bitset_
 
grpc::AsyncGenericServicegeneric_service_ {nullptr}
 
std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface > > interceptor_creators_
 
std::vector< std::unique_ptr< grpc::experimental::ServerInterceptorFactoryInterface > > internal_interceptor_creators_
 
int max_receive_message_size_
 
int max_send_message_size_
 
struct {
   grpc_compression_algorithm   algorithm
 
   bool   is_set
 
maybe_default_compression_algorithm_
 
struct {
   bool   is_set
 
   grpc_compression_level   level
 
maybe_default_compression_level_
 
std::vector< std::unique_ptr< grpc::ServerBuilderOption > > options_
 
std::vector< std::unique_ptr< grpc::ServerBuilderPlugin > > plugins_
 
std::vector< Portports_
 
grpc_resource_quotaresource_quota_
 
grpc_server_config_fetcherserver_config_fetcher_ = nullptr
 
std::vector< std::unique_ptr< NamedService > > services_
 
SyncServerSettings sync_server_settings_
 

Friends

class grpc::experimental::OrcaServerInterceptorFactory
 
class grpc::testing::ServerBuilderPluginTest
 

Detailed Description

A builder class for the creation and startup of grpc::Server instances.

Definition at line 86 of file grpcpp/server_builder.h.

Member Typedef Documentation

◆ HostString

typedef std::unique_ptr<std::string> grpc::ServerBuilder::HostString
protected

Experimental, to be deprecated.

Definition at line 316 of file grpcpp/server_builder.h.

Member Enumeration Documentation

◆ SyncServerOption

Options for synchronous servers.

Enumerator
NUM_CQS 

Number of completion queues.

MIN_POLLERS 

Minimum number of polling threads.

MAX_POLLERS 

Maximum number of polling threads.

CQ_TIMEOUT_MSEC 

Completion queue timeout in milliseconds.

Definition at line 228 of file grpcpp/server_builder.h.

Constructor & Destructor Documentation

◆ ServerBuilder()

grpc::ServerBuilder::ServerBuilder ( )

Definition at line 66 of file server_builder.cc.

◆ ~ServerBuilder()

grpc::ServerBuilder::~ServerBuilder ( )
virtual

Definition at line 85 of file server_builder.cc.

Member Function Documentation

◆ AddChannelArgument()

template<class T >
ServerBuilder& grpc::ServerBuilder::AddChannelArgument ( const std::string &  arg,
const T value 
)
inline

Add a channel argument (an escape hatch to tuning core library parameters directly)

Definition at line 241 of file grpcpp/server_builder.h.

◆ AddCompletionQueue()

std::unique_ptr< grpc::ServerCompletionQueue > grpc::ServerBuilder::AddCompletionQueue ( bool  is_frequently_polled = true)

Add a completion queue for handling asynchronous services.

Best performance is typically obtained by using one thread per polling completion queue.

Caller is required to shutdown the server prior to shutting down the returned completion queue. Caller is also required to drain the completion queue after shutting it down. A typical usage scenario:

// While building the server: ServerBuilder builder; ... cq_ = builder.AddCompletionQueue(); server_ = builder.BuildAndStart();

// While shutting down the server; server_->Shutdown(); cq_->Shutdown(); // Always after the associated server's Shutdown()! // Drain the cq_ that was created void* ignored_tag; bool ignored_ok; while (cq_->Next(&ignored_tag, &ignored_ok)) { }

Parameters
is_frequently_polledThis is an optional parameter to inform gRPC library about whether this completion queue would be frequently polled (i.e. by calling Next() or AsyncNext()). The default value is 'true' and is the recommended setting. Setting this to 'false' (i.e. not polling the completion queue frequently) will have a significantly negative performance impact and hence should not be used in production use cases.

Definition at line 91 of file server_builder.cc.

◆ AddListeningPort()

ServerBuilder & grpc::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 created to.

It can be invoked multiple times.

Parameters
addr_uriThe address to try to bind to the server in URI form. If the scheme name is omitted, "dns:///" is assumed. To bind to any address, please use IPv6 any, i.e., [::]:<port>, which also accepts IPv4 connections. Valid values include dns:///localhost:1234, 192.168.1.1:31416, dns:///[::1]:27182, etc.
credsThe credentials associated with the server.
[out]selected_portIf not nullptr, gets populated with the port number bound to the grpc::Server for the corresponding endpoint after it is successfully bound by BuildAndStart(), 0 otherwise. AddListeningPort does not modify this pointer.

Definition at line 222 of file server_builder.cc.

◆ BuildAndStart()

std::unique_ptr< grpc::Server > grpc::ServerBuilder::BuildAndStart ( )
virtual

Return a running server which is ready for processing calls. Before calling, one typically needs to ensure that:

  1. a service is registered - so that the server knows what to serve (via RegisterService, or RegisterAsyncGenericService)
  2. a listening port has been added - so the server knows where to receive traffic (via AddListeningPort)
  3. [for async api only] completion queues have been added via AddCompletionQueue

Will return a nullptr on errors.

Definition at line 275 of file server_builder.cc.

◆ BuildChannelArgs()

ChannelArguments grpc::ServerBuilder::BuildChannelArgs ( )
protectedvirtual

Experimental API, subject to change.

Reimplemented in grpc::XdsServerBuilder.

Definition at line 237 of file server_builder.cc.

◆ EnableWorkaround()

ServerBuilder & grpc::ServerBuilder::EnableWorkaround ( grpc_workaround_list  id)

Enable a server workaround. Do not use unless you know what the workaround does. For explanation and detailed descriptions of workarounds, see doc/workarounds.md.

Definition at line 464 of file server_builder.cc.

◆ experimental()

experimental_type grpc::ServerBuilder::experimental ( )
inline

NOTE: The function experimental() is not stable public API. It is a view to the experimental components of this class. It may be changed or removed at any time.

Definition at line 305 of file grpcpp/server_builder.h.

◆ InternalAddPluginFactory()

void grpc::ServerBuilder::InternalAddPluginFactory ( std::unique_ptr< grpc::ServerBuilderPlugin >(*)()  CreatePlugin)
static

For internal use only: Register a ServerBuilderPlugin factory function.

Definition at line 458 of file server_builder.cc.

◆ options()

std::vector<grpc::ServerBuilderOption*> grpc::ServerBuilder::options ( )
inlineprotected

Experimental, to be deprecated.

Definition at line 338 of file grpcpp/server_builder.h.

◆ ports()

std::vector<Port> grpc::ServerBuilder::ports ( )
inlineprotected

Experimental, to be deprecated.

Definition at line 326 of file grpcpp/server_builder.h.

◆ RegisterAsyncGenericService()

ServerBuilder & grpc::ServerBuilder::RegisterAsyncGenericService ( grpc::AsyncGenericService service)

Register a generic service. Matches requests with any :authority This is mostly useful for writing generic gRPC Proxies where the exact serialization format is unknown

Definition at line 112 of file server_builder.cc.

◆ RegisterCallbackGenericService()

ServerBuilder & grpc::ServerBuilder::RegisterCallbackGenericService ( grpc::CallbackGenericService service)

Register a generic service that uses the callback API. Matches requests with any :authority This is mostly useful for writing generic gRPC Proxies where the exact serialization format is unknown

Definition at line 125 of file server_builder.cc.

◆ RegisterService() [1/2]

ServerBuilder & grpc::ServerBuilder::RegisterService ( const std::string &  host,
grpc::Service service 
)

Register a service. This call does not take ownership of the service. The service must exist for the lifetime of the Server instance returned by BuildAndStart(). Only matches requests with :authority host

Definition at line 106 of file server_builder.cc.

◆ RegisterService() [2/2]

ServerBuilder & grpc::ServerBuilder::RegisterService ( grpc::Service service)

Register a service. This call does not take ownership of the service. The service must exist for the lifetime of the Server instance returned by BuildAndStart(). Matches requests with any :authority

Definition at line 101 of file server_builder.cc.

◆ services()

std::vector<NamedService*> grpc::ServerBuilder::services ( )
inlineprotected

Experimental, to be deprecated.

Definition at line 329 of file grpcpp/server_builder.h.

◆ set_fetcher()

void grpc::ServerBuilder::set_fetcher ( grpc_server_config_fetcher server_config_fetcher)
inlineprotected

Experimental API, subject to change.

Definition at line 347 of file grpcpp/server_builder.h.

◆ SetCompressionAlgorithmSupportStatus()

ServerBuilder & grpc::ServerBuilder::SetCompressionAlgorithmSupportStatus ( grpc_compression_algorithm  algorithm,
bool  enabled 
)

Set the support status for compression algorithms. All algorithms are enabled by default.

Incoming calls compressed with an unsupported algorithm will fail with GRPC_STATUS_UNIMPLEMENTED.

Definition at line 188 of file server_builder.cc.

◆ SetContextAllocator()

ServerBuilder & grpc::ServerBuilder::SetContextAllocator ( std::unique_ptr< grpc::ContextAllocator context_allocator)

Set the allocator for creating and releasing callback server context. Takes the owndership of the allocator.

Definition at line 138 of file server_builder.cc.

◆ SetDefaultCompressionAlgorithm()

ServerBuilder & grpc::ServerBuilder::SetDefaultCompressionAlgorithm ( grpc_compression_algorithm  algorithm)

The default compression algorithm to use for all channel calls in the absence of a call-specific level. Note that it overrides any compression level set by SetDefaultCompressionLevel.

Definition at line 205 of file server_builder.cc.

◆ SetDefaultCompressionLevel()

ServerBuilder & grpc::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 at line 198 of file server_builder.cc.

◆ SetMaxMessageSize()

ServerBuilder& grpc::ServerBuilder::SetMaxMessageSize ( int  max_message_size)
inline
Deprecated:
For backward compatibility.

Definition at line 200 of file grpcpp/server_builder.h.

◆ SetMaxReceiveMessageSize()

ServerBuilder& grpc::ServerBuilder::SetMaxReceiveMessageSize ( int  max_receive_message_size)
inline

Set max receive message size in bytes. The default is GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH.

Definition at line 187 of file grpcpp/server_builder.h.

◆ SetMaxSendMessageSize()

ServerBuilder& grpc::ServerBuilder::SetMaxSendMessageSize ( int  max_send_message_size)
inline

Set max send message size in bytes. The default is GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH.

Definition at line 194 of file grpcpp/server_builder.h.

◆ SetOption()

ServerBuilder & grpc::ServerBuilder::SetOption ( std::unique_ptr< grpc::ServerBuilderOption option)

Definition at line 163 of file server_builder.cc.

◆ SetResourceQuota()

ServerBuilder & grpc::ServerBuilder::SetResourceQuota ( const grpc::ResourceQuota resource_quota)

Set the attached buffer pool for this server.

Definition at line 212 of file server_builder.cc.

◆ SetSyncServerOption()

ServerBuilder & grpc::ServerBuilder::SetSyncServerOption ( ServerBuilder::SyncServerOption  option,
int  value 
)

Only useful if this is a Synchronous server.

Definition at line 169 of file server_builder.cc.

Friends And Related Function Documentation

◆ grpc::experimental::OrcaServerInterceptorFactory

Definition at line 356 of file grpcpp/server_builder.h.

◆ grpc::testing::ServerBuilderPluginTest

Definition at line 355 of file grpcpp/server_builder.h.

Member Data Documentation

◆ acceptors_

std::vector<std::shared_ptr<grpc::internal::ExternalConnectionAcceptorImpl> > grpc::ServerBuilder::acceptors_
private

Definition at line 411 of file grpcpp/server_builder.h.

◆ algorithm

grpc_compression_algorithm grpc::ServerBuilder::algorithm

Definition at line 401 of file grpcpp/server_builder.h.

◆ authorization_provider_

std::shared_ptr<experimental::AuthorizationPolicyProviderInterface> grpc::ServerBuilder::authorization_provider_
private

Definition at line 414 of file grpcpp/server_builder.h.

◆ callback_generic_service_

grpc::CallbackGenericService* grpc::ServerBuilder::callback_generic_service_ {nullptr}
private

Definition at line 393 of file grpcpp/server_builder.h.

◆ context_allocator_

std::unique_ptr<ContextAllocator> grpc::ServerBuilder::context_allocator_
private

Definition at line 392 of file grpcpp/server_builder.h.

◆ cqs_

std::vector<grpc::ServerCompletionQueue*> grpc::ServerBuilder::cqs_
private

List of completion queues added via AddCompletionQueue method.

Definition at line 386 of file grpcpp/server_builder.h.

◆ creds_

std::shared_ptr<grpc::ServerCredentials> grpc::ServerBuilder::creds_
private

Definition at line 388 of file grpcpp/server_builder.h.

◆ enabled_compression_algorithms_bitset_

uint32_t grpc::ServerBuilder::enabled_compression_algorithms_bitset_
private

Definition at line 403 of file grpcpp/server_builder.h.

◆ generic_service_

grpc::AsyncGenericService* grpc::ServerBuilder::generic_service_ {nullptr}
private

Definition at line 391 of file grpcpp/server_builder.h.

◆ interceptor_creators_

std::vector< std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface> > grpc::ServerBuilder::interceptor_creators_
private

Definition at line 406 of file grpcpp/server_builder.h.

◆ internal_interceptor_creators_

std::vector< std::unique_ptr<grpc::experimental::ServerInterceptorFactoryInterface> > grpc::ServerBuilder::internal_interceptor_creators_
private

Definition at line 409 of file grpcpp/server_builder.h.

◆ is_set

bool grpc::ServerBuilder::is_set

Definition at line 396 of file grpcpp/server_builder.h.

◆ level

grpc_compression_level grpc::ServerBuilder::level

Definition at line 397 of file grpcpp/server_builder.h.

◆ max_receive_message_size_

int grpc::ServerBuilder::max_receive_message_size_
private

Definition at line 377 of file grpcpp/server_builder.h.

◆ max_send_message_size_

int grpc::ServerBuilder::max_send_message_size_
private

Definition at line 378 of file grpcpp/server_builder.h.

◆ maybe_default_compression_algorithm_

struct { ... } grpc::ServerBuilder::maybe_default_compression_algorithm_

◆ maybe_default_compression_level_

struct { ... } grpc::ServerBuilder::maybe_default_compression_level_

◆ options_

std::vector<std::unique_ptr<grpc::ServerBuilderOption> > grpc::ServerBuilder::options_
private

Definition at line 379 of file grpcpp/server_builder.h.

◆ plugins_

std::vector<std::unique_ptr<grpc::ServerBuilderPlugin> > grpc::ServerBuilder::plugins_
private

Definition at line 389 of file grpcpp/server_builder.h.

◆ ports_

std::vector<Port> grpc::ServerBuilder::ports_
private

Definition at line 381 of file grpcpp/server_builder.h.

◆ resource_quota_

grpc_resource_quota* grpc::ServerBuilder::resource_quota_
private

Definition at line 390 of file grpcpp/server_builder.h.

◆ server_config_fetcher_

grpc_server_config_fetcher* grpc::ServerBuilder::server_config_fetcher_ = nullptr
private

Definition at line 412 of file grpcpp/server_builder.h.

◆ services_

std::vector<std::unique_ptr<NamedService> > grpc::ServerBuilder::services_
private

Definition at line 380 of file grpcpp/server_builder.h.

◆ sync_server_settings_

SyncServerSettings grpc::ServerBuilder::sync_server_settings_
private

Definition at line 383 of file grpcpp/server_builder.h.


The documentation for this class was generated from the following files:


grpc
Author(s):
autogenerated on Fri May 16 2025 03:03:31