Skip to content

Commit

Permalink
fix: remove optimize from connect
Browse files Browse the repository at this point in the history
sqlx uses aquire timeout for connect so this was taking too long on a big database
  • Loading branch information
dav1do committed Dec 2, 2024
1 parent 9613878 commit 5f1f615
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
28 changes: 18 additions & 10 deletions one/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,26 @@ fn spawn_database_optimizer(
mut shutdown: tokio::sync::broadcast::Receiver<()>,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
match sqlite_pool.run_statement("PRAGMA optimize=0x10002").await {
Ok(_) => {
info!("successfully ran sqlite optimize=0x10002");
}
Err(e) => {
warn!(error=?e, "failed to run initial database optimize statement");
}
}
loop {
let mut duration = std::time::Duration::from_secs(60 * 60 * 24); // once daily
let mut interval = tokio::time::interval(duration);
interval.tick().await; // first tick is immediate
tokio::select! {
_ = shutdown.recv() => {
break;
}
_ = interval.tick() => {
// start the loop over
}
}
match sqlite_pool.optimize().await {
Ok(_) => {
info!("successfully executed database optimize");
Expand All @@ -337,16 +355,6 @@ fn spawn_database_optimizer(
);
}
}
let mut interval = tokio::time::interval(duration);
interval.tick().await; // first tick is immediate
tokio::select! {
_ = shutdown.recv() => {
break;
}
_ = interval.tick() => {
// start the loop over
}
}
}
})
}
Expand Down
7 changes: 1 addition & 6 deletions sql/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@ impl SqlitePool {
.foreign_keys(true);

let ro_opts = conn_opts.clone().read_only(true);
let write_opts = conn_opts
// Recommended practice is that applications with long-lived database connections should run "PRAGMA optimize=0x10002"
// when the database connection first opens, then run "PRAGMA optimize" again at periodic intervals - perhaps once per day
// https://www.sqlite.org/pragma.html#pragma_optimize
.pragma("optimize", "0x10002");

let writer = SqlitePoolOptions::new()
.min_connections(1)
.max_connections(1)
.acquire_timeout(std::time::Duration::from_secs(5))
.connect_with(write_opts)
.connect_with(conn_opts)
.await?;
let reader = SqlitePoolOptions::new()
.min_connections(1)
Expand Down

0 comments on commit 5f1f615

Please sign in to comment.