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

examples: allow configurable uds socket paths #1613

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"] }
13 changes: 10 additions & 3 deletions examples/src/uds/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
// 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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to say converting a path to a string should be possible without a third party crate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would something like a OnceLock<String> work? AFAICT a &'static str is required. I used ustr because in an actual application one may have multiple copies of the same client but with different endpoints -- I think you'd end up reimplementing ustr in that case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps there is something obvious I missed, but when using a String, and for the reason I filed that other linked issue...

expected a closure that implements the `FnMut` trait, but this closure only implements `FnOnce`
required for `ServiceFn<{closure@examples/src/uds/client.rs:29:44: 29:57}>` to implement `Service<Uri>`
required for `ServiceFn<{closure@examples/src/uds/client.rs:29:44: 29:57}>` to implement `MakeConnection<Uri>`rustc[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20%5B3%5D?3#file%3A%2F%2F%2FUsers%2Fkris%2FCode%2Fkriswuollett%2Ftonic%2Fexamples%2Fsrc%2Fuds%2Fclient.rs)
client.rs(29, 10): the requirement to implement `FnMut` derives from here
client.rs(31, 33): closure is `FnOnce` because it moves the variable `path` out of its environment

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)
}))
Expand Down
8 changes: 4 additions & 4 deletions examples/src/uds/server.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -43,9 +43,9 @@ impl Greeter for MyGreeter {
#[cfg(unix)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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();

Expand Down