clickhouse.rs icon indicating copy to clipboard operation
clickhouse.rs copied to clipboard

Where is the Pool of connections, and how to limit it.

Open rsalmei opened this issue 1 year ago • 3 comments

Hi, I'm trying to migrate https://github.com/suharev7/clickhouse-rs to use yours, but I cannot find where the Pool is. I want to have some connections always established, and need to keep at least one at all times, i.e. pool_min and pool_max.

Your docs say almost nothing about it, besides that one sentence "Reuse created clients or clone them in order to reuse a connection pool". Please, care to explain it better? How does a Pool work here? Thank you.

rsalmei avatar Nov 09 '22 19:11 rsalmei

Hi, this library uses hyper's HTTP pool. You can create a client on your own using hyper::client::Builder and pass it to clickhouse::Client::with_http_client. It should be noted in docs, I agree and will close the issue after adding it.

Hyper opens a new connection if all existing is already busy and keeps them alive at least pool_idle_timeout. This library uses 2s by default because a default setting in CH for inactive HTTP connections is 3s.

However, I believe the hyper's pool doesn't provide any pool_min and pool_max settings, only pool_max_idle_per_host, which specify how many connections with no active requests can live.

pool_max can be implemented on your side by using a semaphore. It's more flexible than restricting a connection pool because allows restricting only specific types of requests, e.g. SELECTs, but not INSERTs or something more specific depending on your tasks.

need to keep at least one at all times

Why do you need that?

loyd avatar Nov 10 '22 02:11 loyd

I see, thanks. I've never used hyper myself, so I'm not familiar with how it manages Pools. I'll try to take a look sometime.

I wanted pool_min and pool_max just because suharev7's provides them, and I've grown accustomed to using them:

pool_min - Lower bound of opened connections for Pool (defaults to 10).
pool_max - Upper bound of opened connections for Pool (defaults to 20).

That makes it simple to limit the number of CH connections, while still having some always available. And yes, I do agree a Semaphore would be more flexible, although the less new code I have to write to port my system, the sooner I could have it done...

Anyway, I like very much your query with .bind() support and the typed .fetch_all(), very cool features! But it seems I won't be able to migrate any time soon, until I can make sense of the hyper Pools, and develop this min/max limiter...

rsalmei avatar Nov 10 '22 05:11 rsalmei

It should be noted in docs, I agree and will close the issue after adding it.

I'm also interested. Code example would be super nice.

gd87429 avatar Nov 12 '22 13:11 gd87429