refactor(build): reformat code and add tonic health dependency

- Reformatted build script with proper indentation and line breaks
- Added tonic-health dependency to Cargo.toml and updated lock file
- Improved error handling in disk cache with concurrent deletion checks
- Refactored conditional chains using && and let expressions
- Reformatted struct initialization and function parameter lists
- Added proper spacing and alignment in language stats processing
- Improved assertion formatting in test cases
- Reorganized import statements and code layout in multiple files
- Updated metrics functions with better parameter handling and formatting
This commit is contained in:
zhenyi
2026-06-11 13:56:15 +08:00
parent c32a7cad2f
commit a40da90ef9
31 changed files with 696 additions and 417 deletions
+30 -19
View File
@@ -271,10 +271,8 @@ impl DiskCache {
}
let start = std::time::Instant::now();
let path = self.cache_file_path(namespace, digest);
if !path.exists() {
crate::metrics::record_cache_op("disk", "miss", start.elapsed());
return Ok(None);
}
// Check expiry before reading, but handle concurrent deletion gracefully.
if let Ok(metadata) = std::fs::metadata(&path)
&& let Ok(modified) = metadata.modified()
&& let Ok(age) = SystemTime::now().duration_since(modified)
@@ -292,16 +290,26 @@ impl DiskCache {
crate::metrics::record_cache_op("disk", "expired", start.elapsed());
return Ok(None);
}
let data = std::fs::read(&path).map_err(GitError::Io)?;
tracing::debug!(
namespace = %namespace,
digest = %digest,
size = data.len(),
elapsed_ms = start.elapsed().as_millis() as u64,
"cache hit"
);
crate::metrics::record_cache_op("disk", "hit", start.elapsed());
Ok(Some(data))
match std::fs::read(&path) {
Ok(data) => {
tracing::debug!(
namespace = %namespace,
digest = %digest,
size = data.len(),
elapsed_ms = start.elapsed().as_millis() as u64,
"cache hit"
);
crate::metrics::record_cache_op("disk", "hit", start.elapsed());
Ok(Some(data))
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
// File was deleted between metadata check and read — treat as miss.
crate::metrics::record_cache_op("disk", "miss", start.elapsed());
Ok(None)
}
Err(e) => Err(GitError::Io(e)),
}
}
/// Insert a cached response for the given namespace and digest.
@@ -338,9 +346,8 @@ impl DiskCache {
return Ok(None);
}
let path = self.cache_file_path(namespace, digest);
if !path.exists() {
return Ok(None);
}
// Check expiry; handle concurrent deletion gracefully.
if let Ok(metadata) = std::fs::metadata(&path)
&& let Ok(modified) = metadata.modified()
&& let Ok(age) = SystemTime::now().duration_since(modified)
@@ -349,8 +356,12 @@ impl DiskCache {
std::fs::remove_file(&path).ok();
return Ok(None);
}
let file = std::fs::File::open(&path).map_err(GitError::Io)?;
Ok(Some(file))
match std::fs::File::open(&path) {
Ok(file) => Ok(Some(file)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(GitError::Io(e)),
}
}
/// Open a cache file for streaming write.