【问题标题】:Trying to understand the use of channel in gRPC client in golang试图了解golang中gRPC客户端中通道的使用
【发布时间】:2018-03-20 19:59:19
【问题描述】:

有人可以帮助我了解 gRPC 代码客户端中通道的用法(用于双向流 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

谢谢!

【问题讨论】:

  • 具体有什么需要帮助理解的?

标签: go grpc


【解决方案1】:

Channel waitc 用于让 main 线程等待 goroutine 完成从服务器接收并正常关闭连接。

<-waitc // is a blocking operation. It can evaluate when channel been closed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-20
    • 2018-07-27
    • 2020-06-03
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 2018-09-23
    • 2019-05-10
    相关资源
    最近更新 更多