refactor(cache): redesign cache system with structured keys and improved performance
- Add repo_path parameter to cached_response and cached_vec_response functions - Implement structured cache key format with namespace, repo_path, and request proto - Replace global cache with Moka in-memory cache using weight-based eviction - Set 256MB memory cap with 10-minute TTL and 2-minute TTI policy - Add metrics collection for cache operations and evictions - Implement efficient repo-scoped invalidation using key structure - Add detailed documentation comments explaining cache architecture - Remove outdated dependencies and update dependency versions - Add error handling for encoding failures in cache operations - Optimize Vec responses with length-delimited encoding and pre-allocation
This commit is contained in:
+19
-4
@@ -7,6 +7,8 @@ use tokio_stream::wrappers::ReceiverStream;
|
||||
use crate::bare::GitBare;
|
||||
use crate::pb::PackfileChunk;
|
||||
|
||||
const MAX_PACK_OBJECTS_STDERR_BYTES: u64 = 64 * 1024;
|
||||
|
||||
impl GitBare {
|
||||
/// Pack objects using git-pack-objects --stdout.
|
||||
///
|
||||
@@ -52,7 +54,15 @@ impl GitBare {
|
||||
if opts.is_some_and(|o| o.delta_base_offset) {
|
||||
args.push("--delta-base-offset".into());
|
||||
}
|
||||
let stdin_data = generate_pack_input(&request);
|
||||
let stdin_data = match generate_pack_input(&request) {
|
||||
Ok(data) => data,
|
||||
Err(err) => {
|
||||
let _ = tx
|
||||
.send(Err(tonic::Status::invalid_argument(err.to_string())))
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut child = match Command::new("git")
|
||||
.args(&args)
|
||||
@@ -118,7 +128,8 @@ impl GitBare {
|
||||
let stderr_task = {
|
||||
let tx = tx.clone();
|
||||
async move {
|
||||
if let Some(mut stderr) = stderr.take() {
|
||||
if let Some(stderr) = stderr.take() {
|
||||
let mut stderr = stderr.take(MAX_PACK_OBJECTS_STDERR_BYTES);
|
||||
let mut s = String::new();
|
||||
if stderr.read_to_string(&mut s).await.is_ok() && !s.is_empty() {
|
||||
let _ = tx.send(Err(tonic::Status::internal(s))).await;
|
||||
@@ -150,15 +161,19 @@ impl GitBare {
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_pack_input(req: &crate::pb::PackObjectsRequest) -> Vec<u8> {
|
||||
fn generate_pack_input(
|
||||
req: &crate::pb::PackObjectsRequest,
|
||||
) -> Result<Vec<u8>, crate::error::GitError> {
|
||||
let mut input = String::new();
|
||||
if let Some(opts) = req.options.as_ref() {
|
||||
for want in &opts.wants {
|
||||
crate::sanitize::validate_oid_hex(&want.hex)?;
|
||||
input.push_str(&format!("{}\n", want.hex));
|
||||
}
|
||||
for have in &opts.haves {
|
||||
crate::sanitize::validate_oid_hex(&have.hex)?;
|
||||
input.push_str(&format!("^{}\n", have.hex));
|
||||
}
|
||||
}
|
||||
input.into_bytes()
|
||||
Ok(input.into_bytes())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user