baerwang commented on code in PR #175:
URL: https://github.com/apache/dubbo-rust/pull/175#discussion_r1503659083


##########
dubbo/src/cluster/router/manager/condition_manager.rs:
##########
@@ -0,0 +1,72 @@
+use crate::cluster::router::condition::{
+    condition_router::{ConditionRouter, ConditionSingleRouters},
+    single_router::ConditionSingleRouter,
+};
+use dubbo_config::router::ConditionRouterConfig;
+use std::{
+    collections::HashMap,
+    sync::{Arc, RwLock},
+};
+
+#[derive(Debug, Clone, Default)]
+pub struct ConditionRouterManager {
+    //Application-level routing applies globally, while service-level routing 
only affects a specific service.
+    pub routers_service: HashMap<String, Arc<RwLock<ConditionSingleRouters>>>,
+    pub routers_application: Arc<RwLock<ConditionSingleRouters>>,
+}
+
+impl ConditionRouterManager {
+    pub fn get_router(&self, service_name: &String) -> Option<ConditionRouter> 
{
+        let routers_application_is_null = 
self.routers_application.read().unwrap().is_null();
+        self.routers_service
+            .get(service_name)
+            .map(|routers_service| {
+                ConditionRouter::new(
+                    Some(routers_service.clone()),
+                    if routers_application_is_null {

Review Comment:
   这段代码还可以再优化下



##########
NOTICE:
##########
@@ -1,5 +1,5 @@
 Apache Dubbo
-Copyright 2018-2024 The Apache Software Foundation
+Copyright 2018-2023 The Apache Software Foundation

Review Comment:
   no need to change



##########
dubbo/src/cluster/router/manager/condition_manager.rs:
##########
@@ -0,0 +1,72 @@
+use crate::cluster::router::condition::{
+    condition_router::{ConditionRouter, ConditionSingleRouters},
+    single_router::ConditionSingleRouter,
+};
+use dubbo_config::router::ConditionRouterConfig;
+use std::{
+    collections::HashMap,
+    sync::{Arc, RwLock},
+};
+
+#[derive(Debug, Clone, Default)]
+pub struct ConditionRouterManager {
+    //Application-level routing applies globally, while service-level routing 
only affects a specific service.
+    pub routers_service: HashMap<String, Arc<RwLock<ConditionSingleRouters>>>,
+    pub routers_application: Arc<RwLock<ConditionSingleRouters>>,
+}
+
+impl ConditionRouterManager {
+    pub fn get_router(&self, service_name: &String) -> Option<ConditionRouter> 
{
+        let routers_application_is_null = 
self.routers_application.read().unwrap().is_null();
+        self.routers_service
+            .get(service_name)
+            .map(|routers_service| {
+                ConditionRouter::new(
+                    Some(routers_service.clone()),
+                    if routers_application_is_null {
+                        None
+                    } else {
+                        Some(self.routers_application.clone())
+                    },
+                )
+            })
+            .or_else(|| {
+                if routers_application_is_null {

Review Comment:
   ditto



##########
dubbo/src/cluster/router/nacos_config_center/nacos_client.rs:
##########
@@ -0,0 +1,126 @@
+use crate::cluster::router::manager::router_manager::{
+    get_global_router_manager, RouterConfigChangeEvent,
+};
+use dubbo_config::router::NacosConfig;
+use dubbo_logger::{tracing, tracing::info};
+use nacos_sdk::api::{
+    config::{ConfigChangeListener, ConfigResponse, ConfigService, 
ConfigServiceBuilder},
+    props::ClientProps,
+};
+use std::sync::{Arc, RwLock};
+
+pub struct NacosClient {
+    pub client: Arc<RwLock<dyn ConfigService>>,
+}
+
+unsafe impl Send for NacosClient {}
+
+unsafe impl Sync for NacosClient {}
+
+pub struct ConfigChangeListenerImpl;
+
+impl NacosClient {
+    pub fn new_init_client(config: NacosConfig) -> Self {
+        let server_addr = config.addr;
+        let namespace = config.namespace;
+        let app = config.app;
+        let enable_auth = config.enable_auth;
+
+        let mut props = ClientProps::new()
+            .server_addr(server_addr)
+            .namespace(namespace)
+            .app_name(app);
+
+        if enable_auth.is_some() {
+            info!("enable nacos auth!");
+        } else {
+            info!("disable nacos auth!");
+        }
+
+        if let Some(auth) = enable_auth {
+            props = props
+                .auth_username(auth.auth_username)
+                .auth_password(auth.auth_password);
+        }
+
+        let client = Arc::new(RwLock::new(
+            ConfigServiceBuilder::new(props)
+                .build()
+                .expect("NacosClient build failed! Please check NacosConfig"),
+        ));
+
+        Self { client }
+    }
+
+    pub fn get_config<T>(&self, data_id: &str, group: &str, config_type: &str) 
-> Option<T>
+    where
+        T: serde::de::DeserializeOwned,
+    {
+        let config_resp = self
+            .client
+            .read()
+            .unwrap()
+            .get_config(data_id.to_string(), group.to_string());
+
+        match config_resp {
+            Ok(config_resp) => {
+                self.add_listener(data_id, group);
+                let string = config_resp.content();
+                let result = serde_yaml::from_str(string);
+
+                match result {
+                    Ok(config) => {
+                        info!(
+                            "success to get {}Router config and parse success",
+                            config_type
+                        );
+                        Some(config)
+                    }
+                    Err(_) => {
+                        info!("failed to parse {}Router rule", config_type);

Review Comment:
   日志加个异常吧,别用info



##########
dubbo/src/cluster/router/condition/matcher.rs:
##########
@@ -0,0 +1,78 @@
+use regex::Regex;
+use std::{collections::HashSet, error::Error, option::Option};
+
+#[derive(Clone, Debug, Default)]
+pub struct ConditionMatcher {
+    _key: String,
+    matches: HashSet<String>,
+    mismatches: HashSet<String>,
+}
+
+impl ConditionMatcher {
+    pub fn new(_key: String) -> Self {
+        ConditionMatcher {
+            _key,
+            matches: HashSet::new(),
+            mismatches: HashSet::new(),
+        }
+    }
+
+    pub fn is_match(&self, value: Option<String>) -> Result<bool, Box<dyn 
Error>> {
+        match value {
+            None => Ok(false),
+            Some(val) => {
+                for match_ in self.matches.iter() {
+                    if self.do_pattern_match(match_, &val) {
+                        return Ok(true);
+                    }
+                }
+                for mismatch in self.mismatches.iter() {
+                    if !self.do_pattern_match(mismatch, &val) {
+                        return Ok(true);
+                    }
+                }
+                Ok(false)
+            }
+        }
+    }
+
+    pub fn get_matches(&mut self) -> &mut HashSet<String> {
+        &mut self.matches
+    }
+    pub fn get_mismatches(&mut self) -> &mut HashSet<String> {
+        &mut self.mismatches
+    }
+
+    fn do_pattern_match(&self, pattern: &str, value: &str) -> bool {
+        if pattern.contains('*') {
+            return star_matcher(pattern, value);
+        }
+
+        if pattern.contains('~') {
+            let parts: Vec<&str> = pattern.split('~').collect();
+
+            if parts.len() == 2 {
+                if let (Ok(left), Ok(right), Ok(val)) = (
+                    parts[0].parse::<i32>(),
+                    parts[1].parse::<i32>(),
+                    value.parse::<i32>(),
+                ) {
+                    return range_matcher(val, left, right);
+                }
+            }
+            return false;
+        }
+        pattern == value
+    }
+}
+
+pub fn star_matcher(pattern: &str, input: &str) -> bool {
+    // 将*替换为任意字符的正则表达式

Review Comment:
   use english



##########
dubbo/src/cluster/router/nacos_config_center/nacos_client.rs:
##########
@@ -0,0 +1,126 @@
+use crate::cluster::router::manager::router_manager::{
+    get_global_router_manager, RouterConfigChangeEvent,
+};
+use dubbo_config::router::NacosConfig;
+use dubbo_logger::{tracing, tracing::info};
+use nacos_sdk::api::{
+    config::{ConfigChangeListener, ConfigResponse, ConfigService, 
ConfigServiceBuilder},
+    props::ClientProps,
+};
+use std::sync::{Arc, RwLock};
+
+pub struct NacosClient {
+    pub client: Arc<RwLock<dyn ConfigService>>,
+}
+
+unsafe impl Send for NacosClient {}
+
+unsafe impl Sync for NacosClient {}
+
+pub struct ConfigChangeListenerImpl;
+
+impl NacosClient {
+    pub fn new_init_client(config: NacosConfig) -> Self {
+        let server_addr = config.addr;
+        let namespace = config.namespace;
+        let app = config.app;
+        let enable_auth = config.enable_auth;
+
+        let mut props = ClientProps::new()
+            .server_addr(server_addr)
+            .namespace(namespace)
+            .app_name(app);
+
+        if enable_auth.is_some() {
+            info!("enable nacos auth!");

Review Comment:
   这段代码可以删除,感觉没必要



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to