Fix pool management

This commit is contained in:
Gregory Schier
2024-02-06 23:26:24 -08:00
parent 1e3d43dbae
commit bcfa2c411f
5 changed files with 23 additions and 13 deletions

View File

@@ -150,34 +150,41 @@ impl GrpcConnection {
} }
pub struct GrpcHandle { pub struct GrpcHandle {
connections: HashMap<String, GrpcConnection>,
pools: HashMap<String, DescriptorPool>, pools: HashMap<String, DescriptorPool>,
} }
impl Default for GrpcHandle { impl Default for GrpcHandle {
fn default() -> Self { fn default() -> Self {
let connections = HashMap::new();
let pools = HashMap::new(); let pools = HashMap::new();
Self { connections, pools } Self { pools }
} }
} }
impl GrpcHandle { impl GrpcHandle {
pub async fn services_from_files( pub async fn services_from_files(
&self, &mut self,
id: &str,
uri: &Uri,
paths: Vec<PathBuf>, paths: Vec<PathBuf>,
) -> Result<Vec<ServiceDefinition>, String> { ) -> Result<Vec<ServiceDefinition>, String> {
let pool = fill_pool_from_files(paths).await?; 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)) Ok(self.services_from_pool(&pool))
} }
pub async fn services_from_reflection( pub async fn services_from_reflection(
&mut self, &mut self,
id: &str,
uri: &Uri, uri: &Uri,
) -> Result<Vec<ServiceDefinition>, String> { ) -> Result<Vec<ServiceDefinition>, String> {
let pool = fill_pool(uri).await?; let pool = fill_pool(uri).await?;
self.pools.insert(self.get_pool_key(id, uri), pool.clone());
Ok(self.services_from_pool(&pool)) 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> { fn services_from_pool(&self, pool: &DescriptorPool) -> Vec<ServiceDefinition> {
pool.services() pool.services()
.map(|s| { .map(|s| {
@@ -268,7 +275,6 @@ impl GrpcHandle {
let conn = get_transport(); let conn = get_transport();
let connection = GrpcConnection { pool, conn, uri }; let connection = GrpcConnection { pool, conn, uri };
self.connections.insert(id.to_string(), connection.clone());
Ok(connection) Ok(connection)
} }
} }

View File

@@ -24,6 +24,7 @@ use tonic_reflection::pb::server_reflection_response::MessageResponse;
use tonic_reflection::pb::ServerReflectionRequest; use tonic_reflection::pb::ServerReflectionRequest;
pub async fn fill_pool_from_files(paths: Vec<PathBuf>) -> Result<DescriptorPool, String> { pub async fn fill_pool_from_files(paths: Vec<PathBuf>) -> Result<DescriptorPool, String> {
println!("FILL POOL FROM FILES");
let mut pool = DescriptorPool::new(); let mut pool = DescriptorPool::new();
let random_file_name = format!("{}.desc", uuid::Uuid::new_v4()); let random_file_name = format!("{}.desc", uuid::Uuid::new_v4());
let desc_path = temp_dir().join(random_file_name); 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> { pub async fn fill_pool(uri: &Uri) -> Result<DescriptorPool, String> {
println!("FILL POOL FROM URI");
let mut pool = DescriptorPool::new(); let mut pool = DescriptorPool::new();
let mut client = ServerReflectionClient::with_origin(get_transport(), uri.clone()); let mut client = ServerReflectionClient::with_origin(get_transport(), uri.clone());

View File

@@ -101,12 +101,15 @@ async fn cmd_grpc_reflect(
let req = get_grpc_request(&app_handle, request_id) let req = get_grpc_request(&app_handle, request_id)
.await .await
.map_err(|e| e.to_string())?; .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 { if req.proto_files.0.len() > 0 {
println!("REFLECT FROM FILES"); println!("REFLECT FROM FILES");
grpc_handle grpc_handle
.lock() .lock()
.await .await
.services_from_files( .services_from_files(
&req.id,
&uri,
req.proto_files req.proto_files
.0 .0
.iter() .iter()
@@ -115,12 +118,10 @@ async fn cmd_grpc_reflect(
) )
.await .await
} else { } else {
println!("REFLECT FROM URI");
let uri = safe_uri(&req.url).map_err(|e| e.to_string())?;
grpc_handle grpc_handle
.lock() .lock()
.await .await
.services_from_reflection(&uri) .services_from_reflection(&req.id, &uri)
.await .await
} }
} }

View File

@@ -54,9 +54,10 @@ export function useGrpc(req: GrpcRequest | null, conn: GrpcConnection | null) {
const debouncedUrl = useDebouncedValue<string>(req?.url ?? 'n/a', 1000); const debouncedUrl = useDebouncedValue<string>(req?.url ?? 'n/a', 1000);
const reflect = useQuery<ReflectResponseService[] | null, string>({ const reflect = useQuery<ReflectResponseService[] | null, string>({
enabled: req != null, enabled: req != null,
queryKey: ['grpc_reflect', debouncedUrl], queryKey: ['grpc_reflect', req?.id ?? 'n/a', debouncedUrl],
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
queryFn: async () => { queryFn: async () => {
console.log('useGrpc.reflect', { requestId });
return (await minPromiseMillis( return (await minPromiseMillis(
invoke('cmd_grpc_reflect', { requestId }), invoke('cmd_grpc_reflect', { requestId }),
300, 300,

View File

@@ -112,11 +112,11 @@ export function useAnyHotkey(
} }
currentKeys.current.delete(normalizeKey(e.key, os)); currentKeys.current.delete(normalizeKey(e.key, os));
}; };
window.addEventListener('keydown', down); document.addEventListener('keydown', down);
window.addEventListener('keyup', up); document.addEventListener('keyup', up);
return () => { return () => {
window.removeEventListener('keydown', down); document.removeEventListener('keydown', down);
window.removeEventListener('keyup', up); document.removeEventListener('keyup', up);
}; };
}, [options.enable, os]); }, [options.enable, os]);
} }