Skip to content

Commit 026d57a

Browse files
Kai MastNoah-Kennedy
andauthored
io: add TcpStream::set_nodelay (#192)
Exposes `net::TcpStream::set_nodelay` like in `tokio::net` and `std::net`. Co-authored-by: Noah Kennedy <nkennedy@cloudflare.com>
1 parent e257f73 commit 026d57a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/io/socket.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ impl Socket {
206206
std::mem::forget(s);
207207
result
208208
}
209+
210+
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
211+
let socket_ref = socket2::SockRef::from(self);
212+
socket_ref.set_nodelay(nodelay)
213+
}
209214
}
210215

211216
impl AsRawFd for Socket {

src/net/tcp/stream.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ impl TcpStream {
207207
pub fn shutdown(&self, how: std::net::Shutdown) -> io::Result<()> {
208208
self.inner.shutdown(how)
209209
}
210+
211+
/// Sets the value of the TCP_NODELAY option on this socket.
212+
///
213+
/// If set, this option disables the Nagle algorithm. This means that segments are always sent
214+
/// as soon as possible, even if there is only a small amount of data. When not set, data is
215+
/// buffered until there is a sufficient amount to send out, thereby avoiding the frequent
216+
/// sending of small packets.
217+
pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
218+
self.inner.set_nodelay(nodelay)
219+
}
210220
}
211221

212222
impl FromRawFd for TcpStream {

0 commit comments

Comments
 (0)