-
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.
refactor(transport): Move channel feature to channel module
- Loading branch information
Showing
17 changed files
with
208 additions
and
195 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
File renamed without changes.
3 changes: 2 additions & 1 deletion
3
tonic/src/transport/service/connection.rs → ...c/transport/channel/service/connection.rs
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
9 changes: 5 additions & 4 deletions
9
tonic/src/transport/service/connector.rs → ...rc/transport/channel/service/connector.rs
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
File renamed without changes.
File renamed without changes.
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,64 @@ | ||
use std::io::{self, IoSlice}; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use hyper::client::connect::{Connected as HyperConnected, Connection}; | ||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; | ||
|
||
use crate::transport::service::io::Io; | ||
|
||
pub(crate) struct BoxedIo(Pin<Box<dyn Io>>); | ||
|
||
impl BoxedIo { | ||
pub(in crate::transport) fn new<I: Io>(io: I) -> Self { | ||
BoxedIo(Box::pin(io)) | ||
} | ||
} | ||
|
||
impl Connection for BoxedIo { | ||
fn connected(&self) -> HyperConnected { | ||
HyperConnected::new() | ||
} | ||
} | ||
|
||
#[cfg(feature = "channel")] | ||
impl AsyncRead for BoxedIo { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut ReadBuf<'_>, | ||
) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_read(cx, buf) | ||
} | ||
} | ||
|
||
#[cfg(feature = "channel")] | ||
impl AsyncWrite for BoxedIo { | ||
fn poll_write( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<io::Result<usize>> { | ||
Pin::new(&mut self.0).poll_write(cx, buf) | ||
} | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_flush(cx) | ||
} | ||
|
||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_shutdown(cx) | ||
} | ||
|
||
fn poll_write_vectored( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
bufs: &[IoSlice<'_>], | ||
) -> Poll<Result<usize, io::Error>> { | ||
Pin::new(&mut self.0).poll_write_vectored(cx, bufs) | ||
} | ||
|
||
fn is_write_vectored(&self) -> bool { | ||
self.0.is_write_vectored() | ||
} | ||
} |
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,26 @@ | ||
mod add_origin; | ||
pub(crate) use self::add_origin::AddOrigin; | ||
|
||
mod connector; | ||
pub(crate) use self::connector::Connector; | ||
|
||
mod connection; | ||
pub(crate) use self::connection::Connection; | ||
|
||
mod discover; | ||
pub(crate) use self::discover::DynamicServiceStream; | ||
|
||
pub(crate) mod executor; | ||
pub(crate) use self::executor::{Executor, SharedExec}; | ||
|
||
pub(crate) mod io; | ||
|
||
mod reconnect; | ||
|
||
mod user_agent; | ||
pub(crate) use self::user_agent::UserAgent; | ||
|
||
#[cfg(feature = "tls")] | ||
mod tls; | ||
#[cfg(feature = "tls")] | ||
pub(crate) use self::tls::TlsConnector; |
File renamed without changes.
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,92 @@ | ||
use std::fmt; | ||
use std::io::Cursor; | ||
use std::sync::Arc; | ||
|
||
use rustls_pki_types::ServerName; | ||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio_rustls::rustls::RootCertStore; | ||
use tokio_rustls::{rustls::ClientConfig, TlsConnector as RustlsConnector}; | ||
|
||
use super::io::BoxedIo; | ||
use crate::transport::service::tls::{add_certs_from_pem, load_identity, ALPN_H2}; | ||
use crate::transport::tls::{Certificate, Identity}; | ||
|
||
#[derive(Debug)] | ||
enum TlsError { | ||
H2NotNegotiated, | ||
} | ||
|
||
impl fmt::Display for TlsError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
TlsError::H2NotNegotiated => write!(f, "HTTP/2 was not negotiated."), | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for TlsError {} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct TlsConnector { | ||
config: Arc<ClientConfig>, | ||
domain: Arc<ServerName<'static>>, | ||
} | ||
|
||
impl TlsConnector { | ||
pub(crate) fn new( | ||
ca_cert: Option<Certificate>, | ||
identity: Option<Identity>, | ||
domain: &str, | ||
) -> Result<Self, crate::Error> { | ||
let builder = ClientConfig::builder(); | ||
let mut roots = RootCertStore::empty(); | ||
|
||
#[cfg(feature = "tls-roots")] | ||
roots.add_parsable_certificates(rustls_native_certs::load_native_certs()?); | ||
|
||
#[cfg(feature = "tls-webpki-roots")] | ||
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); | ||
|
||
if let Some(cert) = ca_cert { | ||
add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?; | ||
} | ||
|
||
let builder = builder.with_root_certificates(roots); | ||
let mut config = match identity { | ||
Some(identity) => { | ||
let (client_cert, client_key) = load_identity(identity)?; | ||
builder.with_client_auth_cert(client_cert, client_key)? | ||
} | ||
None => builder.with_no_client_auth(), | ||
}; | ||
|
||
config.alpn_protocols.push(ALPN_H2.into()); | ||
Ok(Self { | ||
config: Arc::new(config), | ||
domain: Arc::new(ServerName::try_from(domain)?.to_owned()), | ||
}) | ||
} | ||
|
||
pub(crate) async fn connect<I>(&self, io: I) -> Result<BoxedIo, crate::Error> | ||
where | ||
I: AsyncRead + AsyncWrite + Send + Unpin + 'static, | ||
{ | ||
let io = RustlsConnector::from(self.config.clone()) | ||
.connect(self.domain.as_ref().to_owned(), io) | ||
.await?; | ||
|
||
let (_, session) = io.get_ref(); | ||
if session.alpn_protocol() != Some(ALPN_H2) { | ||
return Err(TlsError::H2NotNegotiated)?; | ||
} | ||
|
||
Ok(BoxedIo::new(io)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "channel")] | ||
impl fmt::Debug for TlsConnector { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("TlsConnector").finish() | ||
} | ||
} |
File renamed without changes.
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
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 |
---|---|---|
@@ -1,34 +1,13 @@ | ||
#[cfg(feature = "channel")] | ||
mod add_origin; | ||
#[cfg(feature = "channel")] | ||
mod connection; | ||
#[cfg(feature = "channel")] | ||
mod connector; | ||
#[cfg(feature = "channel")] | ||
mod discover; | ||
#[cfg(feature = "channel")] | ||
pub(crate) mod executor; | ||
pub(crate) mod grpc_timeout; | ||
mod io; | ||
#[cfg(feature = "channel")] | ||
mod reconnect; | ||
pub(crate) mod io; | ||
mod router; | ||
#[cfg(feature = "tls")] | ||
mod tls; | ||
#[cfg(feature = "channel")] | ||
mod user_agent; | ||
pub(super) mod tls; | ||
|
||
pub(crate) use self::grpc_timeout::GrpcTimeout; | ||
pub(crate) use self::io::ServerIo; | ||
#[cfg(feature = "tls")] | ||
pub(crate) use self::tls::TlsAcceptor; | ||
#[cfg(all(feature = "channel", feature = "tls"))] | ||
pub(crate) use self::tls::TlsConnector; | ||
#[cfg(feature = "channel")] | ||
pub(crate) use self::{ | ||
add_origin::AddOrigin, connection::Connection, connector::Connector, | ||
discover::DynamicServiceStream, executor::SharedExec, user_agent::UserAgent, | ||
}; | ||
|
||
pub use self::router::Routes; | ||
pub use self::router::RoutesBuilder; |
Oops, something went wrong.