Reputation: 609
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?
Upvotes: 6
Views: 6733
Reputation: 26414
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.
Upvotes: 3