diff options
Diffstat (limited to 'src/cpp/server/server_builder.cc')
-rw-r--r-- | src/cpp/server/server_builder.cc | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/src/cpp/server/server_builder.cc b/src/cpp/server/server_builder.cc index 66e2055af0..8d8276ca00 100644 --- a/src/cpp/server/server_builder.cc +++ b/src/cpp/server/server_builder.cc @@ -33,13 +33,15 @@ #include <grpc++/server_builder.h> +#include <grpc/support/cpu.h> #include <grpc/support/log.h> #include <grpc++/impl/service_type.h> #include <grpc++/server.h> +#include "src/cpp/server/thread_pool.h" namespace grpc { -ServerBuilder::ServerBuilder() : thread_pool_(nullptr) {} +ServerBuilder::ServerBuilder() {} void ServerBuilder::RegisterService(SynchronousService *service) { services_.push_back(service->service()); @@ -64,14 +66,27 @@ void ServerBuilder::SetThreadPool(ThreadPoolInterface *thread_pool) { } std::unique_ptr<Server> ServerBuilder::BuildAndStart() { - std::unique_ptr<Server> server(new Server(thread_pool_, creds_.get())); + bool thread_pool_owned = false; + if (!thread_pool_ && services_.size()) { + int cores = gpr_cpu_num_cores(); + if (!cores) cores = 4; + thread_pool_ = new ThreadPool(cores); + thread_pool_owned = true; + } + std::unique_ptr<Server> server(new Server(thread_pool_, thread_pool_owned, creds_.get())); for (auto *service : services_) { - server->RegisterService(service); + if (!server->RegisterService(service)) { + return nullptr; + } } for (auto &port : ports_) { - server->AddPort(port); + if (!server->AddPort(port)) { + return nullptr; + } + } + if (!server->Start()) { + return nullptr; } - server->Start(); return server; } |