polyc-runtime 2026.9.6

Shared Unix-coherence runtime for polychrome binaries: logging, health/metrics side-server, signals.
Documentation
//! `TCP_NODELAY` for the plaintext Connect listeners.
//!
//! An HTTP/2 peer writes each frame with its own syscall: a HEADERS frame,
//! then a small DATA frame, then the payload. With Nagle's algorithm on, the
//! kernel holds the second small segment until the first one is
//! acknowledged, and the receiver delays that acknowledgement by up to 40 ms
//! when it has nothing to send back. Each small frame that follows another
//! small frame then waits a full delayed-ACK timer.
//!
//! `connectrpc::axum::serve_tls` sets `TCP_NODELAY` on every accepted
//! stream for exactly this reason. The plaintext `axum::serve` path does not,
//! so a native-mode turn paid two such stalls per model call on the
//! control-plane-to-harness hop: the harness's model request and its output
//! frame each sat behind a delayed ACK. [`listener`] gives the plaintext path
//! the same setting the mutual-TLS path already has.

use axum::serve::{ListenerExt as _, TapIo};
use tokio::net::{TcpListener, TcpStream};

/// Wraps `listener` so every accepted stream has `TCP_NODELAY` set.
///
/// A stream whose option cannot be set is logged and served anyway, the same
/// posture as `connectrpc::axum::serve_tls`: a missing socket option is a
/// latency defect, not a reason to refuse a peer.
#[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);
    }
}