Skip to content

Commit

Permalink
refactor: do not require &mut self in AsyncUdpSocket::poll_send
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and djc committed Mar 24, 2023
1 parent 88897e7 commit 75524fc
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
9 changes: 5 additions & 4 deletions quinn-udp/src/fallback.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io::{self, IoSliceMut},
sync::Mutex,
time::Instant,
};

Expand All @@ -13,14 +14,14 @@ use super::{log_sendmsg_error, RecvMeta, UdpSockRef, UdpState, IO_ERROR_LOG_INTE
/// reduced performance compared to that enabled by some target-specific interfaces.
#[derive(Debug)]
pub struct UdpSocketState {
last_send_error: Instant,
last_send_error: Mutex<Instant>,
}

impl UdpSocketState {
pub fn new() -> Self {
let now = Instant::now();
Self {
last_send_error: now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now),
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)),
}
}

Expand All @@ -29,7 +30,7 @@ impl UdpSocketState {
}

pub fn send(
&mut self,
&self,
socket: UdpSockRef<'_>,
_state: &UdpState,
transmits: &[Transmit],
Expand Down Expand Up @@ -58,7 +59,7 @@ impl UdpSocketState {
// Those are not fatal errors, since the
// configuration can be dynamically changed.
// - Destination unreachable errors have been observed for other
log_sendmsg_error(&mut self.last_send_error, e, transmit);
log_sendmsg_error(&self.last_send_error, e, transmit);
sent += 1;
}
}
Expand Down
8 changes: 6 additions & 2 deletions quinn-udp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::os::unix::io::AsRawFd;
use std::os::windows::io::AsRawSocket;
use std::{
net::{IpAddr, Ipv6Addr, SocketAddr},
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Mutex,
},
time::{Duration, Instant},
};

Expand Down Expand Up @@ -123,11 +126,12 @@ const IO_ERROR_LOG_INTERVAL: Duration = std::time::Duration::from_secs(60);
/// Logging will only be performed if at least [`IO_ERROR_LOG_INTERVAL`]
/// has elapsed since the last error was logged.
fn log_sendmsg_error(
last_send_error: &mut Instant,
last_send_error: &Mutex<Instant>,
err: impl core::fmt::Debug,
transmit: &Transmit,
) {
let now = Instant::now();
let last_send_error = &mut *last_send_error.lock().expect("poisend lock");
if now.saturating_duration_since(*last_send_error) > IO_ERROR_LOG_INTERVAL {
*last_send_error = now;
warn!(
Expand Down
17 changes: 10 additions & 7 deletions quinn-udp/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{
mem::{self, MaybeUninit},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
os::unix::io::AsRawFd,
sync::atomic::{AtomicBool, AtomicUsize},
sync::{
atomic::{AtomicBool, AtomicUsize},
Mutex,
},
time::Instant,
};

Expand All @@ -26,14 +29,14 @@ type IpTosTy = libc::c_int;
/// platforms.
#[derive(Debug)]
pub struct UdpSocketState {
last_send_error: Instant,
last_send_error: Mutex<Instant>,
}

impl UdpSocketState {
pub fn new() -> Self {
let now = Instant::now();
Self {
last_send_error: now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now),
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)),
}
}

Expand All @@ -42,12 +45,12 @@ impl UdpSocketState {
}

pub fn send(
&mut self,
&self,
socket: UdpSockRef<'_>,
state: &UdpState,
transmits: &[Transmit],
) -> Result<usize, io::Error> {
send(state, socket.0, &mut self.last_send_error, transmits)
send(state, socket.0, &self.last_send_error, transmits)
}

pub fn recv(
Expand Down Expand Up @@ -148,7 +151,7 @@ fn send(
#[allow(unused_variables)] // only used on Linux
state: &UdpState,
io: SockRef<'_>,
last_send_error: &mut Instant,
last_send_error: &Mutex<Instant>,
transmits: &[Transmit],
) -> io::Result<usize> {
#[allow(unused_mut)] // only mutable on FeeBSD
Expand Down Expand Up @@ -252,7 +255,7 @@ fn send(
fn send(
state: &UdpState,
io: SockRef<'_>,
last_send_error: &mut Instant,
last_send_error: &Mutex<Instant>,
transmits: &[Transmit],
) -> io::Result<usize> {
let mut hdr: libc::msghdr = unsafe { mem::zeroed() };
Expand Down
9 changes: 5 additions & 4 deletions quinn-udp/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
io::{self, IoSliceMut},
mem,
os::windows::io::AsRawSocket,
sync::Mutex,
time::Instant,
};

Expand All @@ -13,14 +14,14 @@ use super::{log_sendmsg_error, RecvMeta, UdpSockRef, UdpState, IO_ERROR_LOG_INTE
/// QUIC-friendly UDP interface for Windows
#[derive(Debug)]
pub struct UdpSocketState {
last_send_error: Instant,
last_send_error: Mutex<Instant>,
}

impl UdpSocketState {
pub fn new() -> Self {
let now = Instant::now();
Self {
last_send_error: now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now),
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)),
}
}

Expand Down Expand Up @@ -81,7 +82,7 @@ impl UdpSocketState {
}

pub fn send(
&mut self,
&self,
socket: UdpSockRef<'_>,
_state: &UdpState,
transmits: &[Transmit],
Expand All @@ -106,7 +107,7 @@ impl UdpSocketState {

// Other errors are ignored, since they will ususally be handled
// by higher level retransmits and timeouts.
log_sendmsg_error(&mut self.last_send_error, e, transmit);
log_sendmsg_error(&self.last_send_error, e, transmit);
sent += 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion quinn/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait AsyncUdpSocket: Send + Debug + 'static {
/// Send UDP datagrams from `transmits`, or register to be woken if sending may succeed in the
/// future
fn poll_send(
&mut self,
&self,
state: &UdpState,
cx: &mut Context,
transmits: &[Transmit],
Expand Down
2 changes: 1 addition & 1 deletion quinn/src/runtime/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct UdpSocket {

impl AsyncUdpSocket for UdpSocket {
fn poll_send(
&mut self,
&self,
state: &udp::UdpState,
cx: &mut Context,
transmits: &[proto::Transmit],
Expand Down
4 changes: 2 additions & 2 deletions quinn/src/runtime/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ struct UdpSocket {

impl AsyncUdpSocket for UdpSocket {
fn poll_send(
&mut self,
&self,
state: &udp::UdpState,
cx: &mut Context,
transmits: &[proto::Transmit],
) -> Poll<io::Result<usize>> {
let inner = &mut self.inner;
let inner = &self.inner;
let io = &self.io;
loop {
ready!(io.poll_send_ready(cx))?;
Expand Down

0 comments on commit 75524fc

Please sign in to comment.