Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace lazy_static with std's OnceLock #486

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ edition = "2021"
homepage = "https://www.cloudwego.io/docs/volo/"
repository = "/~https://github.com/cloudwego/volo"
license = "MIT OR Apache-2.0"
rust-version = "1.77.0"
rust-version = "1.80.0"

[workspace.dependencies]
pilota = "0.11"
Expand Down Expand Up @@ -69,7 +69,6 @@ hyper-timeout = "0.5"
hyper-util = "0.1"
itertools = "0.13"
itoa = "1"
lazy_static = "1"
libc = "0.2"
linkedbytes = "0.1"
linked-hash-map = "0.5"
Expand Down Expand Up @@ -133,7 +132,7 @@ webpki-roots = "0.26"
tokio-rustls = "0.25"
native-tls = "0.2"
tokio-native-tls = "0.3"
tokio-tungstenite="0.23.1"
tokio-tungstenite = "0.23.1"

[profile.release]
opt-level = 3
Expand Down
1 change: 0 additions & 1 deletion benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ chrono.workspace = true
clap = { workspace = true, features = ["derive"] }
faststr.workspace = true
governor.workspace = true
lazy_static.workspace = true
metainfo.workspace = true
motore.workspace = true
serde.workspace = true
Expand Down
7 changes: 2 additions & 5 deletions benchmark/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use anyhow::anyhow;
use benchmark::{
benchmark::echo::{EchoServer, ObjReq, ObjResp, Request, Response},
perf::Recoder,
runner::processor::process_request,
};
use lazy_static::lazy_static;
use volo_thrift::ServerError;

lazy_static! {
static ref RECODER: Recoder = Recoder::new("VOLO@Server");
}
static RECODER: LazyLock<Recoder> = LazyLock::new(|| Recoder::new("VOLO@Server"));

pub struct S;

Expand Down
1 change: 1 addition & 0 deletions benchmark/src/perf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Recoder {
mem: Arc<UnsafeCell<Vec<u64>>>,
}

unsafe impl Send for Recoder {}
unsafe impl Sync for Recoder {}

impl Recoder {
Expand Down
1 change: 0 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ http-body.workspace = true
http-body-util.workspace = true
hyper.workspace = true
hyper-util.workspace = true
lazy_static.workspace = true
metainfo.workspace = true
motore.workspace = true
serde.workspace = true
Expand Down
31 changes: 14 additions & 17 deletions examples/src/grpc/compression/client.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use lazy_static::lazy_static;
use pilota::FastStr;
use volo_grpc::codec::compression::{
CompressionEncoding::{Gzip, Identity, Zlib},
GzipConfig, Level, ZlibConfig,
};

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.send_compressions(vec![
Gzip(Some(GzipConfig::default())),
Zlib(Some(ZlibConfig {
level: Level::fast(),
})),
])
.accept_compressions(vec![Gzip(None), Identity])
.address(addr)
.build()
};
}
static CLIENT: LazyLock<volo_gen::proto_gen::helloworld::GreeterClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.send_compressions(vec![
Gzip(Some(GzipConfig::default())),
Zlib(Some(ZlibConfig {
level: Level::fast(),
})),
])
.accept_compressions(vec![Gzip(None), Identity])
.address(addr)
.build()
});

#[volo::main]
async fn main() {
Expand Down
17 changes: 7 additions & 10 deletions examples/src/grpc/hello/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use lazy_static::lazy_static;
use pilota::FastStr;

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.address(addr)
.build()
};
}
static CLIENT: LazyLock<volo_gen::proto_gen::helloworld::GreeterClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.address(addr)
.build()
});

#[volo::main]
async fn main() {
Expand Down
26 changes: 12 additions & 14 deletions examples/src/grpc/loadbalance/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::{
cell::RefCell,
hash::{Hash, Hasher},
net::IpAddr,
sync::LazyLock,
};

use lazy_static::lazy_static;
use metainfo::{MetaInfo, METAINFO};
use pilota::FastStr;
use volo::{
Expand All @@ -15,19 +15,17 @@ use volo::{
},
};

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
let discover = StaticDiscover::from(vec![
"127.0.0.1:8080".parse().unwrap(),
"127.0.0.2:8081".parse().unwrap(),
]);
let lb = ConsistentHashBalance::new(ConsistentHashOption::default());
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.load_balance(lb)
.discover(discover)
.build()
};
}
static CLIENT: LazyLock<volo_gen::proto_gen::helloworld::GreeterClient> = LazyLock::new(|| {
let discover = StaticDiscover::from(vec![
"127.0.0.1:8080".parse().unwrap(),
"127.0.0.2:8081".parse().unwrap(),
]);
let lb = ConsistentHashBalance::new(ConsistentHashOption::default());
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.load_balance(lb)
.discover(discover)
.build()
});

#[inline]
fn set_request_hash(code: u64) {
Expand Down
23 changes: 11 additions & 12 deletions examples/src/grpc/multiplex/client.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use lazy_static::lazy_static;
use pilota::FastStr;

lazy_static! {
static ref GREETER_CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
static GREETER_CLIENT: LazyLock<volo_gen::proto_gen::helloworld::GreeterClient> =
LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.address(addr)
.build()
};
static ref ECHO_CLIENT: volo_gen::proto_gen::echo::EchoClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::echo::EchoClientBuilder::new("echo")
.address(addr)
.build()
};
}
});

static ECHO_CLIENT: LazyLock<volo_gen::proto_gen::echo::EchoClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::echo::EchoClientBuilder::new("echo")
.address(addr)
.build()
});

#[volo::main]
async fn main() {
Expand Down
17 changes: 7 additions & 10 deletions examples/src/grpc/streaming/client.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use async_stream::stream;
use lazy_static::lazy_static;
use pilota::FastStr;
use tokio_stream::StreamExt;

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::streaming::StreamingClient = {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::streaming::StreamingClientBuilder::new("streaming")
.address(addr)
.build()
};
}
static CLIENT: LazyLock<volo_gen::proto_gen::streaming::StreamingClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::streaming::StreamingClientBuilder::new("streaming")
.address(addr)
.build()
});

#[volo::main]
async fn main() {
Expand Down
17 changes: 7 additions & 10 deletions examples/src/thrift/hello/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use lazy_static::lazy_static;
use volo_thrift::client::CallOpt;

lazy_static! {
static ref CLIENT: volo_gen::thrift_gen::hello::HelloServiceClient = {
let addr: SocketAddr = "127.0.0.1:8081".parse().unwrap();
volo_gen::thrift_gen::hello::HelloServiceClientBuilder::new("hello")
.address(addr)
.build()
};
}
static CLIENT: LazyLock<volo_gen::thrift_gen::hello::HelloServiceClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8081".parse().unwrap();
volo_gen::thrift_gen::hello::HelloServiceClientBuilder::new("hello")
.address(addr)
.build()
});

#[volo::main]
async fn main() {
Expand Down
19 changes: 8 additions & 11 deletions examples/src/thrift/multiplex/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use std::{future, net::SocketAddr};
use std::{future, net::SocketAddr, sync::LazyLock};

use lazy_static::lazy_static;
use volo_thrift::client::CallOpt;

lazy_static! {
static ref CLIENT: volo_gen::thrift_gen::hello::HelloServiceClient = {
let addr: SocketAddr = "127.0.0.1:8081".parse().unwrap();
volo_gen::thrift_gen::hello::HelloServiceClientBuilder::new("hello")
.address(addr)
.multiplex(true)
.build()
};
}
static CLIENT: LazyLock<volo_gen::thrift_gen::hello::HelloServiceClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8081".parse().unwrap();
volo_gen::thrift_gen::hello::HelloServiceClientBuilder::new("hello")
.address(addr)
.multiplex(true)
.build()
});

#[volo::main]
async fn main() {
Expand Down
17 changes: 7 additions & 10 deletions examples/src/thrift/unknown/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::LazyLock};

use lazy_static::lazy_static;
use volo_thrift::client::CallOpt;

lazy_static! {
static ref CLIENT: volo_gen::thrift_gen::echo::EchoServiceClient = {
let addr: SocketAddr = "127.0.0.1:8081".parse().unwrap();
volo_gen::thrift_gen::echo::EchoServiceClientBuilder::new("hello")
.address(addr)
.build()
};
}
static CLIENT: LazyLock<volo_gen::thrift_gen::echo::EchoServiceClient> = LazyLock::new(|| {
let addr: SocketAddr = "127.0.0.1:8081".parse().unwrap();
volo_gen::thrift_gen::echo::EchoServiceClientBuilder::new("hello")
.address(addr)
.build()
});

#[volo::main]
async fn main() {
Expand Down
1 change: 0 additions & 1 deletion volo-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ faststr.workspace = true
git2.workspace = true
heck.workspace = true
itertools.workspace = true
lazy_static.workspace = true
mockall.workspace = true
mockall_double.workspace = true
nom.workspace = true
Expand Down
12 changes: 6 additions & 6 deletions volo-build/src/legacy/util.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::{
fs::{create_dir_all, File, OpenOptions},
path::{Path, PathBuf},
sync::LazyLock,
};

use lazy_static::lazy_static;
use serde::de::Error;

use super::model::Config;

lazy_static! {
pub static ref DEFAULT_DIR: PathBuf = std::path::Path::new(
pub static DEFAULT_DIR: LazyLock<PathBuf> = LazyLock::new(|| {
std::path::Path::new(
&std::env::var("OUT_DIR")
.expect("OUT_DIR is not set, maybe you are calling volo-build outside build.rs?")
.expect("OUT_DIR is not set, maybe you are calling volo-build outside build.rs?"),
)
.join("idl");
}
.join("idl")
});

pub const DEFAULT_CONFIG_FILE: &str = "volo.yml";

Expand Down
Loading
Loading