diff options
author | Yuchen Zeng <zyc@google.com> | 2016-06-08 17:52:05 -0700 |
---|---|---|
committer | Yuchen Zeng <zyc@google.com> | 2016-06-08 17:52:05 -0700 |
commit | 0a0c1b0d59341e2535e27d86f1cd61f6c114b2ed (patch) | |
tree | 7910593b0f6919ae7bc8269cb8394f4ac0502fd9 /src/cpp/server | |
parent | 2bdde23c25b2df3f5b94100226ca2957e4e2cba4 (diff) | |
parent | d861b13aff7481b4901ab012cfd51c5887b11a2e (diff) |
Merge remote-tracking branch 'upstream/master' into base64_decode
Diffstat (limited to 'src/cpp/server')
-rw-r--r-- | src/cpp/server/server.cc | 11 | ||||
-rw-r--r-- | src/cpp/server/server_builder.cc | 39 |
2 files changed, 42 insertions, 8 deletions
diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index f955a31494..f6c3e5747c 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -295,7 +295,12 @@ Server::Server(ThreadPoolInterface* thread_pool, bool thread_pool_owned, grpc_channel_args channel_args; args->SetChannelArgs(&channel_args); server_ = grpc_server_create(&channel_args, nullptr); - grpc_server_register_completion_queue(server_, cq_.cq(), nullptr); + if (thread_pool_ == nullptr) { + grpc_server_register_non_listening_completion_queue(server_, cq_.cq(), + nullptr); + } else { + grpc_server_register_completion_queue(server_, cq_.cq(), nullptr); + } } Server::~Server() { @@ -407,7 +412,9 @@ bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { sync_methods_->push_back(SyncRequest(unknown_method_.get(), nullptr)); } for (size_t i = 0; i < num_cqs; i++) { - new UnimplementedAsyncRequest(this, cqs[i]); + if (cqs[i]->IsFrequentlyPolled()) { + new UnimplementedAsyncRequest(this, cqs[i]); + } } } // Start processing rpcs. diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 9658a56745..54feac3982 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -60,8 +60,9 @@ ServerBuilder::ServerBuilder() } } -std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue() { - ServerCompletionQueue* cq = new ServerCompletionQueue(); +std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue( + bool is_frequently_polled) { + ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled); cqs_.push_back(cq); return std::unique_ptr<ServerCompletionQueue>(cq); } @@ -99,10 +100,12 @@ void ServerBuilder::AddListeningPort(const grpc::string& addr, std::unique_ptr<Server> ServerBuilder::BuildAndStart() { std::unique_ptr<ThreadPoolInterface> thread_pool; + bool has_sync_methods = false; for (auto it = services_.begin(); it != services_.end(); ++it) { if ((*it)->service->has_synchronous_methods()) { if (thread_pool == nullptr) { thread_pool.reset(CreateDefaultThreadPool()); + has_sync_methods = true; break; } } @@ -116,6 +119,7 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() { for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) { if ((*plugin).second->has_sync_methods()) { thread_pool.reset(CreateDefaultThreadPool()); + has_sync_methods = true; break; } } @@ -123,15 +127,38 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() { if (max_message_size_ > 0) { args.SetInt(GRPC_ARG_MAX_MESSAGE_LENGTH, max_message_size_); } - args.SetInt(GRPC_COMPRESSION_ALGORITHM_STATE_ARG, + args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, compression_options_.enabled_algorithms_bitset); std::unique_ptr<Server> server( new Server(thread_pool.release(), true, max_message_size_, &args)); ServerInitializer* initializer = server->initializer(); + + // If the server has atleast one sync methods, we know that this is a Sync + // server or a Hybrid server and the completion queue (server->cq_) would be + // frequently polled. + int num_frequently_polled_cqs = has_sync_methods ? 1 : 0; + for (auto cq = cqs_.begin(); cq != cqs_.end(); ++cq) { - grpc_server_register_completion_queue(server->server_, (*cq)->cq(), - nullptr); + // A completion queue that is not polled frequently (by calling Next() or + // AsyncNext()) is not safe to use for listening to incoming channels. + // Register all such completion queues as non-listening completion queues + // with the GRPC core library. + if ((*cq)->IsFrequentlyPolled()) { + grpc_server_register_completion_queue(server->server_, (*cq)->cq(), + nullptr); + num_frequently_polled_cqs++; + } else { + grpc_server_register_non_listening_completion_queue(server->server_, + (*cq)->cq(), nullptr); + } + } + + if (num_frequently_polled_cqs == 0) { + gpr_log(GPR_ERROR, + "At least one of the completion queues must be frequently polled"); + return nullptr; } + for (auto service = services_.begin(); service != services_.end(); service++) { if (!server->RegisterService((*service)->host.get(), (*service)->service)) { |