gpt4 book ai didi

Golang gRPC 无法保持事件状态 : the client connection is closing

转载 作者:行者123 更新时间:2023-12-03 10:08:41 46 4
gpt4 key购买 nike

嗨,我正在尝试将 gRPC 客户端连接到服务器,但即使连接成功,我在从 graphql 解析器查询它时也会收到以下错误。但是,如果我直接从解析器拨号,一切正常,因此它与客户端不保持连接打开有关。

rpc 错误:代码 = Canceled desc = grpc:客户端连接正在关闭

客户端

var kacp = keepalive.ClientParameters{
Time: 10 * time.Second, // send pings every 10 seconds if there is no activity
Timeout: time.Second, // wait 1 second for ping back
PermitWithoutStream: true, // send pings even without active streams
}

func gqlHandler() http.HandlerFunc {

conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure(),
grpc.WithKeepaliveParams(kacp),
grpc.WithBlock())

if err != nil {
panic(err)
}
defer conn.Close()

db := proto.NewPlatformDBClient(conn)

gh := handler.GraphQL(platform.NewExecutableSchema(platform.Config{Resolvers: &platform.Resolver{
DB: db,
}}))

gh = cors.Disable(gh)

return gh
}

最佳答案

它是因为defer conn.Close()命令将在使用连接之前执行。
From the go blog

A defer statement pushes a function call onto a list. The list ofsaved calls is executed after the surrounding function returns.


所以你会删除行 defer conn.Close()并在不再使用后关闭连接。

关于Golang gRPC 无法保持事件状态 : the client connection is closing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62441316/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com