gRPC 连接:使用 keepAlive 还是 idleTimeout?

gRPC connection: use keepAlive or idleTimeout?

查看 gRPC Java 文档 - ManagedChannelBuilder,有两个选项可以管理连接。 idleTimeout() 似乎是 default/preferred 配置。但是当我试图搜索比较时,大多数帖子都在谈论 keepAlive 选项。

我很好奇常见的做法是什么,这两种选择的优缺点是什么?

idleTimeout

Set the duration without ongoing RPCs before going to idle mode. In idle mode the channel shuts down all connections, the NameResolver and the LoadBalancer. A new RPC would take the channel out of idle mode. A channel starts in idle mode. Defaults to 30 minutes.

This is an advisory option. Do not rely on any specific behavior related to this option.

keepAliveWithoutCalls

Sets whether keepalive will be performed when there are no outstanding RPC on a connection. Defaults to false.

Clients must receive permission from the service owner before enabling this option. Keepalives on unused connections can easilly accidentally consume a considerable amount of bandwidth and CPU. idleTimeout() should generally be used instead of this option.

在 RPC 进行过程中使用 keepalive 通知连接失败。使用 idleTimeout 释放资源并防止空闲 TCP 连接在通道未使用时断开。

idleTimeout 优于 keepAliveWithoutCalls,因为它倾向于减少系统的整体负载。当您愿意花费客户端、服务器和网络资源来为非常不频繁的 RPC 降低延迟时,使用 keepAliveWithoutCalls。