增加 Go 中的 gRPC 超时

问题描述 投票:0回答:1

有一些 gRPC 调用需要连接保持连接超过 5 分钟,该连接将处于空闲状态,直到操作完成。
我尝试了

keepalive
设置,甚至
context.WithTimeout()
,但 gRPC 连接在 5 分钟后超时(代码 = 未知描述 = 流超时”)

如何增加此类 gRPC 调用的空闲超时?

go grpc grpc-go
1个回答
3
投票

你尝试过吗

clientDeadline := time.Now().Add(time.Duration(*deadlineMs) * time.Millisecond)
ctx, cancel := context.WithDeadline(ctx, clientDeadline)

类似于gRPC官方文档中的定义。这是 https://grpc.io/blog/deadlines/ 链接。

编辑:

您基本上可以通过

Enforcement Policy
选项在服务器端保持活动状态,如 源代码中提到的那样

// EnforcementPolicy is used to set keepalive enforcement policy on the
// server-side. Server will close connection with a client that violates this
// policy.
type EnforcementPolicy struct {
    // MinTime is the minimum amount of time a client should wait before sending
    // a keepalive ping.
    MinTime time.Duration // The current default value is 5 minutes.
    // If true, server allows keepalive pings even when there are no active
    // streams(RPCs). If false, and client sends ping when there are no active
    // streams, server will send GOAWAY and close the connection.
    PermitWithoutStream bool // false by default.
}
© www.soinside.com 2019 - 2024. All rights reserved.