[C++] Non blocking "stop new work" API at shutdown

36 views
Skip to first unread message

Arpit Baldeva

unread,
May 21, 2018, 12:50:48 PM5/21/18
to grpc.io
Hi,


It seems like there is a discrepancy in the way grpc shutdown API works between Java and C++. From Java docs, 

shutdown()
Initiates an orderly shutdown in which preexisting calls continue but new calls are rejected.

shutdownNow()
Initiates a forceful shutdown in which preexisting and new calls are rejected.

The Java shutdown call looks non-blocking from the documentation. Looking at C++ implementation, it looks like when the shutdown call is invoked without specifying the deadline, it blocks until all the work is done. When deadline is specified, the pending work will be cancelled after deadline. The blocking nature of the Shutdown api makes it hard to initiate shutdown sequence. Is it possible to add an api that simply stops new incoming work in a non-blocking fashion (like Java shutdown API)? The application can then process pending rpcs in a graceful manner and just block when necessary. 

Thanks. 

Vijay Pai

unread,
May 22, 2018, 2:14:11 AM5/22/18
to grpc.io
For what you're describing, is it sufficient to just do something like (assuming you're in a member function of a class that contains a Server pointer named server_)

std::thread t([this]{server_->Shutdown();});
... other stuff ...
t.join(); // when ready to finish work

That way you can initiate the Shutdown in a non-blocking fashion without waiting for its outcome. Keep in mind that Shutdown doesn't actually guarantee that no new work gets through, it just means that the application cannot expect to start new work and have it complete.

- Vijay
Reply all
Reply to author
Forward
0 new messages