sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.49k stars 80 forks source link

Pool building freezes when connections drop during initial setup #142

Closed FlixCoder closed 1 year ago

FlixCoder commented 1 year ago

Hi!

I am using r2d2 0.8.10 and noticed, that my service gets stuck on startup consistently under certain conditions: When the database becomes available, while r2d2 is already trying to connect.

The problem is, that in the initial startup phase, all connections are reset until postgresql is then fully start up.

During the build of the pool, the builder waits until the specified amount of connections is reached. However, it only starts connecting the specified amount of connections, so if one of those is dropped during the waiting time, the builder infinitely waits.

I hope this was understandable and thank you!

PS: As a workaround, it seems to work to lower the connection_timeout, it somehow makes it way less likely to appear.

sfackler commented 1 year ago

The pool will retry after failed connection attemps: https://github.com/sfackler/r2d2/blob/master/src/lib.rs#L272-L278.

Lowering the connection timeout will increase the rate at which those retries will be made.

FlixCoder commented 1 year ago

Ah I see, then the freeze might stem from another reason, but it certainly freezes. It normally logs reconnection attempts, but there is no output for like 5 minutes, even though the connection timeout is 30 seconds. With 5 seconds it somehow errors, maybe the condition is just more difficult to occur.

sfackler commented 1 year ago

I don't think I can see how it's possible for initialization to take longer than the connection timeout. A self contained example of your issue would be helpful.

FlixCoder commented 1 year ago

Hmm, I am not able to reproduce it in the setup I expected it to happen. It seems to maybe not be related to the pool start up even, so it might be a diesel or my own issue, so closing this for now! Thank you for the quick response and sorry for any inconvenience.