英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论