From f1284fc950ef977d6a462bc39e3b2a8e6f8291da Mon Sep 17 00:00:00 2001 From: Bastien Orivel Date: Wed, 22 May 2024 00:59:10 +0200 Subject: [PATCH] Use WAL mode for sqlite Hopefully this should help with performance --- .gitignore | 2 +- src/main.rs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7c8172d..198cb44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ /target -/db.sqlite +/db.sqlite* /Rocket.toml /.env /apworlds diff --git a/src/main.rs b/src/main.rs index 30389ee..56eb7c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use db::{DbInstrumentation, QUERY_HISTOGRAM}; +use diesel::connection::SimpleConnection; use diesel::r2d2::Pool; use diesel::sqlite::Sqlite; use diesel::{r2d2::ConnectionManager, SqliteConnection}; @@ -114,6 +115,19 @@ impl From> for Vec { struct AdminToken(String); struct APWorldPath(PathBuf); +#[derive(Debug)] +struct SqliteCustomizer; + +impl diesel::r2d2::CustomizeConnection for SqliteCustomizer { + fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> { + conn.batch_execute( + "PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA busy_timeout = 1000;", + ) + .map_err(diesel::r2d2::Error::QueryError)?; + Ok(()) + } +} + #[rocket::main] async fn main() -> anyhow::Result<()> { dotenv().ok(); @@ -127,7 +141,10 @@ async fn main() -> anyhow::Result<()> { .expect("Failed to set diesel instrumentation"); let manager = ConnectionManager::::new(db_url); - let db_pool = Pool::new(manager).expect("Failed to create database pool, aborting"); + let db_pool = Pool::builder() + .connection_customizer(Box::new(SqliteCustomizer)) + .build(manager) + .expect("Failed to create database pool, aborting"); { let mut connection = db_pool .get()