1
0
Fork 0

Add support of Unix socket

This commit is contained in:
Florian RICHER 2023-01-26 22:40:09 +01:00
parent 7a9b9c0f09
commit 368d5eca46
4 changed files with 59 additions and 10 deletions

2
Cargo.lock generated
View file

@ -122,8 +122,10 @@ version = "0.1.0"
dependencies = [ dependencies = [
"prost", "prost",
"tokio", "tokio",
"tokio-stream",
"tonic", "tonic",
"tonic-build", "tonic-build",
"tower",
] ]
[[package]] [[package]]

View file

@ -19,6 +19,8 @@ path = "src/client/main.rs"
[dependencies] [dependencies]
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } # Required for tonic tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } # Required for tonic
tokio-stream = { version = "0.1", features = ["net"] } # Required for tonic with unix socket
tower = "0.4" # Required for tonic with unix socket
prost = "0.11" # Required for tonic prost = "0.11" # Required for tonic
tonic = "0.8" tonic = "0.8"

View file

@ -1,13 +1,28 @@
use hello_world::greeter_client::GreeterClient; #![cfg_attr(not(unix), allow(unused_imports))]
use hello_world::HelloRequest;
pub mod hello_world { pub mod hello_world {
tonic::include_proto!("helloworld"); tonic::include_proto!("helloworld");
} }
use hello_world::{greeter_client::GreeterClient, HelloRequest};
#[cfg(unix)]
use tokio::net::UnixStream;
use tonic::transport::{Endpoint, Uri};
use tower::service_fn;
#[cfg(unix)]
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = GreeterClient::connect("http://[::1]:50051").await?; let channel = Endpoint::try_from("http://[::]:50051")?
.connect_with_connector(service_fn(|_: Uri| {
let path = "helloworld.sock";
// Connect to a Uds socket
UnixStream::connect(path)
}))
.await?;
let mut client = GreeterClient::new(channel);
let request = tonic::Request::new(HelloRequest { let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(), name: "Tonic".into(),
@ -18,4 +33,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("RESPONSE={:?}", response); println!("RESPONSE={:?}", response);
Ok(()) Ok(())
}
#[cfg(not(unix))]
fn main() {
panic!("The `uds` example only works on unix systems!");
} }

View file

@ -1,12 +1,23 @@
use tonic::{transport::Server, Request, Response, Status}; #![cfg_attr(not(unix), allow(unused_imports))]
use hello_world::greeter_server::{Greeter, GreeterServer}; use std::path::Path;
use hello_world::{HelloReply, HelloRequest}; #[cfg(unix)]
use tokio::net::UnixListener;
#[cfg(unix)]
use tokio_stream::wrappers::UnixListenerStream;
#[cfg(unix)]
use tonic::transport::server::UdsConnectInfo;
use tonic::{transport::Server, Request, Response, Status};
pub mod hello_world { pub mod hello_world {
tonic::include_proto!("helloworld"); tonic::include_proto!("helloworld");
} }
use hello_world::{
greeter_server::{Greeter, GreeterServer},
HelloReply, HelloRequest,
};
#[derive(Default)] #[derive(Default)]
pub struct MyGreeter {} pub struct MyGreeter {}
@ -16,7 +27,11 @@ impl Greeter for MyGreeter {
&self, &self,
request: Request<HelloRequest>, request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> { ) -> Result<Response<HelloReply>, Status> {
println!("Got a request from {:?}", request.remote_addr()); #[cfg(unix)]
{
let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap();
println!("Got a request {:?} with info {:?}", request, conn_info);
}
let reply = hello_world::HelloReply { let reply = hello_world::HelloReply {
message: format!("Hello {}!", request.into_inner().name), message: format!("Hello {}!", request.into_inner().name),
@ -25,17 +40,27 @@ impl Greeter for MyGreeter {
} }
} }
#[cfg(unix)]
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap(); let path = "helloworld.sock";
std::fs::create_dir_all(Path::new(path).parent().unwrap())?;
let greeter = MyGreeter::default(); let greeter = MyGreeter::default();
println!("GreeterServer listening on {}", addr); let uds = UnixListener::bind(path)?;
let uds_stream = UnixListenerStream::new(uds);
Server::builder() Server::builder()
.add_service(GreeterServer::new(greeter)) .add_service(GreeterServer::new(greeter))
.serve(addr) .serve_with_incoming(uds_stream)
.await?; .await?;
Ok(()) Ok(())
}
#[cfg(not(unix))]
fn main() {
panic!("The `uds` example only works on unix systems!");
} }