From e50e5cbde2f77166b9f557938252de6792557f84 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Tue, 18 Aug 2015 12:44:57 -0700 Subject: Add a timeout to shutdown to forcefully end calls --- include/grpc++/server.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'include/grpc++') diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 8755b4b445..084c9936d5 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -63,7 +63,13 @@ class Server GRPC_FINAL : public GrpcLibrary, private CallHook { ~Server(); // Shutdown the server, block until all rpc processing finishes. - void Shutdown(); + // Forcefully terminate pending calls after deadline expires. + template + void Shutdown(const T& deadline) { + ShutdownInternal(TimePoint(deadline).raw_time()); + } + + void Shutdown() { ShutdownInternal(gpr_inf_future(GPR_CLOCK_MONOTONIC)); } // Block waiting for all work to complete (the server must either // be shutting down or some other thread must call Shutdown for this @@ -98,6 +104,8 @@ class Server GRPC_FINAL : public GrpcLibrary, private CallHook { void PerformOpsOnCall(CallOpSetInterface* ops, Call* call) GRPC_OVERRIDE; + void ShutdownInternal(gpr_timespec deadline); + class BaseAsyncRequest : public CompletionQueueTag { public: BaseAsyncRequest(Server* server, ServerContext* context, -- cgit v1.2.3 From 9374ce819bff3c933f08b9512ded5c513527fd1f Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 19 Aug 2015 10:15:44 -0700 Subject: Add comments, fix a subtle bug --- include/grpc++/server.h | 1 + src/cpp/server/server.cc | 4 ++++ 2 files changed, 5 insertions(+) (limited to 'include/grpc++') diff --git a/include/grpc++/server.h b/include/grpc++/server.h index 6a15dcb371..a2bc097c7f 100644 --- a/include/grpc++/server.h +++ b/include/grpc++/server.h @@ -69,6 +69,7 @@ class Server GRPC_FINAL : public GrpcLibrary, private CallHook { ShutdownInternal(TimePoint(deadline).raw_time()); } + // Shutdown the server, waiting for all rpc processing to finish. void Shutdown() { ShutdownInternal(gpr_inf_future(GPR_CLOCK_MONOTONIC)); } // Block waiting for all work to complete (the server must either diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index b27aa32276..8b21337529 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -329,11 +329,15 @@ void Server::ShutdownInternal(gpr_timespec deadline) { shutdown_ = true; grpc_server_shutdown_and_notify(server_, cq_.cq(), new ShutdownRequest()); cq_.Shutdown(); + // Spin, eating requests until the completion queue is completely shutdown. + // If the deadline expires then cancel anything that's pending and keep + // spinning forever until the work is actually drained. SyncRequest* request; bool ok; while (SyncRequest::AsyncWait(&cq_, &request, &ok, deadline)) { if (request == NULL) { // deadline expired grpc_server_cancel_all_calls(server_); + deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC); } else if (ok) { SyncRequest::CallData call_data(this, request); } -- cgit v1.2.3