在C#客户端中为gRPC设置keepalive [英] Set keepalive for gRPC in C# client

查看:55
本文介绍了在C#客户端中为gRPC设置keepalive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#中使用gRPC客户端,并使用了长期存在的双工流.但是,TCP连接有时会关闭,因此我想在客户端中使用keepalive.服务器(用Go编写)已经为Keepalive正确配置,并且已经用Go编写的客户端进行了测试.

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.

我使用以下代码将Keepalive设置为5分钟,并启用跟踪以查看所有传入/传出字节.

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

但是,在日志中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.

如何正确启用Keepalive?

How would I properly enable keepalive?

推荐答案

请尝试在客户端中添加以下ChannelOption.

try add the following ChannelOption in the client.

new ChannelOption("grpc.keepalive_permit_without_calls", 1)

这篇关于在C#客户端中为gRPC设置keepalive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆