7

TL;DR

Does grpc-java's ManagedChannel have an implicit connection pool or is the pooling of ManagedChannel instances the responsibility of the user?


So, I am using java grpc 1.1.2 with protoc 3.2.0. It seems to me that there's no implicit support (as of now) for connection pooling that grpc provides for clients. It seems, however, that the abstraction of a connection in grpc, i.e. the ManagedChannel object does indeed work with multiple TCP connections. Is that correct? If so, does the ManagedChannel come with connection pooling along with it? If that is the case, I probably don't have to worry about the connection pooling, given that the channel is thread-safe and I can simply use a single ManagedChannel instance across my client. However, I might indeed have to pool these channel objects too for greater throughput if need be. Is there such an implementation (pooling of channels) that does this for me in grpc itself?

gravetii
  • 9,273
  • 9
  • 56
  • 75

2 Answers2

14

Yes, ManagedChannel does the connection pooling, and you only need one. It will automatically create and destroy connections as they are needed.

Carl Mastrangelo
  • 5,970
  • 1
  • 28
  • 37
  • Hi there, is there any source for this statement? Thanks. – Jorge Campos Dec 11 '18 at 00:12
  • 8
    I am a gRPC core contributor and am authoritative on the subject. – Carl Mastrangelo Jan 24 '19 at 00:26
  • Hi Carl, thank you for your answer, given my tests and current understanding on how gRPC works I know that you are right and I believe on you. Although for a formal documentation for a system it would be nice to have your/this statement in a documentation page so we could link to it :) – Jorge Campos Jan 24 '19 at 18:21
  • If you send a PR with where you would expect this to be stated I can review it. Unfortunately since I I know how the system works, I don't know where users look for information first. (I assume SO would be that place!) – Carl Mastrangelo Jan 24 '19 at 23:05
  • 1
    @CarlMastrangelo What are the configuration options of `ManagedChannel`, and where are those documented? – Abhijit Sarkar Jun 12 '19 at 18:24
  • 2
    @CarlMastrangelo: Hey, your statement seems to be different than one made by Eric. Please see his comment in https://stackoverflow.com/questions/58764891/grpc-make-high-throughput-client-in-java-scala – anhldbk Dec 16 '19 at 09:12
  • Does every request will create a new Connection? How can I limit connection count – pain Jul 06 '21 at 05:04
6

Since you said you wanted pooling for greater throughput, I assume you want to create and pool multiple connections for one address in a channel. It was not supported, as the channel impl used to create only one connection per address. With the LBv2 which will soon supercede the old version, it's now possible with a custom LoadBalancer in which you can create as many Subchannels for a connection as you wish.

You can refer to the stock pick-first and round-robin LoadBalancers on how to write your own LoadBalancer.

Kun Zhang
  • 599
  • 2
  • 5
  • i've inspected interface (for create statement) and 'm confused. actually we need a logical group of connections to the same address (for example this is a service within a mesh, so pods already balanced), not subchannel per EquivalentAddressGroup. – Simon Logic Feb 13 '23 at 19:23