Skip to content

Commit ec66627

Browse files
committed
Add simple interface for busy_poll on Linux
1 parent 50fc51e commit ec66627

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/socket.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,21 @@ impl Socket {
381381
sys::set_nonblocking(self.as_raw(), nonblocking)
382382
}
383383

384+
/// Sets the busy polling timeout in microseconds for blocking socket operations.
385+
///
386+
/// When there is no data available, the socket will busy poll for the specified
387+
/// duration (in microseconds) before falling back to blocking behavior.
388+
///
389+
/// # Notes
390+
///
391+
/// - Requires `CAP_NET_ADMIN` capability to increase the value
392+
/// - Default value is controlled by `/proc/sys/net/core/busy_read`
393+
/// - Available since Linux 3.11
394+
#[cfg(target_os = "linux")]
395+
pub fn set_busy_poll(&self, busy_poll: u16) -> io::Result<()> {
396+
sys::set_busy_poll(self.as_raw(), busy_poll)
397+
}
398+
384399
/// Shuts down the read, write, or both halves of this connection.
385400
///
386401
/// This function will cause all pending and future I/O on the specified

src/sys/unix.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,11 @@ pub(crate) fn set_nonblocking(fd: RawSocket, nonblocking: bool) -> io::Result<()
993993
}
994994
}
995995

996+
#[cfg(target_os = "linux")]
997+
pub(crate) fn set_busy_poll(fd: RawSocket, busy_poll: u16) -> io::Result<()> {
998+
unsafe { setsockopt(fd, libc::SOL_SOCKET, libc::SO_BUSY_POLL, busy_poll as c_int) }
999+
}
1000+
9961001
pub(crate) fn shutdown(fd: RawSocket, how: Shutdown) -> io::Result<()> {
9971002
let how = match how {
9981003
Shutdown::Write => libc::SHUT_WR,

0 commit comments

Comments
 (0)