This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/dubbo-rust.git
commit 4af80c979980f0e47b24ea418ffaf957a74ce365 Author: yangyang <[email protected]> AuthorDate: Sun Jul 31 11:11:17 2022 +0800 refactor(common): update url model, add some funcs --- dubbo/src/common/url.rs | 93 +++++++++++++++++++++++++++- dubbo/src/echo/mod.rs | 22 ++++--- dubbo/src/protocol/grpc/grpc_invoker.rs | 2 +- dubbo/src/protocol/grpc/grpc_server.rs | 2 +- dubbo/src/protocol/grpc/mod.rs | 7 +-- dubbo/src/protocol/triple/triple_protocol.rs | 2 +- 6 files changed, 108 insertions(+), 20 deletions(-) diff --git a/dubbo/src/common/url.rs b/dubbo/src/common/url.rs index 5d7ce69..911d47e 100644 --- a/dubbo/src/common/url.rs +++ b/dubbo/src/common/url.rs @@ -15,8 +15,97 @@ * limitations under the License. */ -#[derive(Debug, Clone, Default)] +use std::collections::HashMap; + +#[derive(Debug, Clone, Default, PartialEq)] pub struct Url { - pub url: String, + pub uri: String, + pub protocol: String, + pub location: String, + pub ip: String, + pub port: String, pub service_key: String, + pub params: HashMap<String, String>, +} + +impl Url { + pub fn new() -> Self { + Default::default() + } + + pub fn from_url(url: &str) -> Option<Self> { + // url: triple://127.0.0.1:8888/helloworld.Greeter + let uri = url + .parse::<http::Uri>() + .map_err(|err| { + println!("fail to parse url({}), err: {:?}", url, err); + }) + .unwrap(); + Some(Self { + uri: uri.to_string(), + protocol: uri.scheme_str()?.to_string(), + ip: uri.authority()?.host().to_string(), + port: uri.authority()?.port().unwrap().to_string(), + location: uri.authority()?.to_string(), + service_key: uri.path().trim_start_matches('/').to_string(), + params: HashMap::new(), + }) + } + + pub fn get_service_name(&self) -> &str { + &self.service_key + } + + pub fn get_param(&self, key: String) -> Option<String> { + self.params.get(&key).cloned() + } + + pub fn encode_param(&self) -> String { + let mut params_vec: Vec<String> = Vec::new(); + for (k, v) in self.params.iter() { + // let tmp = format!("{}={}", k, v); + params_vec.push(format!("{}={}", k, v)); + } + params_vec.join("&") + } + + pub fn decode(&mut self, params: String) { + let p: Vec<String> = params.split('&').map(|v| v.trim().to_string()).collect(); + for v in p.iter() { + let values: Vec<String> = v.split('=').map(|v| v.trim().to_string()).collect(); + if values.len() != 2 { + continue; + } + self.params.insert(values[0].clone(), values[1].clone()); + } + } + + pub fn to_url(&self) -> String { + format!("{}://{}:{}", self.protocol, self.ip, self.port) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_from_url() { + let u1 = Url::from_url(""); + } + + #[test] + fn test_encode_params() { + let mut u = Url::default(); + u.params.insert("method".to_string(), "GET".to_string()); + u.params.insert("args".to_string(), "GET".to_string()); + + let en = u.encode_param(); + println!("encode_params: {:?}", en); + + let mut u1 = Url::default(); + u1.decode(en); + println!("decode_params: {:?}", u1); + assert_eq!(u1, u); + } } diff --git a/dubbo/src/echo/mod.rs b/dubbo/src/echo/mod.rs index 7d4ff83..e56d001 100644 --- a/dubbo/src/echo/mod.rs +++ b/dubbo/src/echo/mod.rs @@ -138,27 +138,29 @@ async fn test_triple_protocol() { for (_, c) in conf.service.iter() { let mut u = Url::default(); if c.protocol_configs.is_empty() { - u = Url { - url: conf - .protocols + let protocol_url = format!( + "{}/{}", + conf.protocols .get(&c.protocol) .unwrap() .clone() .to_url() .clone(), - service_key: c.name.clone(), - }; + c.name.clone(), + ); + u = Url::from_url(&protocol_url).unwrap(); } else { - u = Url { - url: c - .protocol_configs + let protocol_url = format! { + "{}/{}", + c.protocol_configs .get(&c.protocol) .unwrap() .clone() .to_url() .clone(), - service_key: c.name.clone(), - } + c.name.clone(), + }; + u = Url::from_url(&protocol_url).unwrap(); } println!("url: {:?}", u); urls.push(u.clone()); diff --git a/dubbo/src/protocol/grpc/grpc_invoker.rs b/dubbo/src/protocol/grpc/grpc_invoker.rs index e64a1db..3768fff 100644 --- a/dubbo/src/protocol/grpc/grpc_invoker.rs +++ b/dubbo/src/protocol/grpc/grpc_invoker.rs @@ -33,7 +33,7 @@ pub struct GrpcInvoker { impl GrpcInvoker { pub fn new(url: Url) -> GrpcInvoker { - let endpoint = Endpoint::new(url.url.clone()).unwrap(); + let endpoint = Endpoint::new(url.to_url()).unwrap(); let conn = endpoint.connect_lazy(); Self { url, diff --git a/dubbo/src/protocol/grpc/grpc_server.rs b/dubbo/src/protocol/grpc/grpc_server.rs index b949a55..aadd5ac 100644 --- a/dubbo/src/protocol/grpc/grpc_server.rs +++ b/dubbo/src/protocol/grpc/grpc_server.rs @@ -53,7 +53,7 @@ impl GrpcServer { } pub async fn serve(mut self, url: Url) { - let addr = url.url.clone().parse().unwrap(); + let addr = url.to_url().parse().unwrap(); let svc = super::GRPC_SERVICES .read() .unwrap() diff --git a/dubbo/src/protocol/grpc/mod.rs b/dubbo/src/protocol/grpc/mod.rs index f5a6ca3..ccea468 100644 --- a/dubbo/src/protocol/grpc/mod.rs +++ b/dubbo/src/protocol/grpc/mod.rs @@ -61,11 +61,8 @@ async fn test_hello() { // server start, api: 0.0.0.0:8888/helloworld.Greeter/SayHello let pro = grpc_protocol::GrpcProtocol::new(); - pro.export(Url { - url: "[::1]:50051".to_string(), - service_key: svc_name.clone(), - }) - .await; + pro.export(Url::from_url(&format!("[::1]:50051/{}", svc_name.clone())).unwrap()) + .await; } #[derive(Default)] diff --git a/dubbo/src/protocol/triple/triple_protocol.rs b/dubbo/src/protocol/triple/triple_protocol.rs index 0aa3537..2c0f0ae 100644 --- a/dubbo/src/protocol/triple/triple_protocol.rs +++ b/dubbo/src/protocol/triple/triple_protocol.rs @@ -62,7 +62,7 @@ impl Protocol for TripleProtocol { async fn export(mut self, url: Url) -> Self::Exporter { let server = TripleServer::new(url.service_key.clone()); self.servers.insert(url.service_key.clone(), server.clone()); - server.serve(url.url.clone()).await; + server.serve(url.to_url()).await; TripleExporter::new() }
