summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@atos.net>2021-01-12 09:55:06 +0100
committerMatthias Beyer <matthias.beyer@atos.net>2021-01-12 09:55:06 +0100
commit63f705792e1e5abf6b8fe9211a10815d3478e21a (patch)
treee0ef80485698bd7e61c0fec25d66b2f1f0b4f434
parent35691c3bd2911c9fa690c08a17012f89c9ee199b (diff)
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 <matthias.beyer@atos.net>
-rw-r--r--Cargo.toml2
-rw-r--r--src/source/mod.rs24
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