Skip to content

Commit a789532

Browse files
committed
listen: backlog improvements after review
- backlog size is now a constant, not a function - improved formatting
1 parent 7bc3493 commit a789532

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

src/net/tcp/listener.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use std::{fmt, io};
1313

1414
use crate::io_source::IoSource;
1515
use crate::net::TcpStream;
16-
use crate::sys::get_listen_backlog_size;
1716
#[cfg(any(unix, target_os = "hermit"))]
1817
use crate::sys::tcp::set_reuseaddr;
1918
#[cfg(not(target_os = "wasi"))]
2019
use crate::sys::tcp::{bind, listen, new_for_addr};
20+
use crate::sys::LISTEN_BACKLOG_SIZE;
2121
use crate::{event, sys, Interest, Registry, Token};
2222

2323
/// A structure representing a socket server
@@ -79,7 +79,7 @@ impl TcpListener {
7979
set_reuseaddr(&listener.inner, true)?;
8080

8181
bind(&listener.inner, addr)?;
82-
listen(&listener.inner, get_listen_backlog_size())?;
82+
listen(&listener.inner, LISTEN_BACKLOG_SIZE)?;
8383
Ok(listener)
8484
}
8585

src/sys/mod.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,40 @@ cfg_not_os_poll! {
9898
/// - Default values vary depending on the OS and its version. Common defaults
9999
/// include: -1, 128, 1024, and 4096.
100100
///
101+
// Here is the original code from the standard library
102+
// https://github.com/rust-lang/rust/blob/4f808ba6bf9f1c8dde30d009e73386d984491587/library/std/src/os/unix/net/listener.rs#L72
103+
//
101104
#[allow(dead_code)]
102-
pub(crate) const fn get_listen_backlog_size() -> i32 {
103-
// Here is the original code from the standard library
104-
// https://github.com/rust-lang/rust/blob/4f808ba6bf9f1c8dde30d009e73386d984491587/library/std/src/os/unix/net/listener.rs#L72
105-
#[cfg(any(
106-
target_os = "windows",
107-
target_os = "redox",
108-
target_os = "espidf",
109-
target_os = "horizon"
110-
))]
111-
const BACKLOG_SIZE: i32 = 128;
105+
#[cfg(any(
106+
target_os = "windows",
107+
target_os = "redox",
108+
target_os = "espidf",
109+
target_os = "horizon"
110+
))]
111+
pub(crate) const LISTEN_BACKLOG_SIZE: i32 = 128;
112112

113-
#[allow(dead_code)]
114-
#[cfg(any(
115-
// Silently capped to `/proc/sys/net/core/somaxconn`.
116-
target_os = "linux",
117-
// Silently capped to `kern.ipc.soacceptqueue`.
118-
target_os = "freebsd",
119-
// Silently capped to `kern.somaxconn sysctl`.
120-
target_os = "openbsd",
121-
// Silently capped to the default 128.
122-
target_vendor = "apple",
123-
))]
124-
const BACKLOG_SIZE: i32 = -1;
113+
#[allow(dead_code)]
114+
#[cfg(any(
115+
// Silently capped to `/proc/sys/net/core/somaxconn`.
116+
target_os = "linux",
117+
// Silently capped to `kern.ipc.soacceptqueue`.
118+
target_os = "freebsd",
119+
// Silently capped to `kern.somaxconn sysctl`.
120+
target_os = "openbsd",
121+
// Silently capped to the default 128.
122+
target_vendor = "apple",
123+
))]
124+
pub(crate) const LISTEN_BACKLOG_SIZE: i32 = -1;
125125

126-
#[allow(dead_code)]
127-
#[cfg(not(any(
128-
target_os = "windows",
129-
target_os = "redox",
130-
target_os = "espidf",
131-
target_os = "horizon",
132-
target_os = "linux",
133-
target_os = "freebsd",
134-
target_os = "openbsd",
135-
target_vendor = "apple",
136-
)))]
137-
const BACKLOG_SIZE: i32 = libc::SOMAXCONN;
138-
BACKLOG_SIZE
139-
}
126+
#[allow(dead_code)]
127+
#[cfg(not(any(
128+
target_os = "windows",
129+
target_os = "redox",
130+
target_os = "espidf",
131+
target_os = "horizon",
132+
target_os = "linux",
133+
target_os = "freebsd",
134+
target_os = "openbsd",
135+
target_vendor = "apple",
136+
)))]
137+
pub(crate) const LISTEN_BACKLOG_SIZE: i32 = libc::SOMAXCONN;

src/sys/unix/uds/listener.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::path::Path;
66
use std::{io, mem};
77

88
use crate::net::UnixStream;
9-
use crate::sys::get_listen_backlog_size;
109
use crate::sys::unix::net::new_socket;
1110
use crate::sys::unix::uds::{path_offset, unix_addr};
11+
use crate::sys::LISTEN_BACKLOG_SIZE;
1212

1313
pub(crate) fn bind_addr(address: &SocketAddr) -> io::Result<net::UnixListener> {
1414
let fd = new_socket(libc::AF_UNIX, libc::SOCK_STREAM)?;
@@ -17,7 +17,7 @@ pub(crate) fn bind_addr(address: &SocketAddr) -> io::Result<net::UnixListener> {
1717
let (unix_address, addrlen) = unix_addr(address);
1818
let sockaddr = &unix_address as *const libc::sockaddr_un as *const libc::sockaddr;
1919
syscall!(bind(fd, sockaddr, addrlen))?;
20-
syscall!(listen(fd, get_listen_backlog_size()))?;
20+
syscall!(listen(fd, LISTEN_BACKLOG_SIZE))?;
2121

2222
Ok(socket)
2323
}

0 commit comments

Comments
 (0)