From 3827605a36ca3f23248005e5232734e86f733822 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 15 Dec 2023 15:27:25 -0500 Subject: [PATCH] [BE] Set limits on DB connection lifetime --- backend/src/main.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/src/main.rs b/backend/src/main.rs index eff58f4..9777f7b 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -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());