Skip to content

Commit

Permalink
Merge pull request #7 from flokli/tonic-0.12
Browse files Browse the repository at this point in the history
add tonic 0.12 support
  • Loading branch information
vi authored Jul 9, 2024
2 parents 60bf55b + 3a3ee2c commit 7b6ea1d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"]
Expand All @@ -90,7 +94,7 @@ hyper014 = { version = "0.14.27", features = ["server", "http1"], package="hyper
tokio = { version = "1.29.1", features = ["macros", "rt", "io-util"] }
tokio-test = "0.4.2"
toml = "0.7.6"
tonic-health = "0.11.0"
tonic-health = "0.12.0"
tracing-subscriber = "0.3.17"

[[example]]
Expand Down
5 changes: 4 additions & 1 deletion examples/tonic.rs
Original file line number Diff line number Diff line change
@@ -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 tokio-listener supports different tonic versions, and
/// version-suffixes them.
/// In your code, the crate would probably just be called "tonic".
use tonic_012::transport::Server;
use tonic_health::server::health_reporter;

#[tokio::main(flavor = "current_thread")]
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/tonic011.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
37 changes: 37 additions & 0 deletions src/tonic012.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 7b6ea1d

Please sign in to comment.