Skip to content

Commit

Permalink
chore(#197): naming_push_empty_protection can be set in ClientProps (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CherishCai authored Sep 27, 2023
1 parent 26f52ba commit 89b2111
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ gRPC 交互的 Payload 和 Metadata 由 `Protocol Buffers` 序列化,具体的
- [x] 服务防推空,默认开启,TODO 可选关闭。

#### Common 通用能力
- [ ] 创建参数,自定义传参 + ENV 环境变量读取,后者优先级高;ENV 统一前缀,例如 `NACOS_CLIENT_CONFIG_*` 于配置管理, `NACOS_CLIENT_NAMING_*` 于服务注册
- [x] 创建参数,自定义传参 + ENV 环境变量读取,后者优先级高;ENV 统一前缀,例如 `NACOS_CLIENT_CONFIG_*` 于配置管理, `NACOS_CLIENT_NAMING_*` 于服务注册
- [x] 通用客户端请求交互,Request/Response 通用 gRPC 逻辑,提供给 Config/Naming
- [x] Auth 鉴权;账密登陆 username/password,TODO accessKey/secretKey
- [x] 通用日志,`tracing::info!()`
Expand Down
6 changes: 5 additions & 1 deletion src/api/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub const ENV_NACOS_CLIENT_COMMON_NAMESPACE: &str = "NACOS_CLIENT_NAMESPACE";

pub const ENV_NACOS_CLIENT_COMMON_APP_NAME: &str = "NACOS_CLIENT_APP_NAME";

pub const ENV_NACOS_CLIENT_AUTH_USER_NAME: &str = "NACOS_CLIENT_USERNAME";
pub const ENV_NACOS_CLIENT_AUTH_USERNAME: &str = "NACOS_CLIENT_USERNAME";

pub const ENV_NACOS_CLIENT_AUTH_PASSWORD: &str = "NACOS_CLIENT_PASSWORD";

/// env `NACOS_CLIENT_NAMING_PUSH_EMPTY_PROTECTION`, default true
pub const ENV_NACOS_CLIENT_NAMING_PUSH_EMPTY_PROTECTION: &str =
"NACOS_CLIENT_NAMING_PUSH_EMPTY_PROTECTION";
4 changes: 2 additions & 2 deletions src/api/plugin/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl AuthContext {

/// Add the params.
pub fn add_params(mut self, map: HashMap<String, String>) -> Self {
self.params.extend(map.into_iter());
self.params.extend(map);
self
}
}
Expand All @@ -49,7 +49,7 @@ impl LoginIdentityContext {

/// Add the contexts.
pub fn add_contexts(mut self, map: HashMap<String, String>) -> Self {
self.contexts.extend(map.into_iter());
self.contexts.extend(map);
self
}
}
Expand Down
65 changes: 50 additions & 15 deletions src/api/props.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::api::constants::DEFAULT_SERVER_ADDR;
use crate::properties::{get_value, get_value_u32};
use crate::properties::{get_value, get_value_bool, get_value_u32};

/// Configures settings for Client.
#[derive(Debug, Clone)]
Expand All @@ -13,6 +13,8 @@ pub struct ClientProps {
pub(crate) namespace: String,
/// app_name
pub(crate) app_name: String,
/// naming push_empty_protection, default true
pub(crate) naming_push_empty_protection: bool,
/// metadata
pub(crate) labels: HashMap<String, String>,
/// client_version
Expand All @@ -23,15 +25,15 @@ pub struct ClientProps {

impl ClientProps {
pub(crate) fn get_server_list(&self) -> crate::api::error::Result<Vec<String>> {
if self.server_addr.trim().len() == 0 {
if self.server_addr.trim().is_empty() {
return Err(crate::api::error::Error::WrongServerAddress(String::from(
"Server address is empty",
)));
}
let hosts: Vec<&str> = self.server_addr.trim().split(',').collect::<Vec<&str>>();
let mut result = vec![];
for host in hosts {
let host_port = host.split(":").collect::<Vec<&str>>();
let host_port = host.split(':').collect::<Vec<&str>>();
if host_port.len() == 1 {
result.push(format!(
"{}:{}",
Expand All @@ -57,15 +59,35 @@ impl ClientProps {
let env_project_version = env!("CARGO_PKG_VERSION");
let client_version = format!("Nacos-Rust-Client:{}", env_project_version);

let server_addr = get_value(
crate::api::constants::ENV_NACOS_CLIENT_COMMON_SERVER_ADDRESS,
DEFAULT_SERVER_ADDR,
);

// public is "", Should define a more meaningful namespace
let namespace = get_value(crate::api::constants::ENV_NACOS_CLIENT_COMMON_NAMESPACE, "");

let app_name = get_value(
crate::api::constants::ENV_NACOS_CLIENT_COMMON_APP_NAME,
crate::api::constants::UNKNOWN,
);
let mut labels = HashMap::default();
labels.insert(
crate::api::constants::KEY_LABEL_APP_NAME.to_string(),
app_name.clone(),
);

let naming_push_empty_protection = get_value_bool(
crate::api::constants::ENV_NACOS_CLIENT_NAMING_PUSH_EMPTY_PROTECTION,
true,
);

ClientProps {
server_addr: get_value(
crate::api::constants::ENV_NACOS_CLIENT_COMMON_SERVER_ADDRESS,
DEFAULT_SERVER_ADDR,
),
/// public is "", Should define a more meaningful namespace
namespace: get_value(crate::api::constants::ENV_NACOS_CLIENT_COMMON_NAMESPACE, ""),
app_name: crate::api::constants::UNKNOWN.to_string(),
labels: HashMap::default(),
server_addr,
namespace,
app_name,
naming_push_empty_protection,
labels,
client_version,
auth_context: HashMap::default(),
grpc_port: None,
Expand All @@ -89,7 +111,10 @@ impl ClientProps {

/// Sets the namespace.
pub fn namespace(mut self, namespace: impl Into<String>) -> Self {
self.namespace = namespace.into();
self.namespace = get_value(
crate::api::constants::ENV_NACOS_CLIENT_COMMON_NAMESPACE,
namespace.into(),
);
self
}

Expand All @@ -105,9 +130,18 @@ impl ClientProps {
self
}

/// Sets the naming_push_empty_protection.
pub fn naming_push_empty_protection(mut self, naming_push_empty_protection: bool) -> Self {
self.naming_push_empty_protection = get_value_bool(
crate::api::constants::ENV_NACOS_CLIENT_NAMING_PUSH_EMPTY_PROTECTION,
naming_push_empty_protection,
);
self
}

/// Sets the labels.
pub fn labels(mut self, labels: HashMap<String, String>) -> Self {
self.labels.extend(labels.into_iter());
self.labels.extend(labels);
self
}

Expand All @@ -117,7 +151,7 @@ impl ClientProps {
self.auth_context.insert(
crate::api::plugin::USERNAME.into(),
get_value(
crate::api::constants::ENV_NACOS_CLIENT_AUTH_USER_NAME,
crate::api::constants::ENV_NACOS_CLIENT_AUTH_USERNAME,
username.into(),
),
);
Expand All @@ -130,7 +164,7 @@ impl ClientProps {
self.auth_context.insert(
crate::api::plugin::PASSWORD.into(),
get_value(
crate::api::constants::ENV_NACOS_CLIENT_AUTH_USER_NAME,
crate::api::constants::ENV_NACOS_CLIENT_AUTH_PASSWORD,
password.into(),
),
);
Expand All @@ -157,6 +191,7 @@ mod tests {
grpc_port: Some(8888),
namespace: "test_namespace".to_string(),
app_name: "test_app".to_string(),
naming_push_empty_protection: true,
labels: HashMap::new(),
client_version: "test_version".to_string(),
auth_context: HashMap::new(),
Expand Down
2 changes: 1 addition & 1 deletion src/config/message/request/config_publish_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ impl ConfigPublishRequest {

/// Add into additionMap.
pub fn add_addition_params(&mut self, addition_params: HashMap<String, String>) {
self.addition_map.extend(addition_params.into_iter());
self.addition_map.extend(addition_params);
}
}
22 changes: 15 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ lazy_static! {
static ref PROPERTIES: HashMap<String, String> = {
let env_file_path = std::env::var(ENV_CONFIG_FILE_PATH).ok();
let _ = env_file_path.as_ref().map(|file_path| {
dotenvy::from_path(Path::new(file_path)).or_else(|e| {
dotenvy::from_path(Path::new(file_path)).map_err(|e| {
let _ = dotenvy::dotenv();
Err(e)
e
})
});
dotenvy::dotenv().ok();

let prop = dotenvy::vars()
.into_iter()
.collect::<HashMap<String, String>>();

prop
dotenvy::vars().collect::<HashMap<String, String>>()
};
}

Expand Down Expand Up @@ -111,6 +107,18 @@ pub(crate) mod properties {
.map_or_else(|_e| default, |v| v)
})
}

pub(crate) fn get_value_bool<Key>(key: Key, default: bool) -> bool
where
Key: AsRef<str>,
{
PROPERTIES.get(key.as_ref()).map_or(default, |value| {
value
.to_string()
.parse::<bool>()
.map_or_else(|_e| default, |v| v)
})
}
}

/// Nacos API
Expand Down
2 changes: 1 addition & 1 deletion src/naming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl NacosNamingService {
let (observer, emitter) = observable::service_info_observable::create(
client_id.clone(),
naming_cache.clone(),
true,
client_props.naming_push_empty_protection,
);

let server_request_handler = NamingPushRequestHandler::new(emitter.clone());
Expand Down

0 comments on commit 89b2111

Please sign in to comment.