Skip to content

Commit

Permalink
fixup! tonic: implement the server Connected trait for tokio UnixStream
Browse files Browse the repository at this point in the history
  • Loading branch information
agreen17 committed Jan 13, 2022
1 parent 288a9a7 commit 4933210
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
38 changes: 1 addition & 37 deletions tonic/src/transport/server/conn.rs
Expand Up @@ -4,7 +4,7 @@ use tokio::net::TcpStream;

#[cfg(feature = "tls")]
use crate::transport::Certificate;
#[cfg(any(unix, feature = "tls"))]
#[cfg(feature = "tls")]
use std::sync::Arc;
#[cfg(feature = "tls")]
use tokio_rustls::{rustls::Session, server::TlsStream};
Expand Down Expand Up @@ -98,42 +98,6 @@ impl Connected for TcpStream {
}
}

/// Connection info for Unix domain socket streams.
///
/// This type will be accessible through [request extensions][ext] if you're using
/// a unix stream.
///
/// See [`Connected`] for more details.
///
/// [ext]: crate::Request::extensions
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[derive(Clone, Debug)]
pub struct UdsConnectInfo {
/// Peer address. This will be "unnamed" for client unix sockets.
pub peer_addr: Option<Arc<tokio::net::unix::SocketAddr>>,
/// Process credentials for the unix socket.
pub peer_cred: Option<tokio::net::unix::UCred>,
}

#[cfg(unix)]
impl Connected for tokio::net::UnixStream {
type ConnectInfo = UdsConnectInfo;

fn connect_info(&self) -> Self::ConnectInfo {
UdsConnectInfo {
peer_addr: self.peer_addr().ok().map(Arc::new),
peer_cred: self.peer_cred().ok(),
}
}
}

impl Connected for tokio::io::DuplexStream {
type ConnectInfo = ();

fn connect_info(&self) -> Self::ConnectInfo {}
}

#[cfg(feature = "tls")]
impl<T> Connected for TlsStream<T>
where
Expand Down
4 changes: 3 additions & 1 deletion tonic/src/transport/server/mod.rs
Expand Up @@ -6,6 +6,8 @@ mod recover_error;
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
mod tls;
#[cfg(unix)]
mod unix;

pub use conn::{Connected, TcpConnectInfo};
#[cfg(feature = "tls")]
Expand All @@ -18,7 +20,7 @@ pub use conn::TlsConnectInfo;
use super::service::TlsAcceptor;

#[cfg(unix)]
pub use conn::UdsConnectInfo;
pub use unix::UdsConnectInfo;

use incoming::TcpIncoming;

Expand Down
37 changes: 37 additions & 0 deletions tonic/src/transport/server/unix.rs
@@ -0,0 +1,37 @@
use super::Connected;
use std::sync::Arc;

/// Connection info for Unix domain socket streams.
///
/// This type will be accessible through [request extensions][ext] if you're using
/// a unix stream.
///
/// See [Connected] for more details.
///
/// [ext]: crate::Request::extensions
/// [Connected]: crate::transport::server::Connected
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[derive(Clone, Debug)]
pub struct UdsConnectInfo {
/// Peer address. This will be "unnamed" for client unix sockets.
pub peer_addr: Option<Arc<tokio::net::unix::SocketAddr>>,
/// Process credentials for the unix socket.
pub peer_cred: Option<tokio::net::unix::UCred>,
}

impl Connected for tokio::net::UnixStream {
type ConnectInfo = UdsConnectInfo;

fn connect_info(&self) -> Self::ConnectInfo {
UdsConnectInfo {
peer_addr: self.peer_addr().ok().map(Arc::new),
peer_cred: self.peer_cred().ok(),
}
}
}

impl Connected for tokio::io::DuplexStream {
type ConnectInfo = ();

fn connect_info(&self) -> Self::ConnectInfo {}
}

0 comments on commit 4933210

Please sign in to comment.