Skip to content

std::net: update tcp deferaccept delay type to Duration. #140482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions library/std/src/os/net/linux_ext/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::sealed::Sealed;
use crate::sys_common::AsInner;
use crate::time::Duration;
use crate::{io, net};

/// Os-specific extensions for [`TcpStream`]
Expand Down Expand Up @@ -60,6 +61,8 @@ pub trait TcpStreamExt: Sealed {
/// connections without data to process.
/// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
/// no necessity to set it after the `listen` call.
/// Note that the delay is expressed as Duration from user's perspective
/// the call rounds it to its nearest second.
///
/// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
///
Expand All @@ -69,14 +72,15 @@ pub trait TcpStreamExt: Sealed {
/// #![feature(tcp_deferaccept)]
/// use std::net::TcpStream;
/// use std::os::linux::net::TcpStreamExt;
/// use std::time::Duration;
///
/// let stream = TcpStream::connect("127.0.0.1:8080")
/// .expect("Couldn't connect to the server...");
/// stream.set_deferaccept(1).expect("set_deferaccept call failed");
/// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
/// ```
#[unstable(feature = "tcp_deferaccept", issue = "119639")]
#[cfg(target_os = "linux")]
fn set_deferaccept(&self, accept: u32) -> io::Result<()>;
fn set_deferaccept(&self, accept: Duration) -> io::Result<()>;

/// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
///
Expand All @@ -88,15 +92,16 @@ pub trait TcpStreamExt: Sealed {
/// #![feature(tcp_deferaccept)]
/// use std::net::TcpStream;
/// use std::os::linux::net::TcpStreamExt;
/// use std::time::Duration;
///
/// let stream = TcpStream::connect("127.0.0.1:8080")
/// .expect("Couldn't connect to the server...");
/// stream.set_deferaccept(1).expect("set_deferaccept call failed");
/// assert_eq!(stream.deferaccept().unwrap_or(0), 1);
/// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
/// assert_eq!(stream.deferaccept().unwrap(), Duration::from_secs(1u64));
/// ```
#[unstable(feature = "tcp_deferaccept", issue = "119639")]
#[cfg(target_os = "linux")]
fn deferaccept(&self) -> io::Result<u32>;
fn deferaccept(&self) -> io::Result<Duration>;
}

#[unstable(feature = "tcp_quickack", issue = "96256")]
Expand All @@ -113,12 +118,12 @@ impl TcpStreamExt for net::TcpStream {
}

#[cfg(target_os = "linux")]
fn set_deferaccept(&self, accept: u32) -> io::Result<()> {
fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
self.as_inner().as_inner().set_deferaccept(accept)
}

#[cfg(target_os = "linux")]
fn deferaccept(&self) -> io::Result<u32> {
fn deferaccept(&self) -> io::Result<Duration> {
self.as_inner().as_inner().deferaccept()
}
}
11 changes: 7 additions & 4 deletions library/std/src/os/net/linux_ext/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn deferaccept() {
use crate::net::test::next_test_ip4;
use crate::net::{TcpListener, TcpStream};
use crate::os::net::linux_ext::tcp::TcpStreamExt;
use crate::time::Duration;

macro_rules! t {
($e:expr) => {
Expand All @@ -43,10 +44,12 @@ fn deferaccept() {
}

let addr = next_test_ip4();
let one = Duration::from_secs(1u64);
let zero = Duration::from_secs(0u64);
let _listener = t!(TcpListener::bind(&addr));
let stream = t!(TcpStream::connect(&("localhost", addr.port())));
stream.set_deferaccept(1).expect("set_deferaccept failed");
assert_eq!(stream.deferaccept().unwrap(), 1);
stream.set_deferaccept(0).expect("set_deferaccept failed");
assert_eq!(stream.deferaccept().unwrap(), 0);
stream.set_deferaccept(one).expect("set_deferaccept failed");
assert_eq!(stream.deferaccept().unwrap(), one);
stream.set_deferaccept(zero).expect("set_deferaccept failed");
assert_eq!(stream.deferaccept().unwrap(), zero);
}
13 changes: 9 additions & 4 deletions library/std/src/sys/net/connection/socket/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,19 @@ impl Socket {

// bionic libc makes no use of this flag
#[cfg(target_os = "linux")]
pub fn set_deferaccept(&self, accept: u32) -> io::Result<()> {
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, accept as c_int)
pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
setsockopt(
self,
libc::IPPROTO_TCP,
libc::TCP_DEFER_ACCEPT,
accept.as_secs_f64().round() as c_int,
)
}

#[cfg(target_os = "linux")]
pub fn deferaccept(&self) -> io::Result<u32> {
pub fn deferaccept(&self) -> io::Result<Duration> {
let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)?;
Ok(raw as u32)
Ok(Duration::from_secs(raw.try_into().unwrap()))
}

#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
Expand Down
Loading