feat: rust/ deprecate lazy_static , use once_cell

This commit is contained in:
2024-04-30 20:03:20 +08:00
parent 3dbf993099
commit 5d0c3f5fd4
6 changed files with 35 additions and 24 deletions

View File

@ -1,16 +1,14 @@
use hickory_resolver::config::{NameServerConfigGroup, ResolverConfig, ResolverOpts};
use hickory_resolver::{lookup_ip::LookupIpIntoIter, TokioAsyncResolver};
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use once_cell::sync::{Lazy, OnceCell};
use reqwest::dns::{Addrs, Name, Resolve, Resolving};
use std::collections::HashMap;
use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::{Arc, RwLock};
lazy_static! {
pub static ref MY_HOSTS_MAP: RwLock<HashMap<String, IpAddr>> = RwLock::from(HashMap::new());
}
pub static MY_HOSTS_MAP: Lazy<RwLock<HashMap<String, IpAddr>>> =
Lazy::new(|| RwLock::from(HashMap::new()));
/// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait.
#[derive(Debug, Default, Clone)]

View File

@ -1,6 +1,6 @@
pub mod dns;
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::{Method, RequestBuilder};
use scopeguard::defer;
@ -43,12 +43,12 @@ fn _hyper_version_to_my_version(v: reqwest::Version) -> MyHttpVersion {
}
}
lazy_static! {
static ref DEFAULT_HEADER: RwLock<HeaderMap> = RwLock::from(HeaderMap::new());
static ref DNS_CLIENT: Arc<dns::MyHickoryDnsResolver> =
Arc::from(dns::MyHickoryDnsResolver::default());
static ref HTTP_CLIENT: reqwest::Client = new_http_client(true);
}
static DEFAULT_HEADER: Lazy<RwLock<HeaderMap>> = Lazy::new(|| RwLock::from(HeaderMap::new()));
static DNS_CLIENT: Lazy<Arc<dns::MyHickoryDnsResolver>> =
Lazy::new(|| Arc::from(dns::MyHickoryDnsResolver::default()));
static HTTP_CLIENT: Lazy<reqwest::Client> = Lazy::new(|| new_http_client(true));
fn new_http_client(keep_alive: bool) -> reqwest::Client {
let mut c = reqwest::Client::builder()