Skip to content

Commit

Permalink
chore(tonic): Refactor method to get addr
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Dec 9, 2023
1 parent c1443e2 commit fdf0462
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions tonic/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,23 +211,19 @@ impl<T> Request<T> {
/// This currently only works on the server side.
#[cfg(feature = "transport")]
pub fn local_addr(&self) -> Option<SocketAddr> {
#[cfg(feature = "tls")]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().local_addr())
})
}

#[cfg(not(feature = "tls"))]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr())
let addr = self
.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.local_addr());

if cfg!(feature = "tls") {
addr.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()

Check failure on line 222 in tonic/src/request.rs

View workflow job for this annotation

GitHub Actions / check (ubuntu-latest)

cannot find type `TlsConnectInfo` in this scope
.and_then(|i| i.get_ref().local_addr())
})
} else {
addr
}
}

Expand All @@ -238,23 +234,19 @@ impl<T> Request<T> {
/// This currently only works on the server side.
#[cfg(feature = "transport")]
pub fn remote_addr(&self) -> Option<SocketAddr> {
#[cfg(feature = "tls")]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()
.and_then(|i| i.get_ref().remote_addr())
})
}

#[cfg(not(feature = "tls"))]
{
self.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr())
let addr = self
.extensions()
.get::<TcpConnectInfo>()
.and_then(|i| i.remote_addr());

if cfg!(feature = "tls") {
addr.or_else(|| {
self.extensions()
.get::<TlsConnectInfo<TcpConnectInfo>>()

Check failure on line 245 in tonic/src/request.rs

View workflow job for this annotation

GitHub Actions / check (ubuntu-latest)

cannot find type `TlsConnectInfo` in this scope
.and_then(|i| i.get_ref().remote_addr())
})
} else {
addr
}
}

Expand Down

0 comments on commit fdf0462

Please sign in to comment.