diff --git a/Cargo.toml b/Cargo.toml index 8478f3c..40715a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,6 @@ prost = "0.13" prost-types = "0.13" serde = { version = "1", features = ["derive"] } serde_json = "1" -lazy_static = "1.4" tracing = "0.1" local_ipaddress = "0.1.3" @@ -94,3 +93,7 @@ path = "examples/simple_app.rs" [[example]] name = "lazy_app" path = "examples/lazy_app.rs" + +[[example]] +name = "aliyun_ram_app" +path = "examples/aliyun_ram_app.rs" diff --git a/src/api/constants.rs b/src/api/constants.rs index 8965fcf..92ced09 100644 --- a/src/api/constants.rs +++ b/src/api/constants.rs @@ -29,6 +29,8 @@ pub(crate) mod common_remote { pub const LABEL_MODULE_CONFIG: &str = "config"; } +pub(crate) const ENV_NACOS_CLIENT_PROPS_FILE_PATH: &str = "NACOS_CLIENT_PROPS_FILE_PATH"; + /// env `NACOS_CLIENT_COMMON_THREAD_CORES` to set nacos-client-thread-pool num, default 1 pub const ENV_NACOS_CLIENT_COMMON_THREAD_CORES: &str = "NACOS_CLIENT_COMMON_THREAD_CORES"; diff --git a/src/common/executor/mod.rs b/src/common/executor/mod.rs index be6698e..bc37975 100644 --- a/src/common/executor/mod.rs +++ b/src/common/executor/mod.rs @@ -1,6 +1,5 @@ use crate::api::error::Result; use futures::Future; -use lazy_static::lazy_static; use tokio::{ runtime::{Builder, Runtime}, task::JoinHandle, @@ -8,19 +7,21 @@ use tokio::{ }; use tracing::{error, Instrument}; -lazy_static! { - static ref COMMON_THREAD_CORES: usize = - std::env::var(crate::api::constants::ENV_NACOS_CLIENT_COMMON_THREAD_CORES) - .ok() - .and_then(|v| v.parse::().ok().filter(|n| *n > 0)) - .unwrap_or(1); - static ref RT: Runtime = Builder::new_multi_thread() +static COMMON_THREAD_CORES: std::sync::LazyLock = std::sync::LazyLock::new(|| { + std::env::var(crate::api::constants::ENV_NACOS_CLIENT_COMMON_THREAD_CORES) + .ok() + .and_then(|v| v.parse::().ok().filter(|n| *n > 0)) + .unwrap_or(1) +}); + +static RT: std::sync::LazyLock = std::sync::LazyLock::new(|| { + Builder::new_multi_thread() .enable_all() .thread_name("nacos-client-thread-pool") .worker_threads(*COMMON_THREAD_CORES) .build() - .unwrap(); -} + .unwrap() +}); pub(crate) fn spawn(future: F) -> JoinHandle where diff --git a/src/common/remote/grpc/message/mod.rs b/src/common/remote/grpc/message/mod.rs index 6faa88f..47e8f09 100644 --- a/src/common/remote/grpc/message/mod.rs +++ b/src/common/remote/grpc/message/mod.rs @@ -1,4 +1,3 @@ -use lazy_static::lazy_static; use prost_types::Any; use serde::{de::DeserializeOwned, Serialize}; use std::collections::HashMap; @@ -230,9 +229,8 @@ where client_ip: String, } -lazy_static! { - static ref LOCAL_IP: String = local_ipaddress::get().unwrap(); -} +static LOCAL_IP: std::sync::LazyLock = + std::sync::LazyLock::new(|| local_ipaddress::get().unwrap()); impl GrpcMessageBuilder where diff --git a/src/lib.rs b/src/lib.rs index 80efc80..751e168 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,27 +62,37 @@ //! ``` //! -use lazy_static::lazy_static; -use std::collections::HashMap; -use std::path::Path; - -const ENV_NACOS_CLIENT_PROPS_FILE_PATH: &str = "NACOS_CLIENT_PROPS_FILE_PATH"; - -lazy_static! { - static ref PROPERTIES: HashMap = { - let env_file_path = std::env::var(ENV_NACOS_CLIENT_PROPS_FILE_PATH).ok(); - let _ = env_file_path.as_ref().map(|file_path| { - dotenvy::from_path(Path::new(file_path)).map_err(|e| { - let _ = dotenvy::dotenv(); - e - }) - }); - dotenvy::dotenv().ok(); +/// Nacos API +pub mod api; + +mod common; +#[cfg(feature = "config")] +mod config; +#[cfg(feature = "naming")] +mod naming; - dotenvy::vars().collect::>() - }; +#[allow(dead_code)] +#[path = ""] +mod nacos_proto { + #[path = "_.rs"] + pub mod v2; } +use crate::api::constants::ENV_NACOS_CLIENT_PROPS_FILE_PATH; +use std::collections::HashMap; + +static PROPERTIES: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + let env_file_path = std::env::var(ENV_NACOS_CLIENT_PROPS_FILE_PATH).ok(); + let _ = env_file_path.as_ref().map(|file_path| { + dotenvy::from_path(std::path::Path::new(file_path)).inspect_err(|_e| { + let _ = dotenvy::dotenv(); + }) + }); + dotenvy::dotenv().ok(); + + dotenvy::vars().collect::>() +}); + pub(crate) mod properties { use crate::PROPERTIES; @@ -122,22 +132,6 @@ pub(crate) mod properties { } } -/// Nacos API -pub mod api; - -mod common; -#[cfg(feature = "config")] -mod config; -#[cfg(feature = "naming")] -mod naming; - -#[allow(dead_code)] -#[path = ""] -mod nacos_proto { - #[path = "_.rs"] - pub mod v2; -} - #[cfg(test)] mod tests { use prost_types::Any;