Skip to content

Commit

Permalink
add tonic 0.12 support
Browse files Browse the repository at this point in the history
This adds the `tonic012` feature flag. The tonic 0.11 one is renamed
to tonic_011 internally.
  • Loading branch information
flokli committed Jul 9, 2024
1 parent 60bf55b commit 0ffd6e9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
8 changes: 6 additions & 2 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 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 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")]
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 0ffd6e9

Please sign in to comment.