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
+86 -34
View File
@@ -122,12 +122,15 @@ impl GitksService {
pub fn cleanup_route_cache(&self) {
let before = self.route_cache.len();
self.route_cache.retain(|_key, cached| {
cached.created_at.elapsed() < ROUTE_CACHE_TTL
});
self.route_cache
.retain(|_key, cached| cached.created_at.elapsed() < ROUTE_CACHE_TTL);
let removed = before - self.route_cache.len();
if removed > 0 {
tracing::debug!(removed, remaining = self.route_cache.len(), "route cache cleaned");
tracing::debug!(
removed,
remaining = self.route_cache.len(),
"route cache cleaned"
);
}
}
@@ -388,9 +391,10 @@ impl GitksService {
&self,
command: crate::actor::raft_log::Command,
) -> Result<(), tonic::Status> {
let actor = self.node_actor.as_ref().ok_or_else(|| {
tonic::Status::failed_precondition("node actor not initialized")
})?;
let actor = self
.node_actor
.as_ref()
.ok_or_else(|| tonic::Status::failed_precondition("node actor not initialized"))?;
// Send the command to the actor for Raft processing
let result = ractor::call_t!(
@@ -405,7 +409,9 @@ impl GitksService {
if success {
Ok(())
} else {
Err(tonic::Status::aborted("Raft consensus failed: not leader or timeout"))
Err(tonic::Status::aborted(
"Raft consensus failed: not leader or timeout",
))
}
}
Err(e) => Err(tonic::Status::internal(format!("Raft write error: {e}"))),
@@ -415,20 +421,16 @@ impl GitksService {
/// Perform a ReadIndex check to ensure this node can serve consistent reads.
/// This confirms the Leader is still valid before reading from local state.
pub async fn raft_read_index(&self) -> Result<(), tonic::Status> {
let actor = self.node_actor.as_ref().ok_or_else(|| {
tonic::Status::failed_precondition("node actor not initialized")
})?;
let actor = self
.node_actor
.as_ref()
.ok_or_else(|| tonic::Status::failed_precondition("node actor not initialized"))?;
let request = crate::actor::message::ReadIndexRequest {
relative_path: String::new(),
};
let result = ractor::call_t!(
actor,
GitNodeMessage::ReadIndex,
5000,
request
);
let result = ractor::call_t!(actor, GitNodeMessage::ReadIndex, 5000, request);
match result {
Ok(response) => {
@@ -436,7 +438,7 @@ impl GitksService {
Ok(())
} else {
Err(tonic::Status::failed_precondition(
"not leader, cannot serve consistent read"
"not leader, cannot serve consistent read",
))
}
}
@@ -649,23 +651,73 @@ pub async fn serve(
let span = tracing::info_span!("gitks.server", %addr);
let _enter = span.enter();
tracing::info!("registering gRPC services");
let (health_reporter, health_service) = tonic_health::server::health_reporter();
let repo_svc = repository_service_server::RepositoryServiceServer::new(svc.clone());
let archive_svc = archive_service_server::ArchiveServiceServer::new(svc.clone());
let blame_svc = blame_service_server::BlameServiceServer::new(svc.clone());
let branch_svc = branch_service_server::BranchServiceServer::new(svc.clone());
let commit_svc = commit_service_server::CommitServiceServer::new(svc.clone());
let diff_svc = diff_service_server::DiffServiceServer::new(svc.clone());
let merge_svc = merge_service_server::MergeServiceServer::new(svc.clone());
let pack_svc = pack_service_server::PackServiceServer::new(svc.clone());
let ref_svc = ref_service_server::RefServiceServer::new(svc.clone());
let remote_svc = remote_service_server::RemoteServiceServer::new(svc.clone());
let tag_svc = tag_service_server::TagServiceServer::new(svc.clone());
let tree_svc = tree_service_server::TreeServiceServer::new(svc);
health_reporter
.set_serving::<repository_service_server::RepositoryServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<archive_service_server::ArchiveServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<blame_service_server::BlameServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<branch_service_server::BranchServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<commit_service_server::CommitServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<diff_service_server::DiffServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<merge_service_server::MergeServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<pack_service_server::PackServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<ref_service_server::RefServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<remote_service_server::RemoteServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<tag_service_server::TagServiceServer<GitksService>>()
.await;
health_reporter
.set_serving::<tree_service_server::TreeServiceServer<GitksService>>()
.await;
let server = tonic::transport::Server::builder()
.add_service(repository_service_server::RepositoryServiceServer::new(
svc.clone(),
))
.add_service(archive_service_server::ArchiveServiceServer::new(
svc.clone(),
))
.add_service(blame_service_server::BlameServiceServer::new(svc.clone()))
.add_service(branch_service_server::BranchServiceServer::new(svc.clone()))
.add_service(commit_service_server::CommitServiceServer::new(svc.clone()))
.add_service(diff_service_server::DiffServiceServer::new(svc.clone()))
.add_service(merge_service_server::MergeServiceServer::new(svc.clone()))
.add_service(pack_service_server::PackServiceServer::new(svc.clone()))
.add_service(ref_service_server::RefServiceServer::new(svc.clone()))
.add_service(remote_service_server::RemoteServiceServer::new(svc.clone()))
.add_service(tag_service_server::TagServiceServer::new(svc.clone()))
.add_service(tree_service_server::TreeServiceServer::new(svc));
.add_service(health_service)
.add_service(repo_svc)
.add_service(archive_svc)
.add_service(blame_svc)
.add_service(branch_svc)
.add_service(commit_svc)
.add_service(diff_svc)
.add_service(merge_svc)
.add_service(pack_svc)
.add_service(ref_svc)
.add_service(remote_svc)
.add_service(tag_svc)
.add_service(tree_svc);
tracing::info!("server ready, starting to accept connections");
server.serve(addr).await
}