From 32ceddcc0311ba1767fbdfe9a4f2d1fd7512fc55 Mon Sep 17 00:00:00 2001 From: NetworkCats <210240595+NetworkCats@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:26:52 +0800 Subject: [PATCH] Load the database off the async runtime Loading an MMDB reads the whole ~100 MB file with a blocking std::fs::read. The updater did that directly on a tokio worker, and the 1 vCPU production host runs a single worker, so every scheduled update stalled all in-flight requests until the load finished. With one request every 2 ms on a single-worker runtime, the worst latency during a reload was 31 ms; it is 1.7 ms after this change. Run the load on the blocking thread pool, both for scheduled updates and for the startup load in init_db. Co-Authored-By: Claude Opus 5.5 --- src/updater.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/updater.rs b/src/updater.rs index d8257e7..96cdff1 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -40,7 +40,7 @@ pub async fn init_db( update_url: &str, ) -> Result<(), Box> { let reader = if db_path.exists() { - load_db(db_path)? + validate_db_off_runtime(db_path).await? } else { if let Some(parent) = db_path.parent() { fs::create_dir_all(parent).await?; @@ -66,7 +66,7 @@ async fn download_db( return Err(e); } - let reader = match validate_db(&tmp_path) { + let reader = match validate_db_off_runtime(&tmp_path).await { Ok(r) => r, Err(e) => { let _ = fs::remove_file(&tmp_path).await; @@ -105,6 +105,16 @@ fn validate_db(path: &Path) -> Result Result> { + let path = path.to_owned(); + tokio::task::spawn_blocking(move || validate_db(&path)).await? +} + pub fn spawn_updater( shared_db: SharedDb, db_path: PathBuf,