How to handle connection/channel close issues in GRPC C#

8,368 views
Skip to first unread message

pra...@gmail.com

unread,
Jan 27, 2016, 8:40:42 AM1/27/16
to grpc.io
Hi All,

I have two questions. (these are unary calls)

1) While using synchronous gRPC calls, how should I find out when the Server goes down? Apparently, there is no exception thrown and the client C# code (generated by protoc and grpc extension) keeps polling for Server to get up. 

2) If I use asyn call, whats the best way to find out, when the Server goes down or the application ran to completion. I am using following approach of sleep and polling but I am sure this is not the best way to get it done. 

                    var client = GetDiscount.NewClient(channel);
                    var ctokenSource = new CancellationTokenSource();
                    var data = client.GetDealsAsync(new ModelAndCityId { CityId = 1, ModelId = 1 }, null, null, ctokenSource.Token);
                    
                    while (!data.ResponseAsync.IsCompleted)
                    {
                        System.Threading.Thread.Sleep(waitTime);
                        waitTime += waitTimeIncement;
                        if (waitTime >= 50)
                        {
                            ctokenSource.Cancel(true);
                            data.Dispose();
                            CustomGRPCLoadBalancer.SetServerAsNotReachable(channel.ResolvedTarget);
                            channel.ShutdownAsync().Wait();
                            dataReceivedSuccessfully = false;
                            break;
                        }

                    }


Please pardon me if I am missing something basic as I am still pretty immature with gRPC and protobuf. If you can point me to the sources where i can learn more about such things then that will be great.

pra...@gmail.com

unread,
Feb 1, 2016, 1:04:02 AM2/1/16
to grpc.io, pra...@gmail.com
Guys Someone please let me know if what I am expecting is right of I have totally misunderstood the way GRPC should work?

pra...@gmail.com

unread,
Feb 3, 2016, 2:30:18 AM2/3/16
to grpc.io, pra...@gmail.com
Well I have solved the first issue by adding a deadline of 5 second to the call so if the Server goes down, the channel tries to connect it for 5 seconds else throws exception of DeadLine exceeded. Which serves my purpose.

Vijay Pai

unread,
Feb 16, 2016, 7:04:49 PM2/16/16
to grpc.io, pra...@gmail.com
Hello there,

1. It is intentional that the client will keep trying if no deadline is specified, since the server or network connection might just be done temporarily. You can specify a deadline if you don't want to wait. 
On a more advanced level, there is also an API to check the current connectivity state of the channel - this was added to C# in https://github.com/grpc/grpc/pull/2756 . For a given channel, you can check "channel.State" and see if it is one of the values specified in the enum ChannelState : ChannelState.Idle (idle channel), ChannelState.Connecting (trying to connect), ChannelState.Ready (ready for data transmission), ChannelState.TransientFailure (a failure in the channel that may be temporary), or ChannelState.FatalFailure (a permanent failure on this channel). You can also get notified when the channel state changes with channel.WaitForStateChangedAsync . Examples of how to do this are in src/csharp/Grpc.Core.Tests/ChannelTest.cs .

2. Please see the C# tutorial in http://www.grpc.io/docs/tutorials/basic/csharp.html - the example code and test codes in gRPC tend to follow the standard C# paradigm of using await for async methods. 

I hope that helps!

Regards,
Vijay

prasad gawde

unread,
Feb 18, 2016, 5:32:46 AM2/18/16
to grpc.io, pra...@gmail.com
Thanks Viraj.

The reason I have added deadline is to figure out if the Server has gone down after I created the channel. With the new release of 0.13, the good part is, an exception is thrown if the channel goes down during the RPC request is being processed. 
Reply all
Reply to author
Forward
0 new messages