[BE] Set limits on DB connection lifetime

master
Araozu 2023-12-15 15:27:25 -05:00
parent fc411f4ec2
commit 3827605a36
1 changed files with 14 additions and 0 deletions

View File

@ -80,6 +80,20 @@ pub async fn init_db() -> Result<(), String> {
let start = Instant::now();
let pool = MySqlPoolOptions::new()
.max_connections(5)
/*
On some afternoons for some god forsaken reason the idle connections
to the db stay alive, but stop responding.
When this happens, we must restart the server, or wait for all
the active connections to timeout.
Here are some measures to circumvent that:
*/
// Set the maximum wait time for connections to 10 seconds
// In practice, the slowest connections take 1.5 seconds to connect
.acquire_timeout(std::time::Duration::from_secs(10))
// Set the maximum idle time for connections to 10 minutes
.idle_timeout(std::time::Duration::from_secs(10 * 60))
.connect(db_url.as_str())
.await;
log::info!("DB Pool connection took: {:?} ms", start.elapsed().as_millis());