Sporadic : Error: pool timed out while waiting for an open connection

I keep getting this every few deploys, seems like the connection to the DB is very unreliable.
15 Replies
Percy
Percy10mo ago
Project ID: b58d71f6-d2f6-4c69-ba03-9b155d79a099
Ohad @ BlockMesh Network
b58d71f6-d2f6-4c69-ba03-9b155d79a099 It seems to get fixed if I redeploy the same build, restarting isn't enough
Brody
Brody10mo ago
at what point in your apps lifecycle do you get this error?
Ohad @ BlockMesh Network
After deployment , trying to connect to the DB on startup. It happens 1:5 deploys or less.
Brody
Brody10mo ago
this is postgres right?
Brody
Brody10mo ago
show me the code that connects to the database?
Ohad @ BlockMesh Network
impl DatabaseSettings {
pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
PgSslMode::Prefer
};

PgConnectOptions::new()
.host(&self.host)
.port(self.port)
.ssl_mode(ssl_mode)
.username(&self.username)
.password(&self.password)
}

pub fn with_db(&self) -> PgConnectOptions {
let mut options = self.without_db().database(&self.name);
options.log_statements(log::LevelFilter::Trace);
options
}
}
impl DatabaseSettings {
pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
PgSslMode::Prefer
};

PgConnectOptions::new()
.host(&self.host)
.port(self.port)
.ssl_mode(ssl_mode)
.username(&self.username)
.password(&self.password)
}

pub fn with_db(&self) -> PgConnectOptions {
let mut options = self.without_db().database(&self.name);
options.log_statements(log::LevelFilter::Trace);
options
}
}
pub fn get_connection_pool(settings: &DatabaseSettings) -> PgPool {
PgPoolOptions::new()
.acquire_timeout(Duration::from_secs(5))
.connect_lazy_with(settings.with_db())
}
pub fn get_connection_pool(settings: &DatabaseSettings) -> PgPool {
PgPoolOptions::new()
.acquire_timeout(Duration::from_secs(5))
.connect_lazy_with(settings.with_db())
}
Brody
Brody10mo ago
well you already have a timeout of 5 seconds, that's what i was gonna suggest you add, so the next suggestion would be to add a retry mechanism
Ohad @ BlockMesh Network
Error: pool timed out while waiting for an open connection
Error: pool timed out while waiting for an open connection
Brody
Brody10mo ago
try implementing a retry mechanism
Ohad @ BlockMesh Network
it repeats and on restart is the same, only redeploy sorts it
Brody
Brody10mo ago
try implementing a retry mechanism in code
Ohad @ BlockMesh Network
i pass my docker the DATABSE_URL, do I need to explicitly expose the database PORT? if a restart doesn't help, not seeing why a code based retry will be different.
Brody
Brody10mo ago
they are completely different things, railways restart mechanism restarts the whole docker container let me know how this goes!