-
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(channel): Make channel feature additive (#1574)
* feat(channel): Make channel feature additive * chore(channel): Move add_origin service module to channel module * chore(channel): Move user_agent service to channel module * chore(channel): Move reconnect service to channel module * chore(channel): Move connection service to channel module * chore(channel): Move discover service to channel module * feat(channel): Remove unused Connected implement for BoxedIo * chore(channel): Move channel part of io service to channel module * chore(channel): Move channel part of tls service to channel module * chore(channel): Move connector service to channel module * chore(channel): Move executor service to channel module * chore(channel): Clean up scope of channel service module * chore(channel): Clean up importing in channel service * chore(doc): Update feature flag document about transport and channel
- Loading branch information
Showing
22 changed files
with
242 additions
and
210 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
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
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
6 changes: 3 additions & 3 deletions
6
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
3 changes: 1 addition & 2 deletions
3
tonic/src/transport/service/discover.rs → ...src/transport/channel/service/discover.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.
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,67 @@ | ||
use std::io::{self, IoSlice}; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use hyper::rt; | ||
use hyper_util::client::legacy::connect::{Connected as HyperConnected, Connection}; | ||
|
||
pub(in crate::transport) trait Io: | ||
rt::Read + rt::Write + Send + 'static | ||
{ | ||
} | ||
|
||
impl<T> Io for T where T: rt::Read + rt::Write + Send + 'static {} | ||
|
||
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() | ||
} | ||
} | ||
|
||
impl rt::Read for BoxedIo { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: rt::ReadBufCursor<'_>, | ||
) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.0).poll_read(cx, buf) | ||
} | ||
} | ||
|
||
impl rt::Write 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,28 @@ | ||
mod add_origin; | ||
use self::add_origin::AddOrigin; | ||
|
||
mod user_agent; | ||
use self::user_agent::UserAgent; | ||
|
||
mod reconnect; | ||
use self::reconnect::Reconnect; | ||
|
||
mod connection; | ||
pub(super) use self::connection::Connection; | ||
|
||
mod discover; | ||
pub(super) use self::discover::DynamicServiceStream; | ||
|
||
mod io; | ||
use self::io::BoxedIo; | ||
|
||
mod connector; | ||
pub(crate) use self::connector::{ConnectError, Connector}; | ||
|
||
mod executor; | ||
pub(super) use self::executor::{Executor, SharedExec}; | ||
|
||
#[cfg(feature = "tls")] | ||
mod tls; | ||
#[cfg(feature = "tls")] | ||
pub(super) 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,83 @@ | ||
use std::fmt; | ||
use std::io::Cursor; | ||
use std::sync::Arc; | ||
|
||
use hyper_util::rt::TokioIo; | ||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio_rustls::{ | ||
rustls::{pki_types::ServerName, ClientConfig, RootCertStore}, | ||
TlsConnector as RustlsConnector, | ||
}; | ||
|
||
use super::io::BoxedIo; | ||
use crate::transport::service::tls::{add_certs_from_pem, load_identity, TlsError, ALPN_H2}; | ||
use crate::transport::tls::{Certificate, Identity}; | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct TlsConnector { | ||
config: Arc<ClientConfig>, | ||
domain: Arc<ServerName<'static>>, | ||
assume_http2: bool, | ||
} | ||
|
||
impl TlsConnector { | ||
pub(crate) fn new( | ||
ca_certs: Vec<Certificate>, | ||
identity: Option<Identity>, | ||
domain: &str, | ||
assume_http2: bool, | ||
) -> 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()); | ||
|
||
for cert in ca_certs { | ||
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()), | ||
assume_http2, | ||
}) | ||
} | ||
|
||
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?; | ||
|
||
// Generally we require ALPN to be negotiated, but if the user has | ||
// explicitly set `assume_http2` to true, we'll allow it to be missing. | ||
let (_, session) = io.get_ref(); | ||
let alpn_protocol = session.alpn_protocol(); | ||
if !(alpn_protocol == Some(ALPN_H2) || self.assume_http2) { | ||
return Err(TlsError::H2NotNegotiated.into()); | ||
} | ||
Ok(BoxedIo::new(TokioIo::new(io))) | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.