Skip to content

Commit

Permalink
replace all lazy_static with std's OnceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
Liyixin95 committed Aug 16, 2024
1 parent b7b1884 commit ed62ea3
Show file tree
Hide file tree
Showing 26 changed files with 116 additions and 115 deletions.
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.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
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
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
10 changes: 5 additions & 5 deletions benchmark/src/bin/server.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::OnceLock};

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");
fn recoder() -> &'static Recoder {
static RECODER: OnceLock<Recoder> = OnceLock::new();
RECODER.get_or_init(|| Recoder::new("VOLO@Server"))
}

pub struct S;

impl EchoServer for S {
async fn echo(&self, req: Request) -> Result<Response, ServerError> {
let resp = process_request(&RECODER, req).await;
let resp = process_request(recoder(), req).await;
Ok(resp)
}

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 @@ -14,6 +14,7 @@ pub struct Recoder {
}

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

impl Recoder {
pub fn new(name: impl Into<FastStr>) -> Self {
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
12 changes: 6 additions & 6 deletions examples/src/grpc/compression/client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::OnceLock};

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 = {
fn client() -> &'static volo_gen::proto_gen::helloworld::GreeterClient {
static CLIENT: OnceLock<volo_gen::proto_gen::helloworld::GreeterClient> = OnceLock::new();
CLIENT.get_or_init(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::helloworld::GreeterClientBuilder::new("hello")
.send_compressions(vec![
Expand All @@ -20,15 +20,15 @@ lazy_static! {
.accept_compressions(vec![Gzip(None), Identity])
.address(addr)
.build()
};
})
}

#[volo::main]
async fn main() {
let req = volo_gen::proto_gen::helloworld::HelloRequest {
name: FastStr::from_static_str("Volo"),
};
let resp = CLIENT.say_hello(req).await;
let resp = client().say_hello(req).await;

match resp {
Ok(info) => println!("{info:?}"),
Expand Down
12 changes: 6 additions & 6 deletions examples/src/grpc/hello/client.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::OnceLock};

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

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
fn client() -> &'static volo_gen::proto_gen::helloworld::GreeterClient {
static CLIENT: OnceLock<volo_gen::proto_gen::helloworld::GreeterClient> = OnceLock::new();
CLIENT.get_or_init(|| {
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() {
let req = volo_gen::proto_gen::helloworld::HelloRequest {
name: FastStr::from_static_str("Volo"),
};
let resp = CLIENT.say_hello(req).await;
let resp = client().say_hello(req).await;
match resp {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
Expand Down
13 changes: 7 additions & 6 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::OnceLock,
};

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

lazy_static! {
static ref CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
fn client() -> &'static volo_gen::proto_gen::helloworld::GreeterClient {
static CLIENT: OnceLock<volo_gen::proto_gen::helloworld::GreeterClient> = OnceLock::new();
CLIENT.get_or_init(|| {
let discover = StaticDiscover::from(vec![
"127.0.0.1:8080".parse().unwrap(),
"127.0.0.2:8081".parse().unwrap(),
Expand All @@ -26,7 +27,7 @@ lazy_static! {
.load_balance(lb)
.discover(discover)
.build()
};
})
}

#[inline]
Expand Down Expand Up @@ -59,7 +60,7 @@ async fn main() {
name: FastStr::from_static_str("Volo"),
};
set_request_hash(ip_to_u64(&get_local_ip().unwrap()));
let resp = CLIENT.say_hello(req).await;
let resp = client().say_hello(req).await;
match resp {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
Expand All @@ -70,7 +71,7 @@ async fn main() {
name: FastStr::from_static_str("Volo"),
};
set_request_hash(1000);
let resp = CLIENT.clone().say_hello(req).await;
let resp = client().clone().say_hello(req).await;
match resp {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("{e:?}"),
Expand Down
23 changes: 14 additions & 9 deletions examples/src/grpc/multiplex/client.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::OnceLock};

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

lazy_static! {
static ref GREETER_CLIENT: volo_gen::proto_gen::helloworld::GreeterClient = {
fn greeter_client() -> &'static volo_gen::proto_gen::helloworld::GreeterClient {
static GREETER_CLIENT: OnceLock<volo_gen::proto_gen::helloworld::GreeterClient> =
OnceLock::new();
GREETER_CLIENT.get_or_init(|| {
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 = {
})
}

fn echo_clieht() -> &'static volo_gen::proto_gen::echo::EchoClient {
static ECHO_CLIENT: OnceLock<volo_gen::proto_gen::echo::EchoClient> = OnceLock::new();
ECHO_CLIENT.get_or_init(|| {
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() {
let req = volo_gen::proto_gen::helloworld::HelloRequest {
name: FastStr::from_static_str("Volo"),
};
let resp = GREETER_CLIENT.say_hello(req).await;
let resp = greeter_client().say_hello(req).await;
match resp {
Ok(info) => println!("GREETER: {info:?}"),
Err(e) => eprintln!("GREETER: {e:?}"),
Expand All @@ -32,7 +37,7 @@ async fn main() {
let req = volo_gen::proto_gen::echo::EchoRequest {
message: FastStr::from_static_str("Volo"),
};
let resp = ECHO_CLIENT.echo(req).await;
let resp = echo_clieht().echo(req).await;
match resp {
Ok(info) => println!("ECHO: {info:?}"),
Err(e) => eprintln!("ECHO: {e:?}"),
Expand Down
18 changes: 9 additions & 9 deletions examples/src/grpc/streaming/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::OnceLock};

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 = {
fn client() -> &'static volo_gen::proto_gen::streaming::StreamingClient {
static CLIENT: OnceLock<volo_gen::proto_gen::streaming::StreamingClient> = OnceLock::new();
CLIENT.get_or_init(|| {
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
volo_gen::proto_gen::streaming::StreamingClientBuilder::new("streaming")
.address(addr)
.build()
};
})
}

#[volo::main]
Expand All @@ -26,7 +26,7 @@ async fn unary() {
let req = volo_gen::proto_gen::streaming::StreamingRequest {
message: FastStr::from_static_str("Volo"),
};
match CLIENT.unary(req).await {
match client().unary(req).await {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("Unary, {e:?}"),
}
Expand All @@ -41,7 +41,7 @@ async fn client_streaming() {
yield req.clone();
}
};
match CLIENT.client_streaming(stream_req).await {
match client().client_streaming(stream_req).await {
Ok(info) => println!("{info:?}"),
Err(e) => eprintln!("ClientStreaming, {e:?}"),
}
Expand All @@ -51,7 +51,7 @@ async fn server_streaming() {
let req = volo_gen::proto_gen::streaming::StreamingRequest {
message: FastStr::from_static_str("Volo"),
};
let mut stream_resp = match CLIENT.server_streaming(req).await {
let mut stream_resp = match client().server_streaming(req).await {
Ok(resp) => resp.into_inner(),
Err(e) => {
eprintln!("ServerStreaming, {e:?}");
Expand Down Expand Up @@ -83,7 +83,7 @@ async fn bidirectional_streaming() {
yield req.clone();
}
};
let stream_resp = match CLIENT.bidirectional_streaming(stream_req).await {
let stream_resp = match client().bidirectional_streaming(stream_req).await {
Ok(resp) => resp.into_inner(),
Err(e) => {
eprintln!("BidirectionalStreaming, {e:?}");
Expand Down
12 changes: 6 additions & 6 deletions examples/src/thrift/hello/client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::net::SocketAddr;
use std::{net::SocketAddr, sync::OnceLock};

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

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

#[volo::main]
Expand All @@ -19,7 +19,7 @@ async fn main() {
common: None,
common2: None,
};
let resp = CLIENT
let resp = client()
.clone()
.with_callopt(CallOpt::default())
.hello(req)
Expand Down
12 changes: 6 additions & 6 deletions examples/src/thrift/multiplex/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use std::{future, net::SocketAddr};
use std::{future, net::SocketAddr, sync::OnceLock};

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

lazy_static! {
static ref CLIENT: volo_gen::thrift_gen::hello::HelloServiceClient = {
fn client() -> &'static volo_gen::thrift_gen::hello::HelloServiceClient {
static CLIENT: OnceLock<volo_gen::thrift_gen::hello::HelloServiceClient> = OnceLock::new();
CLIENT.get_or_init(|| {
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]
Expand All @@ -26,7 +26,7 @@ async fn main() {
common: None,
common2: None,
};
let resp = CLIENT
let resp = client()
.clone()
.with_callopt(CallOpt::default())
.hello(req)
Expand Down
Loading

0 comments on commit ed62ea3

Please sign in to comment.