1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-27 03:42:39 +00:00

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.
This commit is contained in:
Thomas O'Donnell 2020-10-17 21:36:21 +02:00 committed by GitHub
parent 95bb901b8a
commit 035e80bbd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,7 +11,7 @@ use std::ffi::OsString;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::string::String; 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. /// 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 /// 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<Self, std::io::Error> { fn from_path_with_timeout(base: &PathBuf, timeout: Duration) -> Result<Self, std::io::Error> {
let start = SystemTime::now(); let start = Instant::now();
let mut folders: HashSet<PathBuf> = HashSet::new(); let mut folders: HashSet<PathBuf> = HashSet::new();
let mut files: HashSet<PathBuf> = HashSet::new(); let mut files: HashSet<PathBuf> = HashSet::new();
@ -233,8 +233,8 @@ impl DirContents {
fs::read_dir(base)? fs::read_dir(base)?
.enumerate() .enumerate()
.take_while(|(n, _)| { .take_while(|(n, _)| {
n & 0xFF != 0 // only check SystemTime once every 2^8 entries n & 0xFF != 0 // only check timeout once every 2^8 entries
|| SystemTime::now().duration_since(start).unwrap() < timeout || start.elapsed() < timeout
}) })
.filter_map(|(_, entry)| entry.ok()) .filter_map(|(_, entry)| entry.ok())
.for_each(|entry| { .for_each(|entry| {
@ -255,7 +255,7 @@ impl DirContents {
log::trace!( log::trace!(
"Building HashSets of directory files, folders and extensions took {:?}", "Building HashSets of directory files, folders and extensions took {:?}",
SystemTime::now().duration_since(start).unwrap() start.elapsed()
); );
Ok(DirContents { Ok(DirContents {