8
votes

I am writing a connection back to a TensorFlow Serving system with gRPC from a C# platform on MS Windows 10. I have seen many references to Time-out and Dead-line with the C++ API for gRPC, but can't seem to figure out how to for a timeout under C#.

I am simply opening a channel to the server, setting up a client and the calling to the server. I would like this Classify to time-out after 5 seconds or so. Any help or direction would be appreciated.

channel = new Channel(modelServer, ChannelCredentials.Insecure);

var client = MyService.NewClient(channel);

MyResponse classvalue = client.Classify(featureSet);

2

2 Answers

13
votes

To set the deadline for a call, you can simply use the following "deadline:"

client.Classify(featureSet, deadline: DateTime.UtcNow.AddSeconds(5));

or

client.Classify(featureSet, new CallOptions(deadline: DateTime.UtcNow.AddSeconds(5)));

Both ways should be easily discoverable by code completion.

8
votes

GRPC timeout to detect server down scenario needs to be used on the call to connect the channel. I did it like this:

    private async void SampleCode()
     {
          var client = await GetClient();
          var data = await client.GetAllTemplatesAsync(request, new 
                CallOptions().WithDeadline(DateTime.UtcNow.AddSeconds(7)));

     }

    private async Task<MyGrpcClient> GetClient()
    {
        var channel = new Channel("somehost",23456, ChannelCredentials.Insecure);
        await channel.ConnectAsync(deadline: DateTime.UtcNow.AddSeconds(2));
        return new MyGrpcClient(channel);
    }

So, if the server is down the call to GetClient() will timeout. Where as if the server is up but response takes too long then the timeout on the client service call will take effect.