【发布时间】:2019-03-06 13:26:16
【问题描述】:
请帮我理解grpc客户端连接错误处理。
我以前使用过 Micrsoft WCF。如果没有服务接受如下传入连接,尝试多次连接并放弃很容易:
// this is pseudo code
int attemptCount = 0;
while (true)
try
{
client.Connect(); // exception is raised if there is not service listening for incoming connection
break;
}
catch (Exception)
{
client.Abort(); // to clear connection faulted state
if (++attempCount == 5)
throw;
Thread.Wait(500); // waiting for service to start
}
如果在某个 IP:port 上没有服务监听,则会进行 5 次连接尝试,然后终止客户端应用程序
这在我同时从 VisualStudio 调试启动客户端和服务时使用,因此有时客户端首先启动,它必须等待服务启动。
我尝试使用 gRPC 客户端执行相同的操作,但无法将 channel.State 从 ChannelState.TransientFailure 重置为正常工作。我知道如果出现问题,gRPC 会在连接之间暂停:
对于许多非致命故障(例如,TCP 连接尝试超时 因为服务器尚不可用),频道可能会花费 越来越多的时间处于这种状态。 https://grpc.io/grpc-java/javadoc/io/grpc/ConnectivityState.html
我可以尝试使用WaitForStateChangedAsync,但是如何配置 gRPC 客户端以在重新连接尝试之间等待一定的时间?
有没有其他方法可以多次连接并终止 gRPC 的客户端?
谢谢
【问题讨论】: