Skip to content

Commit

Permalink
chore: use LazyLock replace lazy_static (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
CherishCai authored Dec 15, 2024
1 parent f01abed commit d1df7d0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 49 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
2 changes: 2 additions & 0 deletions src/api/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
21 changes: 11 additions & 10 deletions src/common/executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
use crate::api::error::Result;
use futures::Future;
use lazy_static::lazy_static;
use tokio::{
runtime::{Builder, Runtime},
task::JoinHandle,
time::{interval, sleep, Duration},
};
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::<usize>().ok().filter(|n| *n > 0))
.unwrap_or(1);
static ref RT: Runtime = Builder::new_multi_thread()
static COMMON_THREAD_CORES: std::sync::LazyLock<usize> = std::sync::LazyLock::new(|| {
std::env::var(crate::api::constants::ENV_NACOS_CLIENT_COMMON_THREAD_CORES)
.ok()
.and_then(|v| v.parse::<usize>().ok().filter(|n| *n > 0))
.unwrap_or(1)
});

static RT: std::sync::LazyLock<Runtime> = 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<F>(future: F) -> JoinHandle<F::Output>
where
Expand Down
6 changes: 2 additions & 4 deletions src/common/remote/grpc/message/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lazy_static::lazy_static;
use prost_types::Any;
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -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<String> =
std::sync::LazyLock::new(|| local_ipaddress::get().unwrap());

impl<T> GrpcMessageBuilder<T>
where
Expand Down
62 changes: 28 additions & 34 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> = {
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::<HashMap<String, String>>()
};
#[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<HashMap<String, String>> = 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::<HashMap<String, String>>()
});

pub(crate) mod properties {
use crate::PROPERTIES;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d1df7d0

Please sign in to comment.