gRPC客户端如何了解网络丢失或故障?

huangapple go评论121阅读模式
英文:

How can gRPC client know about network loss/failure?

问题

有一个使用 gRPC 在 Java 项目中进行的服务器端流式传输。当服务器和客户端之间的网络连接中断时,客户端甚至不会知道这个问题,流观察器处于空闲状态,没有任何活动。我该如何获悉这个断开连接呢?

英文:

There is a server-side streaming in java project by using grpc. When the network was broken between server and client, the client doesn't even know about the problem, the streamobserver is idle and no activity. How can I know about this disconnection?

答案1

得分: 1

你发送消息前无法知道连接是否中断。这就是TCP协议的工作原理

因此,如果你正在进行服务器端流式传输,突然拔掉了调制解调器,客户端直到操作系统关闭套接字(可能需要几分钟)之前都不会知道。

你可以选择改为实现双向流式传输,让客户端定期发送请求,这样在尝试写入套接字时就会知道网络故障,或者你可以启用keepAlive

英文:

You do not know the connection broken until sending a message. That's how TCP protocol works.

So if you are in the middle of server-side streaming and suddenly unplugged your modem, the client side would never know until the OS closes the socket (may take minutes).

You could either change to implement bidi-streaming that the client sends requests periodically so it knows network failures when trying to write to the socket, or you can enable keepAlive.

huangapple
  • 本文由 发表于 2020年10月15日 14:17:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64365853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定