In gRPC c++, is there a way to get notified when the peer gets disconnected?

116 views
Skip to first unread message

Sachin Bharadwaj S

unread,
Aug 12, 2020, 6:57:52 AM8/12/20
to grpc.io

I have implemented a gRPC server application and multiple clients can connect to it and call RPCs.

In the cases of client disconnection or client restarts, I would want to know which client got disconnected so that I can do some cleanup corresponding to that client.

Similarly, if the server goes down, how can the client get notified?

Does gRPC c++ stack provides a notification/callback to the application? If not, what is the best way to handle the connection termination on either side?

Mya Pitzeruse

unread,
Aug 12, 2020, 10:59:56 AM8/12/20
to Sachin Bharadwaj S, grpc.io
Let's start with the server case because it's a bit easier. gRPC provides some built in healthchecking capabilities. Clients watch server health. When a server goes unhealthy, clients remove them from the pool.

gRPC doesn't necessarily surface connection primitives to your server side so it's probably not easy to get at the same information for clients. On the other hand, you could use something like a long lived bidi-streaming API to "simulate" a connection with a backend. You'll need to handle network disconnects, but you could use that approach to do some cleanup. While it's not C++, here's a go example where a server side streaming API cleans up a subscription when the client is disconnected.


--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/ab98d62c-231b-4e5b-bc0e-6519a9d0320ao%40googlegroups.com.


--

Mya Pitzeruse

Principal Software Engineer - Service Infrastructure

Gender Pronouns: She, Her, Hers

mjp...@indeed.com


Indeed - We help people get jobs.

Indeed.com


Facebook  |  Twitter  |  Instagram

Mya Pitzeruse

unread,
Aug 12, 2020, 11:00:42 AM8/12/20
to Sachin Bharadwaj S, grpc.io

apo...@google.com

unread,
Aug 12, 2020, 2:31:54 PM8/12/20
to grpc.io
If you're using the async completion queue based C++ API, then I believe you can use the AsyncNotifyWhenDone API for this. See surrounding comments in the linked code for more details.

Thomas Mercier

unread,
Aug 12, 2020, 2:36:13 PM8/12/20
to apo...@google.com, grpc.io
There is currently some sort of intermittent bug related to that API: https://github.com/grpc/grpc/issues/18929

Sachin Bharadwaj S

unread,
Aug 17, 2020, 2:24:45 AM8/17/20
to Thomas Mercier, Mya Pitzeruse, apo...@google.com, grpc.io
Most of the answers are related to async c++ API.
But, my implementation is based on sync c++ API on both client and server-side. Let me explore more on bidirectional streaming or switch to async

Thanks everyone for the responses

Regards,
Sachin

Reply all
Reply to author
Forward
0 new messages