In a bidirectional streaming setup in gRPC, if one party terminates the TCP connection, for instance, if the server abruptly crashes and restarts, how can the client re-establish the connection? Do we need to handle this manually, or does gRPC itself have mechanisms to address this situation?
在gRPC中的双向流设置中,如果一方终止TCP连接,例如,如果服务器突然崩溃并重新启动,客户端如何重新建立连接?我们需要手动处理这个问题吗?或者gRPC本身有解决这种情况的机制吗?
proto file :
原稿文件:
service LiveService{
rpc Dial(LiveReq)returns(stream LiveResp){}
}
message LiveReq{
}
message LiveResp{
string rtmp = 1;
}
client service:
客户服务:
func handler() {
ctx := context.Background()
req := rpc.LiveReq{}
dial, err := slave.GetLiveClient().Dial(ctx, &req)
if err != nil {
panic(err)
}
for true {
resp, err := dial.Recv() // tcp stop ,server stop
if err != nil {
fmt.Print(err)
}else{
fmt.Print(resp)
}
}
}
how to recover connection?
如何恢复连接?
--------------------------re-edit---------------------------
I did this, check out the link
1.https://github.com/grpc/grpc-go/issues/5507
--------------------------re-edit---------------------------这是我做的,请查看链接1。https://github.com/grpc/grpc-go/issues/5507
更多回答
Does this answer your question? (i.e. the connection will be re-established automatically but it's up to you to re-establish the stream).
这回答了你的问题吗?(即连接将自动重新建立,但重新建立流取决于您)。
@Brits Thanks your reply。I know the TCP re-establishment method, but I want to restore the rpc stream. Can I only execute "slave.GetLiveClient().Dial(ctx, &req)" again, and can't it be automatically restored?
@英国人感谢你的回复。我知道TCP重建方法,但我想恢复RPC流。只能再次执行Slave.GetLiveClient().Dial(ctx,&req),不能自动恢复吗?
Correct - if the stream dies (whether due to a network issue or something else) you need to make an RPC call to re-establish it.
正确-如果流终止(无论是由于网络问题还是其他原因),您需要进行RPC调用以重新建立它。
That just moves the reconnection logic into an Interceptor. This is fine if it achieves your goal; personally I'd rather handle this where the stream is managed because restarting a stream may have unexpected impacts e.g.. loss of data from while the connection was down.
这只是将重新连接逻辑移动到拦截器中。如果它实现了您的目标,这是很好的;就我个人而言,我更愿意在管理流的地方处理这一问题,因为重新启动流可能会产生意想不到的影响。在连接断开时丢失的数据。
我是一名优秀的程序员,十分优秀!