Skip to content

Commit

Permalink
Remove tcp_nodelay from serve
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Nov 30, 2024
1 parent 0b34a14 commit c000243
Showing 1 changed file with 4 additions and 96 deletions.
100 changes: 4 additions & 96 deletions axum/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ where
listener: self.listener,
make_service: self.make_service,
signal,
tcp_nodelay: self.tcp_nodelay,
_marker: PhantomData,
}
}
Expand All @@ -217,34 +216,6 @@ where
}
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
impl<M, S> Serve<TcpListener, M, S> {
/// Instructs the server to set the value of the `TCP_NODELAY` option on every accepted connection.
///
/// See also [`TcpStream::set_nodelay`].
///
/// # Example
/// ```
/// use axum::{Router, routing::get};
///
/// # async {
/// let router = Router::new().route("/", get(|| async { "Hello, World!" }));
///
/// let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
/// axum::serve(listener, router)
/// .tcp_nodelay(true)
/// .await
/// .unwrap();
/// # };
/// ```
pub fn tcp_nodelay(self, nodelay: bool) -> Self {
Self {
tcp_nodelay: Some(nodelay),
..self
}
}
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
impl<L, M, S> Debug for Serve<L, M, S>
where
Expand Down Expand Up @@ -297,7 +268,6 @@ pub struct WithGracefulShutdown<L, M, S, F> {
listener: L,
make_service: M,
signal: F,
tcp_nodelay: Option<bool>,
_marker: PhantomData<S>,
}

Expand All @@ -312,39 +282,6 @@ where
}
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
impl<M, S, F> WithGracefulShutdown<TcpListener, M, S, F> {
/// Instructs the server to set the value of the `TCP_NODELAY` option on every accepted connection.
///
/// See also [`TcpStream::set_nodelay`].
///
/// # Example
/// ```
/// use axum::{Router, routing::get};
///
/// # async {
/// let router = Router::new().route("/", get(|| async { "Hello, World!" }));
///
/// let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
/// axum::serve(listener, router)
/// .with_graceful_shutdown(shutdown_signal())
/// .tcp_nodelay(true)
/// .await
/// .unwrap();
/// # };
///
/// async fn shutdown_signal() {
/// // ...
/// }
/// ```
pub fn tcp_nodelay(self, nodelay: bool) -> Self {
Self {
tcp_nodelay: Some(nodelay),
..self
}
}
}

#[cfg(all(feature = "tokio", any(feature = "http1", feature = "http2")))]
impl<L, M, S, F> Debug for WithGracefulShutdown<L, M, S, F>
where
Expand All @@ -358,20 +295,14 @@ where
listener,
make_service,
signal,
tcp_nodelay,
_marker: _,
} = self;

let mut s = f.debug_struct("WithGracefulShutdown");
s.field("listener", listener)
f.debug_struct("WithGracefulShutdown")
.field("listener", listener)
.field("make_service", make_service)
.field("signal", signal);

if TypeId::of::<L>() == TypeId::of::<TcpListener>() {
s.field("tcp_nodelay", tcp_nodelay);
}

s.finish()
.field("signal", signal)
.finish()
}
}

Expand All @@ -394,7 +325,6 @@ where
mut listener,
mut make_service,
signal,
tcp_nodelay,
_marker: _,
} = self;

Expand All @@ -418,14 +348,6 @@ where
}
};

if let Some(nodelay) = tcp_nodelay {
let tcp_stream: &tokio::net::TcpStream = <dyn std::any::Any>::downcast_ref(&io)
.expect("internal error: tcp_nodelay used with the wrong type of listener");
if let Err(err) = tcp_stream.set_nodelay(nodelay) {
trace!("failed to set TCP_NODELAY on incoming connection: {err:#}");
}
}

let io = TokioIo::new(io);

trace!("connection {remote_addr:?} accepted");
Expand Down Expand Up @@ -684,20 +606,6 @@ mod tests {
UnixListener::bind("").unwrap(),
handler.into_make_service_with_connect_info::<UdsConnectInfo>(),
);

// nodelay
serve(
TcpListener::bind(addr).await.unwrap(),
handler.into_service(),
)
.tcp_nodelay(true);

serve(
TcpListener::bind(addr).await.unwrap(),
handler.into_service(),
)
.with_graceful_shutdown(async { /*...*/ })
.tcp_nodelay(true);
}

async fn handler() {}
Expand Down

0 comments on commit c000243

Please sign in to comment.