Set keepalive for gRPC in c # client

I am using a gRPC client in C # and using a long lived duplex stream. However, the TCP connection closes at some time and so I would like to use keepalive on the client. The server (written in Go) is already configured correctly for keepalive and has already been tested with clients written in Go.

I am using the following code to set keepalive for 5 minutes, and also to enable trace to see all incoming / outgoing bytes.

Environment.SetEnvironmentVariable("GRPC_TRACE", "tcp,channel,http,secure_endpoint");
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "DEBUG");

var callCredentials = CallCredentials.FromInterceptor(Interceptor());

var roots = Encoding.UTF8.GetString(Resources.roots);

Channel = new Channel(address, ChannelCredentials.Create(new SslCredentials(roots), callCredentials), new[]
{
    new ChannelOption("grpc.keepalive_time_ms", 5 * 60 *  1000), // 5 minutes
});

await Channel.ConnectAsync(DateTime.UtcNow.AddSeconds(5));

      

However, there are no bytes in the log sent after 5 minutes and the connection is closed as I can no longer send / receive messages through the same thread after the thread has been idle for a while.

How to enable keepalive correctly?

+3


source to share





All Articles