From 144f105a77ba1168853c4c43ee142a4aa6beed98 Mon Sep 17 00:00:00 2001 From: Kristopher Wuollett Date: Tue, 30 Jan 2024 07:53:53 -0600 Subject: [PATCH 1/2] examples: allow configurable uds socket paths Update unix domain socket examples to show configurable paths. It demonstrates a way of configuring the UDS client using the ustr crate to meet the closure capture requirements of the connector service_fn. Fixes: #1611 Refs: #1612 --- examples/Cargo.toml | 3 ++- examples/src/uds/client.rs | 13 ++++++++++--- examples/src/uds/server.rs | 8 ++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 5c3e7e8b5..0274f1cbd 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -287,7 +287,7 @@ grpc-web = ["dep:tonic-web", "dep:bytes", "dep:http", "dep:hyper", "dep:tracing- tracing = ["dep:tracing", "dep:tracing-subscriber"] hyper-warp = ["dep:either", "dep:tower", "dep:hyper", "dep:http", "dep:http-body", "dep:warp"] hyper-warp-multiplex = ["hyper-warp"] -uds = ["tokio-stream/net", "dep:tower", "dep:hyper"] +uds = ["tokio-stream/net", "dep:tower", "dep:hyper", "dep:ustr"] streaming = ["tokio-stream", "dep:h2"] mock = ["tokio-stream", "dep:tower"] tower = ["dep:hyper", "dep:tower", "dep:http"] @@ -337,6 +337,7 @@ tokio-rustls = { version = "0.24.0", optional = true } hyper-rustls = { version = "0.24.0", features = ["http2"], optional = true } rustls-pemfile = { version = "1", optional = true } tower-http = { version = "0.4", optional = true } +ustr = { version = "1.0.0", optional = true } [build-dependencies] tonic-build = { path = "../tonic-build", features = ["prost"] } diff --git a/examples/src/uds/client.rs b/examples/src/uds/client.rs index e78531ac4..6e3ee176b 100644 --- a/examples/src/uds/client.rs +++ b/examples/src/uds/client.rs @@ -5,21 +5,28 @@ pub mod hello_world { } use hello_world::{greeter_client::GreeterClient, HelloRequest}; +use std::env; #[cfg(unix)] use tokio::net::UnixStream; use tonic::transport::{Endpoint, Uri}; use tower::service_fn; +use ustr::ustr; #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { + // Path needs to be a static str due to service_fn FnMut. + let path = match env::var("EXAMPLE_UDS_PATH") { + Ok(path) => ustr(path.as_ref()).as_str(), + Err(_) => "/tmp/tonic/helloworld" + }; + println!("Using path {}", path); + // We will ignore this uri because uds do not use it // if your connector does use the uri it will be provided // as the request to the `MakeConnection`. let channel = Endpoint::try_from("http://[::]:50051")? - .connect_with_connector(service_fn(|_: Uri| { - let path = "/tmp/tonic/helloworld"; - + .connect_with_connector(service_fn(move |_: Uri| { // Connect to a Uds socket UnixStream::connect(path) })) diff --git a/examples/src/uds/server.rs b/examples/src/uds/server.rs index ccf9c91a8..c8c76a998 100644 --- a/examples/src/uds/server.rs +++ b/examples/src/uds/server.rs @@ -1,6 +1,6 @@ #![cfg_attr(not(unix), allow(unused_imports))] -use std::path::Path; +use std::{env, path::Path}; #[cfg(unix)] use tokio::net::UnixListener; #[cfg(unix)] @@ -43,9 +43,9 @@ impl Greeter for MyGreeter { #[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { - let path = "/tmp/tonic/helloworld"; - - std::fs::create_dir_all(Path::new(path).parent().unwrap())?; + let path = env::var("EXAMPLE_UDS_PATH").unwrap_or("/tmp/tonic/helloworld".to_owned()); + std::fs::create_dir_all(Path::new(&path).parent().unwrap())?; + println!("Using path {}", path); let greeter = MyGreeter::default(); From d4b9e6d7fc8903aefec6cda1dd18e6e5c619eef3 Mon Sep 17 00:00:00 2001 From: Kristopher Wuollett Date: Fri, 2 Feb 2024 14:19:33 -0600 Subject: [PATCH 2/2] chore: fix formatting --- examples/src/uds/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/src/uds/client.rs b/examples/src/uds/client.rs index 6e3ee176b..ac7422537 100644 --- a/examples/src/uds/client.rs +++ b/examples/src/uds/client.rs @@ -18,7 +18,7 @@ async fn main() -> Result<(), Box> { // Path needs to be a static str due to service_fn FnMut. let path = match env::var("EXAMPLE_UDS_PATH") { Ok(path) => ustr(path.as_ref()).as_str(), - Err(_) => "/tmp/tonic/helloworld" + Err(_) => "/tmp/tonic/helloworld", }; println!("Using path {}", path);