Skip to content

Commit 37c7bc2

Browse files
authored
Unrolled build for #140482
Rollup merge of #140482 - devnexen:tcp_deferaccept_toduration, r=joboet std::net: update tcp deferaccept delay type to Duration. See comment [here](#119639 (comment)).
2 parents 8d72d3e + 19d0e72 commit 37c7bc2

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

library/std/src/os/net/linux_ext/tcp.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
use crate::sealed::Sealed;
66
use crate::sys_common::AsInner;
7+
#[cfg(target_os = "linux")]
8+
use crate::time::Duration;
79
use crate::{io, net};
810

911
/// Os-specific extensions for [`TcpStream`]
@@ -59,11 +61,13 @@ pub trait TcpStreamExt: Sealed {
5961

6062
/// A socket listener will be awakened solely when data arrives.
6163
///
62-
/// The `accept` argument set the delay in seconds until the
64+
/// The `accept` argument set the maximum delay until the
6365
/// data is available to read, reducing the number of short lived
6466
/// connections without data to process.
6567
/// Contrary to other platforms `SO_ACCEPTFILTER` feature equivalent, there is
6668
/// no necessity to set it after the `listen` call.
69+
/// Note that the delay is expressed as Duration from user's perspective
70+
/// the call rounds it down to the nearest second expressible as a `c_int`.
6771
///
6872
/// See [`man 7 tcp`](https://man7.org/linux/man-pages/man7/tcp.7.html)
6973
///
@@ -73,16 +77,17 @@ pub trait TcpStreamExt: Sealed {
7377
/// #![feature(tcp_deferaccept)]
7478
/// use std::net::TcpStream;
7579
/// use std::os::linux::net::TcpStreamExt;
80+
/// use std::time::Duration;
7681
///
7782
/// let stream = TcpStream::connect("127.0.0.1:8080")
7883
/// .expect("Couldn't connect to the server...");
79-
/// stream.set_deferaccept(1).expect("set_deferaccept call failed");
84+
/// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
8085
/// ```
8186
#[unstable(feature = "tcp_deferaccept", issue = "119639")]
8287
#[cfg(target_os = "linux")]
83-
fn set_deferaccept(&self, accept: u32) -> io::Result<()>;
88+
fn set_deferaccept(&self, accept: Duration) -> io::Result<()>;
8489

85-
/// Gets the accept delay value (in seconds) of the `TCP_DEFER_ACCEPT` option.
90+
/// Gets the accept delay value of the `TCP_DEFER_ACCEPT` option.
8691
///
8792
/// For more information about this option, see [`TcpStreamExt::set_deferaccept`].
8893
///
@@ -92,15 +97,16 @@ pub trait TcpStreamExt: Sealed {
9297
/// #![feature(tcp_deferaccept)]
9398
/// use std::net::TcpStream;
9499
/// use std::os::linux::net::TcpStreamExt;
100+
/// use std::time::Duration;
95101
///
96102
/// let stream = TcpStream::connect("127.0.0.1:8080")
97103
/// .expect("Couldn't connect to the server...");
98-
/// stream.set_deferaccept(1).expect("set_deferaccept call failed");
99-
/// assert_eq!(stream.deferaccept().unwrap_or(0), 1);
104+
/// stream.set_deferaccept(Duration::from_secs(1u64)).expect("set_deferaccept call failed");
105+
/// assert_eq!(stream.deferaccept().unwrap(), Duration::from_secs(1u64));
100106
/// ```
101107
#[unstable(feature = "tcp_deferaccept", issue = "119639")]
102108
#[cfg(target_os = "linux")]
103-
fn deferaccept(&self) -> io::Result<u32>;
109+
fn deferaccept(&self) -> io::Result<Duration>;
104110
}
105111

106112
#[stable(feature = "tcp_quickack", since = "1.89.0")]
@@ -117,12 +123,12 @@ impl TcpStreamExt for net::TcpStream {
117123
}
118124

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

124130
#[cfg(target_os = "linux")]
125-
fn deferaccept(&self) -> io::Result<u32> {
131+
fn deferaccept(&self) -> io::Result<Duration> {
126132
self.as_inner().as_inner().deferaccept()
127133
}
128134
}

library/std/src/os/net/linux_ext/tests.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ fn deferaccept() {
3232
use crate::net::test::next_test_ip4;
3333
use crate::net::{TcpListener, TcpStream};
3434
use crate::os::net::linux_ext::tcp::TcpStreamExt;
35+
use crate::time::Duration;
3536

3637
macro_rules! t {
3738
($e:expr) => {
@@ -43,10 +44,12 @@ fn deferaccept() {
4344
}
4445

4546
let addr = next_test_ip4();
47+
let one = Duration::from_secs(1u64);
48+
let zero = Duration::from_secs(0u64);
4649
let _listener = t!(TcpListener::bind(&addr));
4750
let stream = t!(TcpStream::connect(&("localhost", addr.port())));
48-
stream.set_deferaccept(1).expect("set_deferaccept failed");
49-
assert_eq!(stream.deferaccept().unwrap(), 1);
50-
stream.set_deferaccept(0).expect("set_deferaccept failed");
51-
assert_eq!(stream.deferaccept().unwrap(), 0);
51+
stream.set_deferaccept(one).expect("set_deferaccept failed");
52+
assert_eq!(stream.deferaccept().unwrap(), one);
53+
stream.set_deferaccept(zero).expect("set_deferaccept failed");
54+
assert_eq!(stream.deferaccept().unwrap(), zero);
5255
}

library/std/src/sys/net/connection/socket/unix.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,15 @@ impl Socket {
485485

486486
// bionic libc makes no use of this flag
487487
#[cfg(target_os = "linux")]
488-
pub fn set_deferaccept(&self, accept: u32) -> io::Result<()> {
489-
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, accept as c_int)
488+
pub fn set_deferaccept(&self, accept: Duration) -> io::Result<()> {
489+
let val = cmp::min(accept.as_secs(), c_int::MAX as u64) as c_int;
490+
setsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT, val)
490491
}
491492

492493
#[cfg(target_os = "linux")]
493-
pub fn deferaccept(&self) -> io::Result<u32> {
494+
pub fn deferaccept(&self) -> io::Result<Duration> {
494495
let raw: c_int = getsockopt(self, libc::IPPROTO_TCP, libc::TCP_DEFER_ACCEPT)?;
495-
Ok(raw as u32)
496+
Ok(Duration::from_secs(raw as _))
496497
}
497498

498499
#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]

0 commit comments

Comments
 (0)