在GRPC中,什么时候应该关闭通道?

huangapple go评论72阅读模式
英文:

When should a channel be closed on GRPC?

问题

ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
.usePlaintext()
.build();

在 GRPC 服务器和客户端之间存在连接。托管通道提供了连接。托管通道应该在什么时候关闭?还是应该在服务器关闭之前保持打开状态?关于这个问题有什么最佳实践?

英文:
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8081)
            .usePlaintext()
            .build();

There is connection between GRPC server and client. The managed channel provide connection. When managed channel must be closed ? Or It should be open until server is shutdown? Whats the best practice about it?

答案1

得分: 3

保持通道活动,直到您不再需要。这通常是整个应用程序的生命周期。

由于通道保持与服务器的连接,因此不应频繁关闭/重新创建它。在应用程序启动时早期创建必要的通道,然后根据需要使用它们是正常的做法。

通道开始于空闲模式,其中没有连接。当执行RPC时,它们会连接并保持这些连接,但如果未使用,最终会返回空闲状态。您可以配置 channelBuilder.idleTimeout() 来选择在未使用时释放资源的程度。

英文:

Keep the channel alive as long as you need it. That is commonly the entire application's lifetime.

Since the channel holds the connections to the servers, it should not be shutdown/recreated frequently. It's normal to create the necessarily channels early in your application's startup and then just use them as necessary.

Channels start in an idle mode which has no connections. When you perform RPCs they connect and keep those connections, but will eventually go back into idle if unused. You can configure channelBuilder.idleTimeout() to choose how aggressively they release their resources when unused.

huangapple
  • 本文由 发表于 2020年9月29日 20:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定