英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论