有人可以帮助我在这里了解gRPC代码客户端中 channel 的用法(用于双向流式RPC):
https://grpc.io/docs/tutorials/basic/go.html
这是代码:
stream, err := client.RouteChat(context.Background())
waitc := make(chan struct{})
go func() {
for {
in, err := stream.Recv()
if err == io.EOF {
// read done.
close(waitc)
return
}
if err != nil {
log.Fatalf("Failed to receive a note : %v", err)
}
log.Printf("Got message %s at point(%d, %d)", in.Message, in.Location.Latitude, in.Location.Longitude)
}
}()
for _, note := range notes {
if err := stream.Send(note); err != nil {
log.Fatalf("Failed to send a note: %v", err)
}
}
stream.CloseSend()
<-waitc
谢谢!
最佳答案
channel waitc
用于使main
线程等待goroutine从服务器接收完成并正常关闭连接。
<-waitc // is a blocking operation. It can evaluate when channel been closed
关于go - 试图了解golang中gRPC客户端中 channel 的使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49393106/