Skip to content

Commit 530e2dd

Browse files
committed
added TcpListener::bind_with_backlog to both sys_common and std::net
Also added TcpListener::DEFAULT_BACKLOG to document the default backlog (currently 128).
1 parent 8604ef0 commit 530e2dd

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

library/std/src/net/tcp.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,52 @@ impl fmt::Debug for TcpStream {
707707
}
708708

709709
impl TcpListener {
710+
/// Default "backlog" for [`TcpListener::bind`]. See
711+
/// [`TcpListener::bind_with_backlog`] for an explanation of backlog
712+
/// values.
713+
#[unstable(feature = "bind_with_backlog", issue = "none")]
714+
pub const DEFAULT_BACKLOG: usize = 128;
715+
710716
/// Creates a new `TcpListener` which will be bound to the specified
711717
/// address.
712718
///
713-
/// The returned listener is ready for accepting connections.
719+
/// The given backlog specifies the maximum number of outstanding
720+
/// connections that will be buffered in the OS waiting to be accepted by
721+
/// [`TcpListener::accept`]. The backlog argument overrides the default
722+
/// specified by [`TcpListener::DEFAULT_BACKLOG`]; that default is
723+
/// reasonable for most use cases.
724+
///
725+
/// This function is otherwise [`TcpListener::bind`]: see that
726+
/// documentation for full details of operation.
727+
///
728+
/// # Examples
729+
///
730+
/// Creates a TCP listener bound to `127.0.0.1:80` with a backlog of 1000:
731+
///
732+
/// ```no_run
733+
/// use std::net::TcpListener;
734+
///
735+
/// let listener = TcpListener::bind_with_backlog("127.0.0.1:80", 1000).unwrap();
736+
/// ```
737+
///
738+
/// # Errors
739+
///
740+
/// The specified backlog may be larger than supported by the underlying
741+
/// system. In this case an [`io::Error`] with
742+
/// [`io::ErrorKind::InvalidData`] will be returned.
743+
#[unstable(feature = "bind_with_backlog", issue = "none")]
744+
pub fn bind_with_backlog<A: ToSocketAddrs>(addr: A, backlog: usize) -> io::Result<TcpListener> {
745+
super::each_addr(addr, move |a| net_imp::TcpListener::bind_with_backlog(a, backlog))
746+
.map(TcpListener)
747+
}
748+
749+
/// Creates a new `TcpListener` which will be bound to the specified
750+
/// address. The returned listener is ready for accepting
751+
/// connections.
752+
///
753+
/// The listener will have a backlog given by
754+
/// [`TcpListener::DEFAULT_BACKLOG`]. See the documentation for
755+
/// [`TcpListener::bind_with_backlog`] for further information.
714756
///
715757
/// Binding with a port number of 0 will request that the OS assigns a port
716758
/// to this listener. The port allocated can be queried via the
@@ -748,7 +790,7 @@ impl TcpListener {
748790
/// ```
749791
#[stable(feature = "rust1", since = "1.0.0")]
750792
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
751-
super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener)
793+
Self::bind_with_backlog(addr, TcpListener::DEFAULT_BACKLOG)
752794
}
753795

754796
/// Returns the local socket address of this listener.

library/std/src/sys_common/net.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,17 @@ pub struct TcpListener {
363363
}
364364

365365
impl TcpListener {
366-
pub fn bind(addr: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
366+
pub fn bind_with_backlog(
367+
addr: io::Result<&SocketAddr>,
368+
backlog: usize,
369+
) -> io::Result<TcpListener> {
367370
let addr = addr?;
368371

372+
// Type-convert the backlog
373+
let backlog = backlog
374+
.try_into()
375+
.map_err(|e| crate::io::Error::new(crate::io::ErrorKind::InvalidData, e))?;
376+
369377
init();
370378

371379
let sock = Socket::new(addr, c::SOCK_STREAM)?;
@@ -385,7 +393,7 @@ impl TcpListener {
385393
cvt(unsafe { c::bind(sock.as_raw(), addrp, len as _) })?;
386394

387395
// Start listening
388-
cvt(unsafe { c::listen(sock.as_raw(), 128) })?;
396+
cvt(unsafe { c::listen(sock.as_raw(), backlog) })?;
389397
Ok(TcpListener { inner: sock })
390398
}
391399

0 commit comments

Comments
 (0)