feat(neshub): iroh 1.3, and send each frame as one datagram batch

send_many_datagrams queues a frame's fragments under one lock hold and
one driver wake instead of one per fragment. It also rejects the whole
frame on TooLarge, so a path that shrinks mid-frame no longer leaves half
a frame on the wire for the client to time out. Eviction behaviour is
unchanged. A refused PyroWave frame now keeps its sequence number, since
nothing went out under it. pyro_bench mirrors both ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DatCaptainHorse
2026-10-05 15:57:53 +03:00
co-authored by Claude Opus 5.5
parent 030dcc5a8c
commit 10fdeddc70
6 changed files with 79 additions and 43 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ name = "neshub"
path = "src/main.rs"
[dependencies]
iroh = "1.2"
iroh = "1.3"
anyhow.workspace = true
clap.workspace = true
+24 -11
View File
@@ -31,6 +31,9 @@ use nesprotocol::pyrowave::{
should_skip,
};
/// Datagrams taken per receive, as the client takes them.
const RECV_BATCH: usize = 64;
struct Opts {
mbps: u64,
fps: u64,
@@ -110,10 +113,16 @@ async fn send(conn: Connection, o: &Opts) -> Result<()> {
} else {
let buf = Bytes::from(d.buf);
let mut at = 0;
for len in d.lens {
conn.send_datagram(buf.slice(at..at + len))?;
at += len;
}
let batch: Vec<Bytes> = d
.lens
.iter()
.map(|&len| {
let datagram = buf.slice(at..at + len);
at += len;
datagram
})
.collect();
conn.send_many_datagrams(&batch)?;
seq = seq.wrapping_add(1);
sent += 1;
}
@@ -147,17 +156,21 @@ async fn receive(conn: Connection) -> Result<()> {
let (mut all_bytes, mut whole, mut partial, mut lost) = (0u64, 0u64, 0u64, 0u64);
let mut second = Instant::now();
let mut busy = Duration::ZERO;
let mut batch = vec![Bytes::new(); RECV_BATCH];
loop {
let d = match conn.read_datagram().await {
Ok(d) => d,
let n = match conn.read_many_datagrams(&mut batch).await {
Ok(n) => n,
Err(_) => break,
};
let t0 = Instant::now();
datagrams += 1;
bytes += d.len() as u64;
if let Some((h, p)) = decode_pyro_datagram(&d) {
let at = d.len() - p.len();
c.push(h, d.slice(at..), start.elapsed().as_micros() as u64);
for d in &mut batch[..n] {
let d = std::mem::take(d);
datagrams += 1;
bytes += d.len() as u64;
if let Some((h, p)) = decode_pyro_datagram(&d) {
let at = d.len() - p.len();
c.push(h, d.slice(at..), start.elapsed().as_micros() as u64);
}
}
busy += t0.elapsed();
if second.elapsed() >= Duration::from_secs(1) {
+23 -15
View File
@@ -102,9 +102,11 @@ impl DatagramSender {
/// used, minus the length prefix a datagram does not need.
///
/// A failure is reported but never retried. Retrying a real-time frame means
/// delivering it late, which is the behaviour this whole change removes. A
/// half-sent frame is abandoned where it stands; the receiver times its
/// fragments out and asks for a keyframe.
/// delivering it late, which is the behaviour this whole change removes. The
/// frame goes out whole or not at all: every fragment is queued under one
/// hold of the connection lock, so a path that shrinks mid-frame rejects the
/// frame rather than leaving half of it on the wire for the receiver to time
/// out.
pub fn send_frame(&self, seq: u16, body: &[u8]) -> Result<(), SendFrameError> {
// Ask QUIC what fits right now rather than assuming. The estimate moves
// over a connection's life as path MTU discovery runs, and it can shrink
@@ -142,18 +144,24 @@ impl DatagramSender {
buf.extend_from_slice(&body[start..end]);
}
for index in 0..total {
let start = index * payload_size;
let end = (start + payload_size).min(body.len());
let datagram = buf.split_to(DGRAM_HDR_LEN + (end - start)).freeze();
// Deliberately not `send_datagram_wait`: that waits for buffer space
// under congestion, which prioritises old datagrams over new ones.
// For live media the opposite is right — drop the backlog, send the
// frame that is actually current.
self.conn
.send_datagram(datagram)
.map_err(SendFrameError::Quic)?;
}
let datagrams: Vec<Bytes> = (0..total)
.map(|index| {
let start = index * payload_size;
let end = (start + payload_size).min(body.len());
buf.split_to(DGRAM_HDR_LEN + (end - start)).freeze()
})
.collect();
// One call for the whole frame: one lock hold and one driver wake rather
// than one per fragment, and a `TooLarge` rejects every fragment instead
// of the ones after the path shrank. Like `send_datagram`, it evicts the
// oldest queued datagrams to make room -- deliberately not the waiting
// kind, which prioritises old datagrams over new ones. For live media
// the opposite is right: drop the backlog, send the frame that is
// actually current.
self.conn
.send_many_datagrams(&datagrams)
.map_err(SendFrameError::Quic)?;
Ok(())
}
+17 -10
View File
@@ -71,16 +71,23 @@ impl PyroSender {
// Taking the `Vec` is free, and each datagram is a slice of it.
let buf = Bytes::from(datagrams.buf);
let mut at = 0;
for len in datagrams.lens {
debug_assert!(len >= PYRO_DGRAM_HDR_LEN);
let datagram = buf.slice(at..at + len);
at += len;
// Not `send_datagram_wait`, for the reason `dgram::send_frame`
// gives; the guard above is what keeps this from evicting.
if let Err(e) = self.conn.send_datagram(datagram) {
debug!("{label}: PyroWave frame {} cut short: {e}", frame.frame);
break;
}
let batch: Vec<Bytes> = datagrams
.lens
.iter()
.map(|&len| {
debug_assert!(len >= PYRO_DGRAM_HDR_LEN);
let datagram = buf.slice(at..at + len);
at += len;
datagram
})
.collect();
// Evicting, for the reason `dgram::send_frame` gives; the guard above is
// what keeps it from evicting anything. The batch is queued whole or
// not at all, so a refused frame put nothing on the wire and its
// sequence number goes to the next frame.
if let Err(e) = self.conn.send_many_datagrams(&batch) {
debug!("{label}: PyroWave frame {} not sent: {e}", frame.frame);
return;
}
self.seq = self.seq.wrapping_add(1);
}