about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db.rs20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/db.rs b/src/db.rs
index ee6f494..c6723ff 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,7 +1,7 @@
 //! Read-only access to the GeneNetwork MariaDB database (db_webqtl).
 
 use mysql::prelude::{FromRow, Queryable};
-use mysql::{params, Opts, Pool};
+use mysql::{params, Opts, OptsBuilder, Pool, PoolOpts};
 use serde::Serialize;
 use std::sync::{Mutex, OnceLock};
 
@@ -26,13 +26,29 @@ fn sql_uri() -> String {
 /// around an Option gives the same "build once" behavior while still
 /// letting a failed attempt be retried on the next call instead of
 /// wedging into a permanent error.
+///
+/// Two defaults are turned off because each costs a full round trip to
+/// the database per checkout, on top of the query: `check_health` (a
+/// ping when a connection is taken from the pool) and
+/// `reset_connection` (a COM_RESET_CONNECTION when it is returned).
+/// Together they tripled the cost of every database call against a
+/// database about 130 ms away (390 ms instead of 130 ms). Nothing here
+/// needs the reset (queries are read-only and keep no session state),
+/// and the ping only protects against a connection the server closed
+/// while idle -- which would now surface as a database error on the
+/// next query using it (the pool then drops that connection).
 fn pool() -> Result<Pool, mysql::Error> {
     static POOL: OnceLock<Mutex<Option<Pool>>> = OnceLock::new();
     let mut guard = POOL.get_or_init(|| Mutex::new(None)).lock().unwrap();
     if let Some(pool) = guard.as_ref() {
         return Ok(pool.clone());
     }
-    let new_pool = Pool::new(Opts::from_url(&sql_uri())?)?;
+    let opts = OptsBuilder::from_opts(Opts::from_url(&sql_uri())?).pool_opts(
+        PoolOpts::default()
+            .with_reset_connection(false)
+            .with_check_health(false),
+    );
+    let new_pool = Pool::new(opts)?;
     *guard = Some(new_pool.clone());
     Ok(new_pool)
 }