-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop oldest udp message when buffer is full #128
Conversation
So I wanted to make sure that this behavior correctly matches existing network stacks so I wrote a test: use s2n_quic_core::interval_set::IntervalSet;
use socket2::{Domain, Socket, Type};
use std::{net::SocketAddr, time::Duration};
use tokio::{net::UdpSocket, time::timeout};
#[tokio::test]
async fn queue_behavior() -> std::io::Result<()> {
let packet_count = 1_000u32;
let rx_socket = {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
socket.set_recv_buffer_size(u16::MAX as _)?;
socket.set_nonblocking(true)?;
let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
socket.bind(&addr.into())?;
UdpSocket::from_std(socket.into())?
};
let tx_socket = UdpSocket::bind("0.0.0.0:0").await?;
let rx_addr = rx_socket.local_addr()?;
dbg!(rx_addr);
let mut payload = vec![0u8; 1200];
eprintln!("sending {packet_count} packets");
for i in 0..packet_count {
payload[..4].copy_from_slice(&i.to_be_bytes());
let _ = tx_socket.send_to(&payload, rx_addr).await;
}
eprintln!("receiving packets");
let mut packets = IntervalSet::new();
while let Ok(Ok((payload_len, _remote))) = timeout(
Duration::from_millis(1000),
rx_socket.recv_from(&mut payload),
)
.await
{
if payload_len < 4 {
continue;
}
let i = u32::from_be_bytes((payload[..4]).try_into().unwrap());
packets.insert_value(i).unwrap();
}
dbg!(packets);
Ok(())
} [package]
name = "udp-queue-test"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
tokio = { version = "1", features = ["full"] }
s2n-quic-core = "0.24"
socket2 = "0.5" Running on my Linux machine it turns out that my assumptions (and the TODO I left for that) don't match reality:
If the behavior was preferring the most recent packets I would expect that
|
Same behavior on macOS:
|
In that case we can close this MR. Thanks for actually verifying the behaviour! |
Sorry for not checking before I put the TODO 😄! |
Actually, if you could update your PR to just remove the TODO that would be helpful. Otherwise, I can later. |
7acc8eb
to
81a5d3b
Compare
Resolved following todo, which is also mentioned in issue #105 :
How:
Replaces
mpsc
queue withbroadcast
queue which has the behaviour we want. According to tokio docsTo use this channel there is a one public API change required.
Datagram
needs to implementClone
.