tower-rs/tower-grpc

Connection reuse/pooling

brndnmtthws opened this issue · 8 comments

Hey folks,

Great work on the project so far. I have a question I can't answer by reading the code: how is socket reuse & connection pooling supposed to work? If I have clients making calls to the same services over and over, I don't want to have to open a new socket every time. Furthermore, I want to avoid the TLS handshake on every API call.

I'm also not very familiar with tokio at this point, so it's possible that this is already happening and I simply don't understand. From the example code, however, it looks like a new connection is created for each API call.

I just did some more digging, and it looks like hyper correctly handles reuse/pooling. Presumably with #122 this will be resolved.

@brndnmtthws hi! You can use the tower-balance crate to load balance a set of connections, and I believe there is a dynamic pool in there. Hyper itself does some basic connection pooling but I believe its not as configurable as tower-balance is. If you create a client, you can then put it beind a tower-buffer and reuse that connection. Using something like tower-reconnect you can then reconnect that socket incase of any transport errors.

@LucioFranco that's helpful, thanks. Hopefully I can figure out how to make it work :)

Load balancing isn't much of a concern in my case, but reusing the connection definitely is.

@brndnmtthws yup, you should be able to reuse the connection very easily by using buffer and reconnect. Buffer adds clone to the client and reconnect allows you to set this connection to some address and it will reconnect on any issues.

This is probably the best example of general tower, which of course works with tower-grpc https://github.com/tower-rs/tower/blob/master/tower/examples/client.rs#L47

That's extremely helpful, thank you kindly!

It seems like the project is heading away from h2 and toward hyper, is that right?

I'm waiting on #157 to get merged so I can properly implementing connection pooling with tower & hyper.

@brndnmtthws hyper internally uses h2, tower-h2 was just a PoC transport for tower as we explored the api a bit.

@LucioFranco I appreciate the clarification. I will hold off until #157 is ready :)