use axum::serve::{ListenerExt as _, TapIo};
use tokio::net::{TcpListener, TcpStream};
#[must_use]
pub fn listener(
listener: TcpListener,
) -> TapIo<TcpListener, impl FnMut(&mut TcpStream) + Send + 'static> {
listener.tap_io(|stream| {
if let Err(error) = stream.set_nodelay(true) {
tracing::warn!(%error, "failed to set TCP_NODELAY on an accepted connection");
}
})
}
#[cfg(test)]
mod tests {
#![allow(clippy::pedantic, clippy::nursery, missing_docs)]
use axum::serve::Listener as _;
#[tokio::test]
async fn an_accepted_stream_has_nodelay_set() {
let bound = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = bound.local_addr().unwrap();
let mut wrapped = super::listener(bound);
let client = tokio::net::TcpStream::connect(addr).await.unwrap();
let (accepted, _) = wrapped.accept().await;
assert!(accepted.nodelay().unwrap());
drop(client);
}
}