Set keepalive in C# client

1,521 views
Skip to first unread message

koen.vl...@gmail.com

unread,
Jul 22, 2017, 3:08:14 PM7/22/17
to grpc.io

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?

Jan Tattermusch

unread,
Jul 24, 2017, 8:21:54 AM7/24/17
to koen.vl...@gmail.com, grpc.io
Have you tried adding GrpcEnvironment.SetLogger(new ConsoleLogger());?

Getting gRPC traces in C# is a bit more complicated that it ought to be (https://github.com/grpc/grpc/issues/10574)

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/3509b5e5-87d6-4bfd-a8d6-fc3a0446aa99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

koen.vl...@gmail.com

unread,
Jul 24, 2017, 12:02:17 PM7/24/17
to grpc.io, koen.vl...@gmail.com
I already had a logger in place and I was also seeing logs, there were just no outgoing logs at the 5 minute mark or any time after that, even though I still had a connection open.

For now, I'm manually sending ping messages every 5 minutes on the channel to keep it open, but this should not be necessary as e.g. in Go keep-alive already works.


On Monday, July 24, 2017 at 2:21:54 PM UTC+2, Jan Tattermusch wrote:
Have you tried adding GrpcEnvironment.SetLogger(new ConsoleLogger());?

Getting gRPC traces in C# is a bit more complicated that it ought to be (https://github.com/grpc/grpc/issues/10574)
On Sat, Jul 22, 2017 at 9:08 PM, <koen.vl...@gmail.com> wrote:

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?

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.

Jan Tattermusch

unread,
Aug 4, 2017, 3:47:48 AM8/4/17
to grpc.io, koen.vl...@gmail.com
In that case, please file a github issue and include as much info as possible to reproduce.

thanks
Jan

wangju...@gmail.com

unread,
Dec 4, 2017, 10:55:58 PM12/4/17
to grpc.io
I encounter the same problem, and I don't know how to deal with it.
Reply all
Reply to author
Forward
0 new messages