Refactor: cascade of matches in cache system
This commit is contained in:
@@ -150,28 +150,27 @@ pub mod conf {
|
|||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SELF_MTIME: String = {
|
static ref SELF_MTIME: String = {
|
||||||
match std::env::current_exe() {
|
std::env::current_exe()
|
||||||
Ok(path) => match fs::metadata(&path) {
|
.map_err(|_| warn!("Failed to get path of current executable"))
|
||||||
Ok(metadata) => match metadata.modified() {
|
.ok()
|
||||||
Ok(mtime) => match mtime.duration_since(std::time::UNIX_EPOCH) {
|
.and_then(|path| {
|
||||||
Ok(duration) => format!("{}", duration.as_millis()),
|
fs::metadata(&path)
|
||||||
Err(err) => format!("m{}", err.duration().as_millis()),
|
.map_err(|_| warn!("Failed to get metadata of current executable"))
|
||||||
},
|
.ok()
|
||||||
Err(_) => {
|
})
|
||||||
warn!("Failed to get modification time of current executable");
|
.and_then(|metadata| {
|
||||||
"no-mtime".to_string()
|
metadata
|
||||||
}
|
.modified()
|
||||||
},
|
.map_err(|_| warn!("Failed to get metadata of current executable"))
|
||||||
Err(_) => {
|
.ok()
|
||||||
warn!("Failed to get metadata of current executable");
|
})
|
||||||
"no-mtime".to_string()
|
.and_then(|mtime| {
|
||||||
}
|
Some(match mtime.duration_since(std::time::UNIX_EPOCH) {
|
||||||
},
|
Ok(duration) => format!("{}", duration.as_millis()),
|
||||||
Err(_) => {
|
Err(err) => format!("m{}", err.duration().as_millis()),
|
||||||
warn!("Failed to get path of current executable");
|
})
|
||||||
"no-mtime".to_string()
|
})
|
||||||
}
|
.unwrap_or("no-mtime".to_string())
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,87 +240,67 @@ impl ModuleCacheEntry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_data(&self) -> Option<ModuleCacheData> {
|
pub fn get_data(&self) -> Option<ModuleCacheData> {
|
||||||
if let Some(p) = &self.mod_cache_path {
|
let path = self.mod_cache_path.as_ref()?;
|
||||||
match fs::read(p) {
|
let compressed_cache_bytes = fs::read(path).ok()?;
|
||||||
Ok(compressed_cache_bytes) => match zstd::decode_all(&compressed_cache_bytes[..]) {
|
let cache_bytes = zstd::decode_all(&compressed_cache_bytes[..])
|
||||||
Ok(cache_bytes) => match bincode::deserialize(&cache_bytes[..]) {
|
.map_err(|err| warn!("Failed to decompress cached code: {}", err))
|
||||||
Ok(data) => Some(data),
|
.ok()?;
|
||||||
Err(err) => {
|
bincode::deserialize(&cache_bytes[..])
|
||||||
warn!("Failed to deserialize cached code: {}", err);
|
.map_err(|err| warn!("Failed to deserialize cached code: {}", err))
|
||||||
None
|
.ok()
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(err) => {
|
|
||||||
warn!("Failed to decompress cached code: {}", err);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(_) => None,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_data(&self, data: &ModuleCacheData) {
|
pub fn update_data(&self, data: &ModuleCacheData) {
|
||||||
if let Some(p) = &self.mod_cache_path {
|
let _ = self.update_data_impl(data);
|
||||||
let cache_buf = match bincode::serialize(&data) {
|
}
|
||||||
Ok(data) => match zstd::encode_all(&data[..], conf::compression_level()) {
|
|
||||||
Ok(data) => data,
|
fn update_data_impl(&self, data: &ModuleCacheData) -> Option<()> {
|
||||||
Err(err) => {
|
let path = self.mod_cache_path.as_ref()?;
|
||||||
warn!("Failed to compress cached code: {}", err);
|
let serialized_data = bincode::serialize(&data)
|
||||||
return;
|
.map_err(|err| warn!("Failed to serialize cached code: {}", err))
|
||||||
}
|
.ok()?;
|
||||||
},
|
let compressed_data = zstd::encode_all(&serialized_data[..], conf::compression_level())
|
||||||
Err(err) => {
|
.map_err(|err| warn!("Failed to compress cached code: {}", err))
|
||||||
warn!("Failed to serialize cached code: {}", err);
|
.ok()?;
|
||||||
return;
|
|
||||||
}
|
// Optimize syscalls: first, try writing to disk. It should succeed in most cases.
|
||||||
};
|
// Otherwise, try creating the cache directory and retry writing to the file.
|
||||||
// Optimize syscalls: first, try writing to disk. It should succeed in most cases.
|
let err = fs::write(path, &compressed_data).err()?; // return on success
|
||||||
// Otherwise, try creating the cache directory and retry writing to the file.
|
debug!(
|
||||||
match fs::write(p, &cache_buf) {
|
"Attempting to create the cache directory, because \
|
||||||
Ok(()) => (),
|
failed to write cached code to disk, path: {}, message: {}",
|
||||||
Err(err) => {
|
path.display(),
|
||||||
debug!(
|
err,
|
||||||
"Attempting to create the cache directory, because \
|
);
|
||||||
failed to write cached code to disk, path: {}, message: {}",
|
|
||||||
p.display(),
|
let cache_dir = path.parent().unwrap();
|
||||||
err,
|
fs::create_dir_all(cache_dir)
|
||||||
|
.map_err(|err| {
|
||||||
|
warn!(
|
||||||
|
"Failed to create cache directory, path: {}, message: {}",
|
||||||
|
cache_dir.display(),
|
||||||
|
err
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.ok()?;
|
||||||
|
|
||||||
|
let err = fs::write(path, &compressed_data).err()?;
|
||||||
|
warn!(
|
||||||
|
"Failed to write cached code to disk, path: {}, message: {}",
|
||||||
|
path.display(),
|
||||||
|
err
|
||||||
|
);
|
||||||
|
fs::remove_file(path)
|
||||||
|
.map_err(|err| {
|
||||||
|
if err.kind() != io::ErrorKind::NotFound {
|
||||||
|
warn!(
|
||||||
|
"Failed to cleanup invalid cache, path: {}, message: {}",
|
||||||
|
path.display(),
|
||||||
|
err
|
||||||
);
|
);
|
||||||
let cache_dir = p.parent().unwrap();
|
|
||||||
match fs::create_dir_all(cache_dir) {
|
|
||||||
Ok(()) => match fs::write(p, &cache_buf) {
|
|
||||||
Ok(()) => (),
|
|
||||||
Err(err) => {
|
|
||||||
warn!(
|
|
||||||
"Failed to write cached code to disk, path: {}, message: {}",
|
|
||||||
p.display(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
match fs::remove_file(p) {
|
|
||||||
Ok(()) => (),
|
|
||||||
Err(err) => {
|
|
||||||
if err.kind() != io::ErrorKind::NotFound {
|
|
||||||
warn!(
|
|
||||||
"Failed to cleanup invalid cache, path: {}, message: {}",
|
|
||||||
p.display(),
|
|
||||||
err
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Err(err) => warn!(
|
|
||||||
"Failed to create cache directory, path: {}, message: {}",
|
|
||||||
cache_dir.display(),
|
|
||||||
err
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
.ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user