aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/grpc++/server_builder.h53
-rw-r--r--src/cpp/server/server_builder.cc35
2 files changed, 50 insertions, 38 deletions
diff --git a/include/grpc++/server_builder.h b/include/grpc++/server_builder.h
index 8fac168ff7..42e2268851 100644
--- a/include/grpc++/server_builder.h
+++ b/include/grpc++/server_builder.h
@@ -34,6 +34,7 @@
#ifndef GRPCXX_SERVER_BUILDER_H
#define GRPCXX_SERVER_BUILDER_H
+#include <climits>
#include <map>
#include <memory>
#include <vector>
@@ -42,6 +43,8 @@
#include <grpc++/impl/server_builder_plugin.h>
#include <grpc++/support/config.h>
#include <grpc/compression.h>
+#include <grpc/support/cpu.h>
+#include <grpc/support/useful.h>
namespace grpc {
@@ -62,21 +65,7 @@ class ServerBuilder {
public:
ServerBuilder();
- struct SyncServerSettings {
- // Number of server completion queues to create to listen to incoming RPCs.
- int num_cqs;
-
- // Minimum number of threads per completion queue that should be listening
- // to incoming RPCs.
- int min_pollers;
-
- // Maximum number of threads per completion queue that can be listening to
- // incoming RPCs.
- int max_pollers;
-
- // The timeout for server completion queue's AsyncNext call.
- int cq_timeout_msec;
- };
+ enum SyncServerOption { NUM_CQS, MIN_POLLERS, MAX_POLLERS, CQ_TIMEOUT_MSEC };
/// Register a service. This call does not take ownership of the service.
/// The service must exist for the lifetime of the \a Server instance returned
@@ -131,8 +120,8 @@ class ServerBuilder {
ServerBuilder& SetOption(std::unique_ptr<ServerBuilderOption> option);
- /// Note: Only useful if this is a Synchronous server.
- void SetSyncServerSettings(SyncServerSettings settings);
+ /// Only useful if this is a Synchronous server.
+ ServerBuilder& SetSyncServerOption(SyncServerOption option, int value);
/// Tries to bind \a server to the given \a addr.
///
@@ -189,13 +178,27 @@ class ServerBuilder {
int* selected_port;
};
- // Sync server settings. If this is not set via SetSyncServerSettings(), the
- // following default values are used:
- // sync_server_settings_.num_cqs = Number of CPUs
- // sync_server_settings_.min_pollers = 1
- // sync_server_settings_.max_pollers = INT_MAX
- // sync_server_settings_.cq_timeout_msec = 1000
- struct SyncServerSettings sync_server_settings_;
+ struct SyncServerSettings {
+ SyncServerSettings()
+ : num_cqs(GPR_MAX(gpr_cpu_num_cores(), 4)),
+ min_pollers(1),
+ max_pollers(INT_MAX),
+ cq_timeout_msec(100) {}
+
+ // Number of server completion queues to create to listen to incoming RPCs.
+ int num_cqs;
+
+ // Minimum number of threads per completion queue that should be listening
+ // to incoming RPCs.
+ int min_pollers;
+
+ // Maximum number of threads per completion queue that can be listening to
+ // incoming RPCs.
+ int max_pollers;
+
+ // The timeout for server completion queue's AsyncNext call.
+ int cq_timeout_msec;
+ };
typedef std::unique_ptr<grpc::string> HostString;
struct NamedService {
@@ -212,6 +215,8 @@ class ServerBuilder {
std::vector<std::unique_ptr<NamedService>> services_;
std::vector<Port> ports_;
+ SyncServerSettings sync_server_settings_;
+
/* List of completion queues added via AddCompletionQueue() method */
std::vector<ServerCompletionQueue*> cqs_;
diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc
index 59c40dedaf..6b4ff28972 100644
--- a/src/cpp/server/server_builder.cc
+++ b/src/cpp/server/server_builder.cc
@@ -55,6 +55,7 @@ static void do_plugin_list_init(void) {
ServerBuilder::ServerBuilder()
: max_receive_message_size_(-1),
max_send_message_size_(-1),
+ sync_server_settings_(SyncServerSettings()),
generic_service_(nullptr) {
gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
for (auto it = g_plugin_factory_list->begin();
@@ -70,16 +71,6 @@ ServerBuilder::ServerBuilder()
sizeof(maybe_default_compression_level_));
memset(&maybe_default_compression_algorithm_, 0,
sizeof(maybe_default_compression_algorithm_));
-
- // Sync server setting defaults
- sync_server_settings_.min_pollers = 1;
- sync_server_settings_.max_pollers = INT_MAX;
-
- int num_cpus = gpr_cpu_num_cores();
- num_cpus = GPR_MAX(num_cpus, 4);
- sync_server_settings_.num_cqs = num_cpus;
-
- sync_server_settings_.cq_timeout_msec = 1000;
}
std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue(
@@ -119,6 +110,26 @@ ServerBuilder& ServerBuilder::SetOption(
return *this;
}
+ServerBuilder& ServerBuilder::SetSyncServerOption(
+ ServerBuilder::SyncServerOption option, int val) {
+ switch (option) {
+ case NUM_CQS:
+ sync_server_settings_.num_cqs = val;
+ break;
+
+ case MIN_POLLERS:
+ sync_server_settings_.min_pollers = val;
+ break;
+ case MAX_POLLERS:
+ sync_server_settings_.max_pollers = val;
+ break;
+ case CQ_TIMEOUT_MSEC:
+ sync_server_settings_.cq_timeout_msec = val;
+ break;
+ }
+ return *this;
+}
+
ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus(
grpc_compression_algorithm algorithm, bool enabled) {
if (enabled) {
@@ -142,10 +153,6 @@ ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm(
return *this;
}
-void ServerBuilder::SetSyncServerSettings(SyncServerSettings settings) {
- sync_server_settings_ = settings; // copy the settings
-}
-
ServerBuilder& ServerBuilder::AddListeningPort(
const grpc::string& addr, std::shared_ptr<ServerCredentials> creds,
int* selected_port) {