Add a safety check to ensure atleast one of the completion queues is

listening completion queue (i.e frequently polled)
pull/6149/head
Sree Kuchibhotla 9 years ago
parent 1f5e262589
commit 7def036085
  1. 17
      src/cpp/server/server_builder.cc

@ -86,8 +86,11 @@ void ServerBuilder::AddListeningPort(const grpc::string& addr,
std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
std::unique_ptr<ThreadPoolInterface> thread_pool;
// Does this server have atleast one sync method
bool has_sync_methods = false;
for (auto it = services_.begin(); it != services_.end(); ++it) {
if ((*it)->service->has_synchronous_methods()) {
has_sync_methods = true;
if (thread_pool == nullptr) {
thread_pool.reset(CreateDefaultThreadPool());
break;
@ -105,6 +108,12 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
compression_options_.enabled_algorithms_bitset);
std::unique_ptr<Server> server(
new Server(thread_pool.release(), true, max_message_size_, &args));
// 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) {
// A completion queue that is not polled frequently (by calling Next() or
// AsyncNext()) is not safe to use for listening to incoming channels.
@ -113,11 +122,19 @@ std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
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,
"Atleast 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)) {

Loading…
Cancel
Save