From 63f705792e1e5abf6b8fe9211a10815d3478e21a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 12 Jan 2021 09:55:06 +0100 Subject: Fix: Spawn filesystem task on a blocking tokio thread This patch changes the (possibly heavy) File reading from disk to be executed on a blocking tokio thread. This way, we ensure we do not block until the `read()` operation is finished and can continue executing async tasks on the runtime. Because IO might be expensive (think of a 5GB file that is read to memory), this is a good optimization. Signed-off-by: Matthias Beyer --- Cargo.toml | 2 +- src/source/mod.rs | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ed3a4e4..8f3c7c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ shiplift = { git = "https://github.com/softprops/shiplift", rev = "03cc8c0 syntect = "4.4" tar = "0.4" terminal_size = "0.1" -tokio = { version = "0.2", features = ["macros", "fs", "process", "io-util"] } +tokio = { version = "0.2", features = ["macros", "fs", "process", "io-util", "blocking"] } toml = "0.5" typed-builder = "0.7" unindent = "0.1" diff --git a/src/source/mod.rs b/src/source/mod.rs index c2a5bb0..20671bc 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -81,18 +81,24 @@ impl SourceEntry { } pub async fn verify_hash(&self) -> Result<()> { - use std::io::Read; let p = self.source_file_path(); - trace!("Reading to buffer: {}", p.display()); - let mut buf = vec![]; - std::fs::OpenOptions::new() - .create(false) - .create_new(false) - .read(true) - .open(&p)? - .read_to_end(&mut buf)?; + + let path = p.clone(); + let buf = tokio::task::spawn_blocking(move || { + use std::io::Read; + + let mut buf = vec![]; + std::fs::OpenOptions::new() + .create(false) + .create_new(false) + .read(true) + .open(path)? + .read_to_end(&mut buf) + .map(|_| buf) + }) + .await??; trace!("Reading to buffer finished: {}", p.display()); self.package_source -- cgit v1.2.3