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:
zhenyi
2026-06-12 12:53:23 +08:00
parent a40da90ef9
commit 934858bebf
82 changed files with 1273 additions and 4969 deletions
+19 -4
View File
@@ -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())
}
+7 -1
View File
@@ -12,6 +12,8 @@ use crate::pb::ReceivePackResponse;
/// Maximum time allowed for a git receive-pack process before it is killed.
const RECEIVE_PACK_TIMEOUT: Duration = Duration::from_secs(1800); // 30 minutes
const MAX_RECEIVE_PACKET_BYTES: usize = 16 * 1024 * 1024;
const MAX_RECEIVE_STDERR_BYTES: u64 = 64 * 1024;
impl GitBare {
/// Receive pack data using git-receive-pack with true concurrent streaming.
@@ -85,6 +87,9 @@ impl GitBare {
}
match result {
Ok(req) => {
if req.packet.len() > MAX_RECEIVE_PACKET_BYTES {
break;
}
if stdin.write_all(&req.packet).await.is_err() {
break;
}
@@ -134,7 +139,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_RECEIVE_STDERR_BYTES);
let mut s = String::new();
if stderr.read_to_string(&mut s).await.is_ok() && !s.is_empty() {
let _ = tx
+7 -1
View File
@@ -12,6 +12,8 @@ use crate::pb::UploadPackResponse;
/// Maximum time allowed for a git upload-pack process before it is killed.
const UPLOAD_PACK_TIMEOUT: Duration = Duration::from_secs(600); // 10 minutes
const MAX_UPLOAD_PACKET_BYTES: usize = 16 * 1024 * 1024;
const MAX_UPLOAD_STDERR_BYTES: u64 = 64 * 1024;
impl GitBare {
/// Upload pack data using git-upload-pack with true concurrent streaming.
@@ -87,6 +89,9 @@ impl GitBare {
}
match result {
Ok(req) => {
if req.packet.len() > MAX_UPLOAD_PACKET_BYTES {
break;
}
if stdin.write_all(&req.packet).await.is_err() {
break;
}
@@ -137,7 +142,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_UPLOAD_STDERR_BYTES);
let mut s = String::new();
if stderr.read_to_string(&mut s).await.is_ok() && !s.is_empty() {
let _ = tx