Skip to content

Commit e9cab49

Browse files
authored
feat(server): remove AddrStream struct (#2869)
remove addrstream type, it provides no benefit over tokio::net::tcpstream Closes #2850
1 parent 2c7344a commit e9cab49

File tree

4 files changed

+11
-140
lines changed

4 files changed

+11
-140
lines changed

src/server/conn.rs

-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ cfg_feature! {
8181
pub(super) use self::upgrades::UpgradeableConnection;
8282
}
8383

84-
#[cfg(feature = "tcp")]
85-
pub use super::tcp::{AddrIncoming, AddrStream};
86-
8784
/// A lower-level configuration of the HTTP protocol.
8885
///
8986
/// This structure is used to configure options for an HTTP server connection.

src/server/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
//! use hyper::{Body, Request, Response, Server};
9393
//! use hyper::service::{make_service_fn, service_fn};
9494
//! # #[cfg(feature = "runtime")]
95-
//! use hyper::server::conn::AddrStream;
95+
//! use tokio::net::TcpStream;
9696
//!
9797
//! #[derive(Clone)]
9898
//! struct AppContext {
@@ -115,14 +115,14 @@
115115
//! };
116116
//!
117117
//! // A `MakeService` that produces a `Service` to handle each connection.
118-
//! let make_service = make_service_fn(move |conn: &AddrStream| {
118+
//! let make_service = make_service_fn(move |conn: &TcpStream| {
119119
//! // We have to clone the context to share it with each invocation of
120120
//! // `make_service`. If your data doesn't implement `Clone` consider using
121121
//! // an `std::sync::Arc`.
122122
//! let context = context.clone();
123123
//!
124124
//! // You can grab the address of the incoming connection like so.
125-
//! let addr = conn.remote_addr();
125+
//! let addr = conn.peer_addr().unwrap();
126126
//!
127127
//! // Create a `Service` for responding to the request.
128128
//! let service = service_fn(move |req| {

src/server/tcp.rs

+5-131
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ use std::io;
33
use std::net::{SocketAddr, TcpListener as StdTcpListener};
44
use std::time::Duration;
55

6-
use tokio::net::TcpListener;
6+
use tokio::net::{TcpListener, TcpStream};
77
use tokio::time::Sleep;
88
use tracing::{debug, error, trace};
99

1010
use crate::common::{task, Future, Pin, Poll};
1111

12-
#[allow(unreachable_pub)] // https://github.com/rust-lang/rust/issues/57411
13-
pub use self::addr_stream::AddrStream;
1412
use super::accept::Accept;
1513

1614
/// A stream of connections from binding to an address.
@@ -98,7 +96,7 @@ impl AddrIncoming {
9896
self.sleep_on_errors = val;
9997
}
10098

101-
fn poll_next_(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<AddrStream>> {
99+
fn poll_next_(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<TcpStream>> {
102100
// Check if a previous timeout is active that was set by IO errors.
103101
if let Some(ref mut to) = self.timeout {
104102
ready!(Pin::new(to).poll(cx));
@@ -107,7 +105,7 @@ impl AddrIncoming {
107105

108106
loop {
109107
match ready!(self.listener.poll_accept(cx)) {
110-
Ok((socket, remote_addr)) => {
108+
Ok((socket, _)) => {
111109
if let Some(dur) = self.tcp_keepalive_timeout {
112110
let socket = socket2::SockRef::from(&socket);
113111
let conf = socket2::TcpKeepalive::new().with_time(dur);
@@ -118,8 +116,7 @@ impl AddrIncoming {
118116
if let Err(e) = socket.set_nodelay(self.tcp_nodelay) {
119117
trace!("error trying to set TCP nodelay: {}", e);
120118
}
121-
let local_addr = socket.local_addr()?;
122-
return Poll::Ready(Ok(AddrStream::new(socket, remote_addr, local_addr)));
119+
return Poll::Ready(Ok(socket));
123120
}
124121
Err(e) => {
125122
// Connection errors can be ignored directly, continue by
@@ -155,7 +152,7 @@ impl AddrIncoming {
155152
}
156153

157154
impl Accept for AddrIncoming {
158-
type Conn = AddrStream;
155+
type Conn = TcpStream;
159156
type Error = io::Error;
160157

161158
fn poll_accept(
@@ -193,126 +190,3 @@ impl fmt::Debug for AddrIncoming {
193190
.finish()
194191
}
195192
}
196-
197-
mod addr_stream {
198-
use std::io;
199-
use std::net::SocketAddr;
200-
#[cfg(unix)]
201-
use std::os::unix::io::{AsRawFd, RawFd};
202-
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
203-
use tokio::net::TcpStream;
204-
205-
use crate::common::{task, Pin, Poll};
206-
207-
pin_project_lite::pin_project! {
208-
/// A transport returned yieled by `AddrIncoming`.
209-
#[derive(Debug)]
210-
pub struct AddrStream {
211-
#[pin]
212-
inner: TcpStream,
213-
pub(super) remote_addr: SocketAddr,
214-
pub(super) local_addr: SocketAddr
215-
}
216-
}
217-
218-
impl AddrStream {
219-
pub(super) fn new(
220-
tcp: TcpStream,
221-
remote_addr: SocketAddr,
222-
local_addr: SocketAddr,
223-
) -> AddrStream {
224-
AddrStream {
225-
inner: tcp,
226-
remote_addr,
227-
local_addr,
228-
}
229-
}
230-
231-
/// Returns the remote (peer) address of this connection.
232-
#[inline]
233-
pub fn remote_addr(&self) -> SocketAddr {
234-
self.remote_addr
235-
}
236-
237-
/// Returns the local address of this connection.
238-
#[inline]
239-
pub fn local_addr(&self) -> SocketAddr {
240-
self.local_addr
241-
}
242-
243-
/// Consumes the AddrStream and returns the underlying IO object
244-
#[inline]
245-
pub fn into_inner(self) -> TcpStream {
246-
self.inner
247-
}
248-
249-
/// Attempt to receive data on the socket, without removing that data
250-
/// from the queue, registering the current task for wakeup if data is
251-
/// not yet available.
252-
pub fn poll_peek(
253-
&mut self,
254-
cx: &mut task::Context<'_>,
255-
buf: &mut tokio::io::ReadBuf<'_>,
256-
) -> Poll<io::Result<usize>> {
257-
self.inner.poll_peek(cx, buf)
258-
}
259-
}
260-
261-
impl AsyncRead for AddrStream {
262-
#[inline]
263-
fn poll_read(
264-
self: Pin<&mut Self>,
265-
cx: &mut task::Context<'_>,
266-
buf: &mut ReadBuf<'_>,
267-
) -> Poll<io::Result<()>> {
268-
self.project().inner.poll_read(cx, buf)
269-
}
270-
}
271-
272-
impl AsyncWrite for AddrStream {
273-
#[inline]
274-
fn poll_write(
275-
self: Pin<&mut Self>,
276-
cx: &mut task::Context<'_>,
277-
buf: &[u8],
278-
) -> Poll<io::Result<usize>> {
279-
self.project().inner.poll_write(cx, buf)
280-
}
281-
282-
#[inline]
283-
fn poll_write_vectored(
284-
self: Pin<&mut Self>,
285-
cx: &mut task::Context<'_>,
286-
bufs: &[io::IoSlice<'_>],
287-
) -> Poll<io::Result<usize>> {
288-
self.project().inner.poll_write_vectored(cx, bufs)
289-
}
290-
291-
#[inline]
292-
fn poll_flush(self: Pin<&mut Self>, _cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
293-
// TCP flush is a noop
294-
Poll::Ready(Ok(()))
295-
}
296-
297-
#[inline]
298-
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
299-
self.project().inner.poll_shutdown(cx)
300-
}
301-
302-
#[inline]
303-
fn is_write_vectored(&self) -> bool {
304-
// Note that since `self.inner` is a `TcpStream`, this could
305-
// *probably* be hard-coded to return `true`...but it seems more
306-
// correct to ask it anyway (maybe we're on some platform without
307-
// scatter-gather IO?)
308-
self.inner.is_write_vectored()
309-
}
310-
}
311-
312-
#[cfg(unix)]
313-
impl AsRawFd for AddrStream {
314-
fn as_raw_fd(&self) -> RawFd {
315-
self.inner.as_raw_fd()
316-
}
317-
}
318-
}

src/service/make.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ where
108108
/// # async fn run() {
109109
/// use std::convert::Infallible;
110110
/// use hyper::{Body, Request, Response, Server};
111-
/// use hyper::server::conn::AddrStream;
111+
/// use tokio::net::TcpStream;
112112
/// use hyper::service::{make_service_fn, service_fn};
113113
///
114114
/// let addr = ([127, 0, 0, 1], 3000).into();
115115
///
116-
/// let make_svc = make_service_fn(|socket: &AddrStream| {
117-
/// let remote_addr = socket.remote_addr();
116+
/// let make_svc = make_service_fn(|socket: &TcpStream| {
117+
/// let remote_addr = socket.peer_addr().unwrap();
118118
/// async move {
119119
/// Ok::<_, Infallible>(service_fn(move |_: Request<Body>| async move {
120120
/// Ok::<_, Infallible>(

0 commit comments

Comments
 (0)