Closed hoangtranwork closed 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.
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).
Why would min_idle have to be max_size almost all the time?
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.
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.
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)
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.
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.
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.