-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transport): Allow custom IO and UDS example (#184)
Closes #136
- Loading branch information
1 parent
7077d8d
commit b90c340
Showing
12 changed files
with
306 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#[cfg(unix)] | ||
|
||
pub mod hello_world { | ||
tonic::include_proto!("helloworld"); | ||
} | ||
|
||
use hello_world::{greeter_client::GreeterClient, HelloRequest}; | ||
use http::Uri; | ||
use std::convert::TryFrom; | ||
use tokio::net::UnixStream; | ||
use tonic::transport::Endpoint; | ||
use tower::service_fn; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// 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("lttp://[::]:50051")? | ||
.connect_with_connector(service_fn(|_: Uri| { | ||
let path = "/tmp/tonic/helloworld"; | ||
|
||
// Connect to a Uds socket | ||
UnixStream::connect(path) | ||
})) | ||
.await?; | ||
|
||
let mut client = GreeterClient::new(channel); | ||
|
||
let request = tonic::Request::new(HelloRequest { | ||
name: "Tonic".into(), | ||
}); | ||
|
||
let response = client.say_hello(request).await?; | ||
|
||
println!("RESPONSE={:?}", response); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::path::Path; | ||
use tokio::net::UnixListener; | ||
use tonic::{transport::Server, Request, Response, Status}; | ||
|
||
pub mod hello_world { | ||
tonic::include_proto!("helloworld"); | ||
} | ||
|
||
use hello_world::{ | ||
greeter_server::{Greeter, GreeterServer}, | ||
HelloReply, HelloRequest, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct MyGreeter {} | ||
|
||
#[tonic::async_trait] | ||
impl Greeter for MyGreeter { | ||
async fn say_hello( | ||
&self, | ||
request: Request<HelloRequest>, | ||
) -> Result<Response<HelloReply>, Status> { | ||
println!("Got a request: {:?}", request); | ||
|
||
let reply = hello_world::HelloReply { | ||
message: format!("Hello {}!", request.into_inner().name).into(), | ||
}; | ||
Ok(Response::new(reply)) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let path = "/tmp/tonic/helloworld"; | ||
|
||
tokio::fs::create_dir_all(Path::new(path).parent().unwrap()).await?; | ||
|
||
let mut uds = UnixListener::bind(path)?; | ||
|
||
let greeter = MyGreeter::default(); | ||
|
||
Server::builder() | ||
.add_service(GreeterServer::new(greeter)) | ||
.serve_with_incoming(uds.incoming()) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use super::Server; | ||
use crate::transport::service::BoxedIo; | ||
use futures_core::Stream; | ||
use futures_util::stream::TryStreamExt; | ||
use hyper::server::{ | ||
accept::Accept, | ||
conn::{AddrIncoming, AddrStream}, | ||
}; | ||
use std::{ | ||
net::SocketAddr, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
time::Duration, | ||
}; | ||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
#[cfg(feature = "tls")] | ||
use tracing::error; | ||
|
||
#[cfg_attr(not(feature = "tls"), allow(unused_variables))] | ||
pub(crate) fn tcp_incoming<IO, IE>( | ||
incoming: impl Stream<Item = Result<IO, IE>>, | ||
server: Server, | ||
) -> impl Stream<Item = Result<BoxedIo, crate::Error>> | ||
where | ||
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static, | ||
IE: Into<crate::Error>, | ||
{ | ||
async_stream::try_stream! { | ||
futures_util::pin_mut!(incoming); | ||
|
||
while let Some(stream) = incoming.try_next().await? { | ||
#[cfg(feature = "tls")] | ||
{ | ||
if let Some(tls) = &server.tls { | ||
let io = match tls.accept(stream).await { | ||
Ok(io) => io, | ||
Err(error) => { | ||
error!(message = "Unable to accept incoming connection.", %error); | ||
continue | ||
}, | ||
}; | ||
yield BoxedIo::new(io); | ||
continue; | ||
} | ||
} | ||
|
||
yield BoxedIo::new(stream); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct TcpIncoming { | ||
inner: AddrIncoming, | ||
} | ||
|
||
impl TcpIncoming { | ||
pub(crate) fn new( | ||
addr: SocketAddr, | ||
nodelay: bool, | ||
keepalive: Option<Duration>, | ||
) -> Result<Self, crate::Error> { | ||
let mut inner = AddrIncoming::bind(&addr)?; | ||
inner.set_nodelay(nodelay); | ||
inner.set_keepalive(keepalive); | ||
Ok(TcpIncoming { inner }) | ||
} | ||
} | ||
|
||
impl Stream for TcpIncoming { | ||
type Item = Result<AddrStream, std::io::Error>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
Pin::new(&mut self.inner).poll_accept(cx) | ||
} | ||
} |
Oops, something went wrong.