diff options
author | Matthias Beyer <matthias.beyer@atos.net> | 2021-01-12 09:55:06 +0100 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@atos.net> | 2021-01-12 09:55:06 +0100 |
commit | 63f705792e1e5abf6b8fe9211a10815d3478e21a (patch) | |
tree | e0ef80485698bd7e61c0fec25d66b2f1f0b4f434 /src/source | |
parent | 35691c3bd2911c9fa690c08a17012f89c9ee199b (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>
Diffstat (limited to 'src/source')
-rw-r--r-- | src/source/mod.rs | 24 |
1 files changed, 15 insertions, 9 deletions
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 |