dou7466 2019-08-21 01:30 采纳率: 0%
浏览 490

在Go中关闭grpc服务器时如何检测错误?

I'm trying to detect the gRPC server shutdown in Go gRPC client code like

import (
     pb "mysample.com/api/rpc"
     "google.golang.org/grpc"
)
var stream pb.SearchProductService_MarketStreamClient
for {
    r, err := stream.Recv()
    if err == io.EOF {
        log.Println("Info: server close")
        break
    } else if err != nil {
        log.Println("Err: grpc code:", grpc.Code(err))
        if err != nil {
          log.Fatal(err)
        }
    }
    outs := printOrderbook(r)
    fmt.Println(outs)
    if outf != nil {
        outf.WriteString(time.Now().Format("030405.000 ") +
            outs + "
")
    }
}

The output message is

mysample.go:122: Err: grpc code: Unavailable
panic: rpc error: code = Unavailable desc = transport is closing

I'm trying to search 'transport is closing' in Go's source and $GOPATH/src/, but I can't not found such phrase. Is there any smart method to handle such deeper error?

  • 写回答

1条回答 默认 最新

  • dongqiao3833 2019-08-21 06:28
    关注

    gRPC always need to return errors properly, if you do not handle errors the connection will go into TRANSIENT_FAILURE state and you cannot request any query with this client connection. Catch the error properly and return it using grpc error codes. You can find deeper insites here https://www.grpc.io/docs/guides/error/ and use this inbuilt package grpc: google.golang.org/grpc/status

    conn *grpc.ClientConn conn.GetState() will give the current connection state

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度