Skip to content

Commit d1df7d0

Browse files
authored
chore: use LazyLock replace lazy_static (#250)
1 parent f01abed commit d1df7d0

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ prost = "0.13"
5656
prost-types = "0.13"
5757
serde = { version = "1", features = ["derive"] }
5858
serde_json = "1"
59-
lazy_static = "1.4"
6059

6160
tracing = "0.1"
6261
local_ipaddress = "0.1.3"
@@ -94,3 +93,7 @@ path = "examples/simple_app.rs"
9493
[[example]]
9594
name = "lazy_app"
9695
path = "examples/lazy_app.rs"
96+
97+
[[example]]
98+
name = "aliyun_ram_app"
99+
path = "examples/aliyun_ram_app.rs"

src/api/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub(crate) mod common_remote {
2929
pub const LABEL_MODULE_CONFIG: &str = "config";
3030
}
3131

32+
pub(crate) const ENV_NACOS_CLIENT_PROPS_FILE_PATH: &str = "NACOS_CLIENT_PROPS_FILE_PATH";
33+
3234
/// env `NACOS_CLIENT_COMMON_THREAD_CORES` to set nacos-client-thread-pool num, default 1
3335
pub const ENV_NACOS_CLIENT_COMMON_THREAD_CORES: &str = "NACOS_CLIENT_COMMON_THREAD_CORES";
3436

src/common/executor/mod.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
use crate::api::error::Result;
22
use futures::Future;
3-
use lazy_static::lazy_static;
43
use tokio::{
54
runtime::{Builder, Runtime},
65
task::JoinHandle,
76
time::{interval, sleep, Duration},
87
};
98
use tracing::{error, Instrument};
109

11-
lazy_static! {
12-
static ref COMMON_THREAD_CORES: usize =
13-
std::env::var(crate::api::constants::ENV_NACOS_CLIENT_COMMON_THREAD_CORES)
14-
.ok()
15-
.and_then(|v| v.parse::<usize>().ok().filter(|n| *n > 0))
16-
.unwrap_or(1);
17-
static ref RT: Runtime = Builder::new_multi_thread()
10+
static COMMON_THREAD_CORES: std::sync::LazyLock<usize> = std::sync::LazyLock::new(|| {
11+
std::env::var(crate::api::constants::ENV_NACOS_CLIENT_COMMON_THREAD_CORES)
12+
.ok()
13+
.and_then(|v| v.parse::<usize>().ok().filter(|n| *n > 0))
14+
.unwrap_or(1)
15+
});
16+
17+
static RT: std::sync::LazyLock<Runtime> = std::sync::LazyLock::new(|| {
18+
Builder::new_multi_thread()
1819
.enable_all()
1920
.thread_name("nacos-client-thread-pool")
2021
.worker_threads(*COMMON_THREAD_CORES)
2122
.build()
22-
.unwrap();
23-
}
23+
.unwrap()
24+
});
2425

2526
pub(crate) fn spawn<F>(future: F) -> JoinHandle<F::Output>
2627
where

src/common/remote/grpc/message/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use lazy_static::lazy_static;
21
use prost_types::Any;
32
use serde::{de::DeserializeOwned, Serialize};
43
use std::collections::HashMap;
@@ -230,9 +229,8 @@ where
230229
client_ip: String,
231230
}
232231

233-
lazy_static! {
234-
static ref LOCAL_IP: String = local_ipaddress::get().unwrap();
235-
}
232+
static LOCAL_IP: std::sync::LazyLock<String> =
233+
std::sync::LazyLock::new(|| local_ipaddress::get().unwrap());
236234

237235
impl<T> GrpcMessageBuilder<T>
238236
where

src/lib.rs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,37 @@
6262
//! ```
6363
//!
6464
65-
use lazy_static::lazy_static;
66-
use std::collections::HashMap;
67-
use std::path::Path;
68-
69-
const ENV_NACOS_CLIENT_PROPS_FILE_PATH: &str = "NACOS_CLIENT_PROPS_FILE_PATH";
70-
71-
lazy_static! {
72-
static ref PROPERTIES: HashMap<String, String> = {
73-
let env_file_path = std::env::var(ENV_NACOS_CLIENT_PROPS_FILE_PATH).ok();
74-
let _ = env_file_path.as_ref().map(|file_path| {
75-
dotenvy::from_path(Path::new(file_path)).map_err(|e| {
76-
let _ = dotenvy::dotenv();
77-
e
78-
})
79-
});
80-
dotenvy::dotenv().ok();
65+
/// Nacos API
66+
pub mod api;
67+
68+
mod common;
69+
#[cfg(feature = "config")]
70+
mod config;
71+
#[cfg(feature = "naming")]
72+
mod naming;
8173

82-
dotenvy::vars().collect::<HashMap<String, String>>()
83-
};
74+
#[allow(dead_code)]
75+
#[path = ""]
76+
mod nacos_proto {
77+
#[path = "_.rs"]
78+
pub mod v2;
8479
}
8580

81+
use crate::api::constants::ENV_NACOS_CLIENT_PROPS_FILE_PATH;
82+
use std::collections::HashMap;
83+
84+
static PROPERTIES: std::sync::LazyLock<HashMap<String, String>> = std::sync::LazyLock::new(|| {
85+
let env_file_path = std::env::var(ENV_NACOS_CLIENT_PROPS_FILE_PATH).ok();
86+
let _ = env_file_path.as_ref().map(|file_path| {
87+
dotenvy::from_path(std::path::Path::new(file_path)).inspect_err(|_e| {
88+
let _ = dotenvy::dotenv();
89+
})
90+
});
91+
dotenvy::dotenv().ok();
92+
93+
dotenvy::vars().collect::<HashMap<String, String>>()
94+
});
95+
8696
pub(crate) mod properties {
8797
use crate::PROPERTIES;
8898

@@ -122,22 +132,6 @@ pub(crate) mod properties {
122132
}
123133
}
124134

125-
/// Nacos API
126-
pub mod api;
127-
128-
mod common;
129-
#[cfg(feature = "config")]
130-
mod config;
131-
#[cfg(feature = "naming")]
132-
mod naming;
133-
134-
#[allow(dead_code)]
135-
#[path = ""]
136-
mod nacos_proto {
137-
#[path = "_.rs"]
138-
pub mod v2;
139-
}
140-
141135
#[cfg(test)]
142136
mod tests {
143137
use prost_types::Any;

0 commit comments

Comments
 (0)