Repository: rustfs/rustfs
File: rustfs/src/server/http.rs (Rust)

Code before:
                        res = listener.accept() => match res {
                            Ok(conn) => conn,
                            Err(err) => {
                                error!("error accepting connection: {err}");
                                continue;
                            }
                        },
                        _ = ctrl_c.as_mut() => {
                            info!("Ctrl-C received in worker thread");
                            let _ = shutdown_tx_clone.send(());
                            break;
                        },
                        _ = shutdown_rx.recv() => {
                            info!("Shutdown signal received in worker thread");
                            break;
                        }
                    }
                }
            };

            let socket_ref = SockRef::from(&socket);

            // Enable TCP Keepalive to detect dead clients (e.g. power loss)
            // Idle: 10s, Interval: 5s, Retries: 3 (where supported)

            #[cfg(not(any(target_os = "openbsd", target_os = "netbsd")))]
            let ka = TcpKeepalive::new()
                .with_time(Duration::from_secs(10))
                .with_interval(Duration::from_secs(5));

            // On OpenBSD and NetBSD, socket2 only supports configuring the initial
            // TCP keepalive timeout; intervals and retries cannot be set.
            #[cfg(any(target_os = "openbsd", target_os = "netbsd"))]
            let ka = TcpKeepalive::new().with_time(Duration::from_secs(10));

            #[cfg(not(any(target_os = "openbsd", target_os = "netbsd")))]
            let ka = ka.with_retries(3);
            if let Err(err) = socket_ref.set_tcp_keepalive(&ka) {
                warn!(?err, "Failed to set TCP_KEEPALIVE");
            }

            if let Err(err) = socket_ref.set_tcp_nodelay(true) {
                warn!(?err, "Failed to set TCP_NODELAY");
            }
            #[cfg(not(any(target_os = "openbsd", target_os = "netbsd")))]
            if let Err(err) = socket_ref.set_recv_buffer_size(4 * MI_B) {
                warn!(?err, "Failed to set set_recv_buffer_size");
            }
            #[cfg(not(any(target_os = "openbsd", target_os = "netbsd")))]
            if let Err(err) = socket_ref.set_send_buffer_size(4 * MI_B) {
                warn!(?err, "Failed to set set_send_buffer_size");

Review comment (suggestion):
`netbsd` https://github.com/rust-lang/socket2/blob/feac83ad8597d7cd5bf0a3e68983925a12b13ce0/src/lib.rs#L516

 

Code after:
                    tokio::select! {
                        res = listener.accept() => match res {
                            Ok(conn) => conn,
                            Err(err) => {
                                error!("error accepting connection: {err}");
                                continue;
                            }
                        },
                        _ = ctrl_c.as_mut() => {
                            info!("Ctrl-C received in worker thread");
                            let _ = shutdown_tx_clone.send(());
                            break;
                        },
                        _ = shutdown_rx.recv() => {
                            info!("Shutdown signal received in worker thread");
                            break;
                        }
                    }
                }
            };

            let socket_ref = SockRef::from(&socket);

            // Enable TCP Keepalive to detect dead clients (e.g. power loss)
            // Idle: 10s, Interval: 5s, Retries: 3
            let ka = {
                #[cfg(not(target_os = "openbsd"))]
                let ka = TcpKeepalive::new()
                    .with_time(Duration::from_secs(10))
                    .with_interval(Duration::from_secs(5))
                    .with_retries(3);

                // On OpenBSD socket2 only supports configuring the initial
                // TCP keepalive timeout; intervals and retries cannot be set.
                #[cfg(target_os = "openbsd")]
                let ka = TcpKeepalive::new().with_time(Duration::from_secs(10));

                ka
            };

            if let Err(err) = socket_ref.set_tcp_keepalive(&ka) {
                warn!(?err, "Failed to set TCP_KEEPALIVE");
            }

            if let Err(err) = socket_ref.set_tcp_nodelay(true) {
                warn!(?err, "Failed to set TCP_NODELAY");
            }
            #[cfg(not(any(target_os = "openbsd")))]
            if let Err(err) = socket_ref.set_recv_buffer_size(4 * MI_B) {
                warn!(?err, "Failed to set set_recv_buffer_size");
            }