Skip to content

Commit

Permalink
examples: allow configurable uds socket paths
Browse files Browse the repository at this point in the history
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: hyperium#1611
Refs: hyperium#1612
  • Loading branch information
kriswuollett committed Jan 30, 2024
1 parent c30cb78 commit 144f105
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
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(),
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

0 comments on commit 144f105

Please sign in to comment.