Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make gRPC client Timeout in C# if the server is down?

Tags:

c#

grpc

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);

like image 520
mazecreator Avatar asked Jun 07 '16 03:06

mazecreator


People also ask

Does gRPC have a timeout?

gRPC supports specifying a timeout for both client as well as the server. The Client can specify during runtime the amount of time it wants to wait before cancelling the request. The Server can also check on its end whether the requests need to be catered to or has the Client already given up on the request.

What is gRPC keepalive?

GRPC uses keepalive ping as a way to check if a channel is currently working by sending HTTP2 pings over the transport. It is sent periodically, and if the ping is not acknowledged by the peer within a certain timeout period, the transport is disconnected [1].

What is blocking stub in gRPC?

a blocking/synchronous stub: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception. a non-blocking/asynchronous stub that makes non-blocking calls to the server, where the response is returned asynchronously.

What is gRPC deadline?

A deadline allows a gRPC client to specify how long it will wait for a call to complete. When a deadline is exceeded, the call is canceled. Setting a deadline is important because it provides an upper limit on how long a call can run for.


1 Answers

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.

like image 130
Jan Tattermusch Avatar answered Oct 05 '22 02:10

Jan Tattermusch