sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.51k stars 82 forks source link

Is there a way to not keep idle connections? #115

Closed hoangtranwork closed 3 years ago

hoangtranwork commented 3 years ago

It seems that establish_idle_connections() is called on drops (and some other places):

https://github.com/sfackler/r2d2/blob/711af4b38fd68a9be2c6a5a0465f2c2002f5b87a/src/lib.rs#L193

So r2d2 always have certain number of (idle) connections, and the setting idle_timeout is not very useful. Could you please share why is it decided that way? Thank you.

sfackler commented 3 years ago

The minimum number of connections in the pool is controlled by https://docs.rs/r2d2/0.8.9/r2d2/struct.Builder.html#method.min_idle.

Generally speaking, the entire point of a connection pool is to keep connections around between uses so they're immediately available next time a request comes in.

hoangtranwork commented 3 years ago

Thank you. min_idle is fine for "keeping x number of connections around". It'd be nice though that x doesn't have to be max_size almost all the time. It'd help in an environment where the DB is shared and prefer clients to play nice instead of keeping too many connections at all time (many of which are idle).

sfackler commented 3 years ago

Why would min_idle have to be max_size almost all the time?

hoangtranwork commented 3 years ago

Sorry I didn't make it clear. I was just describing what r2d2 is doing currently. Let use an example: if we set min_idle=1 and max_size=10, then r2d2 will not only maintain at minimum 1 idle connection (which is great) but it also try to maintain exactly 10 connections at almost all time (which is not very flexible). I think with this config, the behavior should be: A. keep at least 1 idle connection, and B. open at max 10 connections, and C. do not pro-actively try to open more idle connections than min_idle

Please correct me if my observation is wrong. Thank you.

sfackler commented 3 years ago

What specific conditions do you mean when you refer to "at almost all time"? If there is no usage of the pool, it will tend towards having exactly min_idle connections in it. If a single thread is using the pool, that will result in min_idle + 1 connections existing.

hoangtranwork commented 3 years ago

From what I have tried, one case is: when a connection reaches idle_timeout, it is dropped, but a new idle connection is created right away filling the place.

From what I understand from the code, it does that in every case (during drop_conn, establish_idle_connection is called - as referenced in the opening post above)

sfackler commented 3 years ago

establish_idle_connections only adds connections up to min_idle. If the pool is already larger than min_idle, establish_idle_connections does not create new connections.

hoangtranwork commented 3 years ago

ohhhhhhh yes, I see why, the default value for min_idle is equal to max_size. I'm sorry I missed this. I thought default value is 0 :D It seems to be a weird default choice though. Anyway I think we can close this issue. Thank you very much.