feat(gateway): implement remote service forwarding for distributed git operations
- Add remote client functions for archive, blame, and branch services - Implement fallback logic to forward requests to remote storage nodes - Add logging for forwarding operations with route details - Update Cargo.lock with new dependencies including ractor cluster libraries - Extend .gitignore with IDE and build system files - Remove outdated comments from bare repository implementation
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
use ractor::RpcReplyPort;
|
||||
use ractor_cluster::BytesConvertable;
|
||||
use ractor_cluster::RactorClusterMessage;
|
||||
use crate::pb::RepositoryHeader;
|
||||
|
||||
impl BytesConvertable for RepositoryHeader {
|
||||
fn into_bytes(self) -> Vec<u8> {
|
||||
prost::Message::encode_to_vec(&self)
|
||||
}
|
||||
|
||||
fn from_bytes(bytes: Vec<u8>) -> Self {
|
||||
prost::Message::decode(bytes.as_slice()).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RouteDecision {
|
||||
pub found: bool,
|
||||
pub storage_name: String,
|
||||
pub relative_path: String,
|
||||
pub actor_name: String,
|
||||
pub grpc_addr: String,
|
||||
}
|
||||
|
||||
impl BytesConvertable for RouteDecision {
|
||||
fn into_bytes(self) -> Vec<u8> {
|
||||
encode_strings(&[
|
||||
if self.found { "1" } else { "0" }.to_string(),
|
||||
self.storage_name,
|
||||
self.relative_path,
|
||||
self.actor_name,
|
||||
self.grpc_addr,
|
||||
])
|
||||
}
|
||||
|
||||
fn from_bytes(bytes: Vec<u8>) -> Self {
|
||||
let values = decode_strings(bytes);
|
||||
Self {
|
||||
found: values.first().is_some_and(|v| v == "1"),
|
||||
storage_name: values.get(1).cloned().unwrap_or_default(),
|
||||
relative_path: values.get(2).cloned().unwrap_or_default(),
|
||||
actor_name: values.get(3).cloned().unwrap_or_default(),
|
||||
grpc_addr: values.get(4).cloned().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NodeHealth {
|
||||
pub storage_name: String,
|
||||
pub repo_count: u64,
|
||||
pub healthy: bool,
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
impl BytesConvertable for NodeHealth {
|
||||
fn into_bytes(self) -> Vec<u8> {
|
||||
encode_strings(&[
|
||||
self.storage_name,
|
||||
self.repo_count.to_string(),
|
||||
if self.healthy { "1" } else { "0" }.to_string(),
|
||||
self.version,
|
||||
])
|
||||
}
|
||||
|
||||
fn from_bytes(bytes: Vec<u8>) -> Self {
|
||||
let values = decode_strings(bytes);
|
||||
Self {
|
||||
storage_name: values.first().cloned().unwrap_or_default(),
|
||||
repo_count: values.get(1).and_then(|v| v.parse().ok()).unwrap_or_default(),
|
||||
healthy: values.get(2).is_some_and(|v| v == "1"),
|
||||
version: values.get(3).cloned().unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(RactorClusterMessage)]
|
||||
pub enum GitNodeMessage {
|
||||
ScanAndRegister,
|
||||
|
||||
RegisterRepository(RepositoryHeader),
|
||||
|
||||
RemoveRepository(RepositoryHeader),
|
||||
|
||||
#[rpc]
|
||||
RouteRepository(RepositoryHeader, RpcReplyPort<RouteDecision>),
|
||||
|
||||
#[rpc]
|
||||
ListRepositoryPaths(RpcReplyPort<String>),
|
||||
|
||||
#[rpc]
|
||||
RepositoryExists(RepositoryHeader, RpcReplyPort<bool>),
|
||||
|
||||
#[rpc]
|
||||
GetNodeHealth(RpcReplyPort<NodeHealth>),
|
||||
}
|
||||
|
||||
#[derive(ractor_cluster::RactorMessage)]
|
||||
pub enum RepoActorMessage {
|
||||
UpdateMetadata(RepositoryHeader),
|
||||
}
|
||||
|
||||
fn encode_strings(values: &[String]) -> Vec<u8> {
|
||||
let mut buf = Vec::new();
|
||||
for value in values {
|
||||
let bytes = value.as_bytes();
|
||||
buf.extend((bytes.len() as u64).to_be_bytes());
|
||||
buf.extend(bytes);
|
||||
}
|
||||
buf
|
||||
}
|
||||
|
||||
fn decode_strings(bytes: Vec<u8>) -> Vec<String> {
|
||||
let mut values = Vec::new();
|
||||
let mut offset = 0;
|
||||
while offset + 8 <= bytes.len() {
|
||||
let len = u64::from_be_bytes(bytes[offset..offset + 8].try_into().unwrap()) as usize;
|
||||
offset += 8;
|
||||
if offset + len > bytes.len() {
|
||||
break;
|
||||
}
|
||||
values.push(String::from_utf8_lossy(&bytes[offset..offset + len]).into_owned());
|
||||
offset += len;
|
||||
}
|
||||
values
|
||||
}
|
||||
Reference in New Issue
Block a user