From 035e80bbd3ba83ed97ff0c86308d20547f1ac311 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Sat, 17 Oct 2020 21:36:21 +0200 Subject: [PATCH] chore(context): Use monotonic clock for timeout (#1802) Have switched to use a monotonic clock for calculating the timeout when indexing the current directory for the context to avoid any issues with calculating the timeout when the systems clock might change. --- src/context.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/context.rs b/src/context.rs index de7b901d..3f78375a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -11,7 +11,7 @@ use std::ffi::OsString; use std::fs; use std::path::{Path, PathBuf}; use std::string::String; -use std::time::{Duration, SystemTime}; +use std::time::{Duration, Instant}; /// Context contains data or common methods that may be used by multiple modules. /// The data contained within Context will be relevant to this particular rendering @@ -223,7 +223,7 @@ impl DirContents { } fn from_path_with_timeout(base: &PathBuf, timeout: Duration) -> Result { - let start = SystemTime::now(); + let start = Instant::now(); let mut folders: HashSet = HashSet::new(); let mut files: HashSet = HashSet::new(); @@ -233,8 +233,8 @@ impl DirContents { fs::read_dir(base)? .enumerate() .take_while(|(n, _)| { - n & 0xFF != 0 // only check SystemTime once every 2^8 entries - || SystemTime::now().duration_since(start).unwrap() < timeout + n & 0xFF != 0 // only check timeout once every 2^8 entries + || start.elapsed() < timeout }) .filter_map(|(_, entry)| entry.ok()) .for_each(|entry| { @@ -255,7 +255,7 @@ impl DirContents { log::trace!( "Building HashSets of directory files, folders and extensions took {:?}", - SystemTime::now().duration_since(start).unwrap() + start.elapsed() ); Ok(DirContents {