6
votes

I'm using the gRPC client in C# and using a long-lived duplex stream. However, the TCP connection is closed at some time and therefore I would like to use a keepalive in the client. The server (written in Go) is already configured correctly for the keepalive and already tested with clients written in Go.

I use the following code to set a keepalive for 5 minutes and also to enable tracing for viewing 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, in the log there are no bytes sent at 5 minutes and the connection is closed as I can no longer send/receive messages via the same stream after the stream has been idle for some time.

How would I properly enable keepalive?

1

1 Answers

2
votes

try add the following ChannelOption in the client.

new ChannelOption("grpc.keepalive_permit_without_calls", 1)