From 18c0c6c2773d297bc6e10b8fec7f2554a6f9ecab Mon Sep 17 00:00:00 2001 From: Matilda Smeds Date: Wed, 3 May 2023 21:14:12 +0200 Subject: [PATCH 1/3] net: Add nodelay methods on TcpSocket We add set_nodelay() and nodelay() methods on the tokio::net::TcpSocket. This way developers can set NODELAY option on the socket directly, without having to resort for workarounds, as described on the original issue.. Closes #5510 --- tokio/src/net/tcp/socket.rs | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index 1c36d3a88df..ae897cf3afb 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -404,6 +404,51 @@ impl TcpSocket { self.inner.linger() } + /// Sets the value of the TCP_NODELAY option on this socket. + /// + /// If set, this option disables the Nagle algorithm. This means that segments are always + /// sent as soon as possible, even if there is only a small amount of data. When not set, + /// data is buffered until there is a sufficient amount to send out, thereby avoiding + /// the frequent sending of small packets. + /// + /// # Examples + /// + /// ```no_run + /// use tokio::net::TcpSocket; + /// + /// # async fn dox() -> Result<(), Box> { + /// let socket = TcpSocket::new_v4()?; + /// + /// println!("{:?}", socket.nodelay()?); + /// # Ok(()) + /// # } + /// ``` + pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> { + self.inner.set_nodelay(nodelay) + } + + /// Gets the value of the `TCP_NODELAY` option on this socket. + /// + /// For more information about this option, see [`set_nodelay`]. + /// + /// [`set_nodelay`]: TcpSocket::set_nodelay + /// + /// # Examples + /// + /// ```no_run + /// use tokio::net::TcpSocket; + /// + /// # async fn dox() -> Result<(), Box> { + /// let stream = TcpSocket::new_v4()?; + /// + /// stream.set_nodelay(true)?; + /// # Ok(()) + /// # } + /// ``` + pub fn nodelay(&self) -> io::Result { + self.inner.nodelay() + } + /// Gets the value of the `IP_TOS` option for this socket. /// /// For more information about this option, see [`set_tos`]. From d20473eaac424f3afe041acf510b2284645dcfef Mon Sep 17 00:00:00 2001 From: Matilda Smeds Date: Thu, 4 May 2023 07:33:13 +0200 Subject: [PATCH 2/3] add backticks --- tokio/src/net/tcp/socket.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index ae897cf3afb..19c85056c8c 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -404,7 +404,7 @@ impl TcpSocket { self.inner.linger() } - /// Sets the value of the TCP_NODELAY option on this socket. + /// Sets the value of the `TCP_NODELAY` option on this socket. /// /// If set, this option disables the Nagle algorithm. This means that segments are always /// sent as soon as possible, even if there is only a small amount of data. When not set, @@ -848,4 +848,4 @@ cfg_windows! { TcpSocket { inner } } } -} +} \ No newline at end of file From 4a4eaa44c057dc1360b0a739bcbb99112b14aa60 Mon Sep 17 00:00:00 2001 From: Matilda Smeds Date: Thu, 4 May 2023 07:34:04 +0200 Subject: [PATCH 3/3] rustfmt --- tokio/src/net/tcp/socket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index 19c85056c8c..df792f9a615 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -848,4 +848,4 @@ cfg_windows! { TcpSocket { inner } } } -} \ No newline at end of file +}