mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 07:23:51 +01:00
Fix pool management
This commit is contained in:
@@ -150,34 +150,41 @@ impl GrpcConnection {
|
||||
}
|
||||
|
||||
pub struct GrpcHandle {
|
||||
connections: HashMap<String, GrpcConnection>,
|
||||
pools: HashMap<String, DescriptorPool>,
|
||||
}
|
||||
|
||||
impl Default for GrpcHandle {
|
||||
fn default() -> Self {
|
||||
let connections = HashMap::new();
|
||||
let pools = HashMap::new();
|
||||
Self { connections, pools }
|
||||
Self { pools }
|
||||
}
|
||||
}
|
||||
|
||||
impl GrpcHandle {
|
||||
pub async fn services_from_files(
|
||||
&self,
|
||||
&mut self,
|
||||
id: &str,
|
||||
uri: &Uri,
|
||||
paths: Vec<PathBuf>,
|
||||
) -> Result<Vec<ServiceDefinition>, String> {
|
||||
let pool = fill_pool_from_files(paths).await?;
|
||||
self.pools.insert(self.get_pool_key(id, uri), pool.clone());
|
||||
Ok(self.services_from_pool(&pool))
|
||||
}
|
||||
pub async fn services_from_reflection(
|
||||
&mut self,
|
||||
id: &str,
|
||||
uri: &Uri,
|
||||
) -> Result<Vec<ServiceDefinition>, String> {
|
||||
let pool = fill_pool(uri).await?;
|
||||
self.pools.insert(self.get_pool_key(id, uri), pool.clone());
|
||||
Ok(self.services_from_pool(&pool))
|
||||
}
|
||||
|
||||
fn get_pool_key(&self, id: &str, uri: &Uri) -> String {
|
||||
format!("{}-{}", id, uri)
|
||||
}
|
||||
|
||||
fn services_from_pool(&self, pool: &DescriptorPool) -> Vec<ServiceDefinition> {
|
||||
pool.services()
|
||||
.map(|s| {
|
||||
@@ -268,7 +275,6 @@ impl GrpcHandle {
|
||||
|
||||
let conn = get_transport();
|
||||
let connection = GrpcConnection { pool, conn, uri };
|
||||
self.connections.insert(id.to_string(), connection.clone());
|
||||
Ok(connection)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ use tonic_reflection::pb::server_reflection_response::MessageResponse;
|
||||
use tonic_reflection::pb::ServerReflectionRequest;
|
||||
|
||||
pub async fn fill_pool_from_files(paths: Vec<PathBuf>) -> Result<DescriptorPool, String> {
|
||||
println!("FILL POOL FROM FILES");
|
||||
let mut pool = DescriptorPool::new();
|
||||
let random_file_name = format!("{}.desc", uuid::Uuid::new_v4());
|
||||
let desc_path = temp_dir().join(random_file_name);
|
||||
@@ -88,6 +89,7 @@ pub fn get_transport() -> Client<HttpsConnector<HttpConnector>, BoxBody> {
|
||||
}
|
||||
|
||||
pub async fn fill_pool(uri: &Uri) -> Result<DescriptorPool, String> {
|
||||
println!("FILL POOL FROM URI");
|
||||
let mut pool = DescriptorPool::new();
|
||||
let mut client = ServerReflectionClient::with_origin(get_transport(), uri.clone());
|
||||
|
||||
|
||||
@@ -101,12 +101,15 @@ async fn cmd_grpc_reflect(
|
||||
let req = get_grpc_request(&app_handle, request_id)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let uri = safe_uri(&req.url).map_err(|e| e.to_string())?;
|
||||
if req.proto_files.0.len() > 0 {
|
||||
println!("REFLECT FROM FILES");
|
||||
grpc_handle
|
||||
.lock()
|
||||
.await
|
||||
.services_from_files(
|
||||
&req.id,
|
||||
&uri,
|
||||
req.proto_files
|
||||
.0
|
||||
.iter()
|
||||
@@ -115,12 +118,10 @@ async fn cmd_grpc_reflect(
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
println!("REFLECT FROM URI");
|
||||
let uri = safe_uri(&req.url).map_err(|e| e.to_string())?;
|
||||
grpc_handle
|
||||
.lock()
|
||||
.await
|
||||
.services_from_reflection(&uri)
|
||||
.services_from_reflection(&req.id, &uri)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user