correct way to cancel a streaming connection attempt in c++

1,997 views
Skip to first unread message

larso...@gmail.com

unread,
May 22, 2015, 6:29:55 PM5/22/15
to grp...@googlegroups.com
see this thread for some related discussion (java):

I want to create a bidirectional streaming connection that runs for, say, 60 seconds, and I'd like to have it fail if it can't connect within 5 seconds, allowing the user to fix connection details or whatever.  If I set a grpc deadline it has to apply to the entire RPC, so I'd need to set a timeout of more than 60 seconds to avoid interrupting the normal streaming duration.  Without a deadline set it will attempt to connect forever.

Is there a way to cancel the connection attempt while it's running, before it has connected?  I ran stub_.reset() from another thread but it didn't do anything immediately.  grpc_shutdown() seems harmful as it had a mutex problem later on (and I see this was removed from the examples as no longer necessary).

Michael Larson

unread,
May 26, 2015, 12:28:42 PM5/26/15
to grp...@googlegroups.com
a tentative answer to my question:

You can call TryCancel() on the client context from another thread.  I started a "connection monitor" thread just before creating the stream object (the point at which the code would normally hang until the grpc deadline).  With some external synchronization this thread can know whether the stream has received any data within N seconds and call TryCancel() if it hasn't.  I am not using the grpc deadline (it is set to its default, a very long time) and am watching received messages to implement my own timeout while the stream is running.

Please let me know if there is a better or more intended way to do this.

Thanks,

Michael

Prashant Shubham

unread,
Nov 25, 2018, 8:22:02 AM11/25/18
to grpc.io
Hi Michael, Were you able to find any other way to cancel the stream from client side?
Reply all
Reply to author
Forward
0 new messages