From 0ffd6e94afdc4aa6a9f3a11ef8b78890855d022c Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 9 Jul 2024 11:18:48 +0300 Subject: [PATCH] add tonic 0.12 support This adds the `tonic012` feature flag. The tonic 0.11 one is renamed to tonic_011 internally. --- Cargo.toml | 8 ++++++-- examples/tonic.rs | 5 ++++- src/lib.rs | 4 ++++ src/tonic011.rs | 4 ++-- src/tonic012.rs | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/tonic012.rs diff --git a/Cargo.toml b/Cargo.toml index ee580ff..c6ff9c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,8 @@ serde = { version = "1.0.171", features = ["derive"] , optional = true} serde_with = { version = "3.0.0", optional = true} socket2 = {version="0.5.3", optional=true, features=["all"]} tokio = { version = "1.29.1", features = ["net", "io-std", "time", "sync"] } -tonic = { package = "tonic", version = "0.11.0", optional = true } +tonic_012 = { package = "tonic", version = "0.12.0", optional = true } +tonic_011 = { package = "tonic", version = "0.11.0", optional = true } tonic_010 = { package = "tonic", version = "0.10.2", optional = true } tracing = "0.1.37" axum07 = { version="0.7", package="axum", optional=true } @@ -73,7 +74,10 @@ socket_options = ["socket2"] tonic010 = ["dep:tonic_010"] ## Enable `tonic(v0.11)::transport::server::Connected` implementation for [`Connection`] -tonic011 = ["dep:tonic"] +tonic011 = ["dep:tonic_011"] + +## Enable `tonic(v0.12)::transport::server::Connected` implementation for [`Connection`] +tonic012 = ["dep:tonic_012"] ## Enable [`tokio_util::net::Listener`] implementation for [`Listener`]. tokio-util = ["dep:tokio-util"] diff --git a/examples/tonic.rs b/examples/tonic.rs index a59041e..7faafe8 100644 --- a/examples/tonic.rs +++ b/examples/tonic.rs @@ -1,5 +1,8 @@ /// A simple example of how to use tokio-listener with a tonic gRPC server. -use tonic::transport::Server; +/// Keep in mind this crate optionally pulls in different tonic versions, +/// and suffixes them with their version. In your example, the crate would +/// probably just be called "tonic". +use tonic_012::transport::Server; use tonic_health::server::health_reporter; #[tokio::main(flavor = "current_thread")] diff --git a/src/lib.rs b/src/lib.rs index daf5f67..9e70992 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,10 @@ mod tonic010; #[cfg_attr(docsrs_alt, doc(cfg(feature = "tonic011")))] mod tonic011; +#[cfg(feature = "tonic012")] +#[cfg_attr(docsrs_alt, doc(cfg(feature = "tonic012")))] +mod tonic012; + #[cfg(feature = "tokio-util")] #[cfg_attr(docsrs_alt, doc(cfg(feature = "tokio-util")))] mod tokioutil; diff --git a/src/tonic011.rs b/src/tonic011.rs index b5f88c6..3d25d32 100644 --- a/src/tonic011.rs +++ b/src/tonic011.rs @@ -1,6 +1,6 @@ #[cfg(all(feature = "unix", unix))] -use tonic::transport::server::UdsConnectInfo; -use tonic::transport::server::{Connected, TcpConnectInfo}; +use tonic_011::transport::server::UdsConnectInfo; +use tonic_011::transport::server::{Connected, TcpConnectInfo}; use crate::Connection; diff --git a/src/tonic012.rs b/src/tonic012.rs new file mode 100644 index 0000000..4264826 --- /dev/null +++ b/src/tonic012.rs @@ -0,0 +1,37 @@ +#[cfg(all(feature = "unix", unix))] +use tonic_012::transport::server::UdsConnectInfo; +use tonic_012::transport::server::{Connected, TcpConnectInfo}; + +use crate::Connection; + +#[derive(Clone)] +pub enum ListenerConnectInfo { + Tcp(TcpConnectInfo), + #[cfg(all(feature = "unix", unix))] + #[cfg_attr(docsrs_alt, doc(cfg(all(feature = "unix", unix))))] + Unix(UdsConnectInfo), + #[cfg(feature = "inetd")] + #[cfg_attr(docsrs_alt, doc(cfg(feature = "inetd")))] + Stdio, + Other, +} + +impl Connected for Connection { + type ConnectInfo = ListenerConnectInfo; + + fn connect_info(&self) -> Self::ConnectInfo { + if let Some(tcp_stream) = self.try_borrow_tcp() { + return ListenerConnectInfo::Tcp(tcp_stream.connect_info()); + } + #[cfg(all(feature = "unix", unix))] + if let Some(unix_stream) = self.try_borrow_unix() { + return ListenerConnectInfo::Unix(unix_stream.connect_info()); + } + #[cfg(feature = "inetd")] + if self.try_borrow_stdio().is_some() { + return ListenerConnectInfo::Stdio; + } + + ListenerConnectInfo::Other + } +}