Skip to content

Commit 8b38665

Browse files
mxindenclaude
authored andcommitted
fix(transport): back-pressure the outbound QUIC datagram queue
When the outgoing datagram queue was full, `add_datagram` head-dropped the oldest queued datagram. On the MASQUE connect-udp path that silently discards an inner-connection packet the application already handed us, which looks like loss to the inner connection and hurts upload throughput. Back-pressure instead of dropping. The queue stays a fixed length; `send_datagram` always queues the datagram and returns whether the queue still has room: - `Ok(true)`: queued, and space remains for more. - `Ok(false)`: queued, but the queue is now full. The caller should stop producing datagrams until it receives `OutgoingDatagramSpaceAvailable`. Nothing already queued is dropped. The datagram is accepted even when the queue is already at capacity, so a producer that has already built a datagram never has to hold it in a queue neqo cannot reach. `Ok(false)` is a high-watermark signal to stop, not a rejection. `OutgoingDatagramSpaceAvailable` fires once the queue drops back below capacity, whether a slot was freed by sending or by dropping a too-big datagram, so a sender is never left blocked while space already exists. It is surfaced to HTTP/3 clients as `Http3ClientEvent::OutgoingDatagramSpaceAvailable` and to HTTP/3 servers as `Http3ServerEvent::OutgoingDatagramSpaceAvailable`, analogous to `SendStreamWritable`. Scope: this covers the QUIC datagram queue only. The connect-udp HTTP DATAGRAM Capsule fallback (used when the peer offers no QUIC datagram support) still drops on a full control stream and reports success; giving it backpressure is left for a follow-up (see `write_datagram_capsule`). Removes the now-unreachable `OutgoingDatagramOutcome::DroppedQueueFull` variant and the `dropped_queue_full` stat. Fixes #2852
1 parent 2d9598a commit 8b38665

18 files changed

Lines changed: 660 additions & 93 deletions

File tree

‎neqo-http3/src/client_events.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ pub enum Http3ClientEvent {
115115
WebTransport(WebTransportEvent),
116116
/// `ConnectUdp` events
117117
ConnectUdp(ConnectUdpEvent),
118+
/// The outgoing QUIC datagram queue has space again after having been full.
119+
/// The application can resume sending HTTP/3 datagrams.
120+
OutgoingDatagramSpaceAvailable,
118121
}
119122

120123
#[derive(Debug, Default, Clone)]
@@ -333,6 +336,12 @@ impl Http3ClientEvents {
333336
self.events.push(event);
334337
}
335338

339+
/// Signal that the outgoing QUIC datagram queue has space again.
340+
pub(crate) fn datagram_space_available(&self) {
341+
self.events
342+
.push_unique(Http3ClientEvent::OutgoingDatagramSpaceAvailable);
343+
}
344+
336345
/// Add a new `StateChange` event.
337346
pub(crate) fn connection_state_change(&self, state: Http3State) {
338347
match state {

‎neqo-http3/src/connect_udp.rs‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,26 @@ pub trait ClientSession {
6262

6363
/// Send a connect-udp datagram.
6464
///
65+
/// # Returns
66+
///
67+
/// `Ok(true)` if the datagram was queued with room to spare, or `Ok(false)`
68+
/// if it was queued but the queue is now full and the sender should stop
69+
/// until an [`OutgoingDatagramSpaceAvailable`] event (backpressure).
70+
///
6571
/// # Errors
6672
///
6773
/// It may return [`Error::InvalidStreamId`] if a stream does not exist anymore.
6874
/// The function returns `TooMuchData` if the supply buffer is bigger than
6975
/// the allowed remote datagram size.
76+
///
77+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ClientEvent::OutgoingDatagramSpaceAvailable
7078
fn connect_udp_send_datagram<I: Into<DatagramTracking>>(
7179
&mut self,
7280
session_id: StreamId,
7381
buf: &[u8],
7482
id: I,
7583
now: Instant,
76-
) -> Res<()>;
84+
) -> Res<bool>;
7785
}
7886

7987
impl ClientSession for Http3Client {
@@ -118,7 +126,7 @@ impl ClientSession for Http3Client {
118126
buf: &[u8],
119127
id: I,
120128
now: Instant,
121-
) -> Res<()> {
129+
) -> Res<bool> {
122130
qtrace!("connect_udp_send_datagram session:{session_id:?}");
123131
let (conn, handler) = self.connection_and_handler();
124132
handler.connect_udp_send_datagram(conn, session_id, buf, id, now)
@@ -153,14 +161,20 @@ trait Handler {
153161
now: Instant,
154162
) -> Res<()>;
155163

164+
/// Returns `Ok(true)` if the datagram was queued with room to spare, or
165+
/// `Ok(false)` if it was queued but the queue is now full and the sender
166+
/// should stop until an [`OutgoingDatagramSpaceAvailable`] event
167+
/// (backpressure).
168+
///
169+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ClientEvent::OutgoingDatagramSpaceAvailable
156170
fn connect_udp_send_datagram<I: Into<DatagramTracking>>(
157171
&self,
158172
conn: &mut Connection,
159173
session_id: StreamId,
160174
buf: &[u8],
161175
id: I,
162176
now: Instant,
163-
) -> Res<()>;
177+
) -> Res<bool>;
164178
}
165179

166180
impl Handler for Http3Connection {
@@ -232,7 +246,7 @@ impl Handler for Http3Connection {
232246
buf: &[u8],
233247
id: I,
234248
now: Instant,
235-
) -> Res<()> {
249+
) -> Res<bool> {
236250
self.extended_connect_send_datagram(session_id, conn, buf, id, now)
237251
}
238252
}
@@ -256,14 +270,20 @@ pub(crate) trait ServerHandler {
256270
now: Instant,
257271
) -> Res<()>;
258272

273+
/// Returns `Ok(true)` if the datagram was queued with room to spare, or
274+
/// `Ok(false)` if it was queued but the queue is now full and the sender
275+
/// should stop until an [`OutgoingDatagramSpaceAvailable`] event
276+
/// (backpressure).
277+
///
278+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ServerEvent::OutgoingDatagramSpaceAvailable
259279
fn connect_udp_send_datagram<I: Into<DatagramTracking>>(
260280
&mut self,
261281
conn: &mut Connection,
262282
session_id: StreamId,
263283
buf: &[u8],
264284
id: I,
265285
now: Instant,
266-
) -> Res<()>;
286+
) -> Res<bool>;
267287
}
268288

269289
impl ServerHandler for Http3ServerHandler {
@@ -300,7 +320,7 @@ impl ServerHandler for Http3ServerHandler {
300320
buf: &[u8],
301321
id: I,
302322
now: Instant,
303-
) -> Res<()> {
323+
) -> Res<bool> {
304324
self.mark_needs_processing();
305325
self.base_handler_mut()
306326
.connect_udp_send_datagram(conn, session_id, buf, id, now)
@@ -391,7 +411,7 @@ impl ServerSession {
391411
buf: &[u8],
392412
id: I,
393413
now: Instant,
394-
) -> Res<()> {
414+
) -> Res<bool> {
395415
let session_id = self.stream_handler.stream_id();
396416
self.stream_handler
397417
.handler

‎neqo-http3/src/connection.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,14 +1630,20 @@ impl Http3Connection {
16301630
Ok(())
16311631
}
16321632

1633+
/// Returns `Ok(true)` if the datagram was queued with room to spare, or
1634+
/// `Ok(false)` if it was queued but the queue is now full and the sender
1635+
/// should stop until an [`OutgoingDatagramSpaceAvailable`] event
1636+
/// (backpressure).
1637+
///
1638+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ClientEvent::OutgoingDatagramSpaceAvailable
16331639
pub(crate) fn extended_connect_send_datagram<I: Into<DatagramTracking>>(
16341640
&self,
16351641
session_id: StreamId,
16361642
conn: &mut Connection,
16371643
buf: &[u8],
16381644
id: I,
16391645
now: Instant,
1640-
) -> Res<()> {
1646+
) -> Res<bool> {
16411647
self.validate_extended_connect_session(session_id)?
16421648
.borrow_mut()
16431649
.send_datagram(conn, buf, id, now)

‎neqo-http3/src/connection_client.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,9 @@ impl Http3Client {
915915
ConnectionEvent::Datagram(dgram) => {
916916
self.base_handler.handle_datagram(dgram);
917917
}
918+
ConnectionEvent::OutgoingDatagramSpaceAvailable => {
919+
self.events.datagram_space_available();
920+
}
918921
ConnectionEvent::SendStreamComplete { .. }
919922
| ConnectionEvent::OutgoingDatagramOutcome { .. }
920923
| ConnectionEvent::SconeUpdated(_)

‎neqo-http3/src/connection_server.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ impl Http3ServerHandler {
284284
}
285285
}
286286
ConnectionEvent::Datagram(dgram) => self.base_handler.handle_datagram(dgram),
287+
ConnectionEvent::OutgoingDatagramSpaceAvailable => {
288+
self.events.datagram_space_available();
289+
}
287290
ConnectionEvent::AuthenticationNeeded
288291
| ConnectionEvent::EchFallbackAuthenticationNeeded { .. }
289292
| ConnectionEvent::ZeroRttRejected

‎neqo-http3/src/features/extended_connect/session.rs‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,17 @@ impl Session {
390390
self.control_stream_send.send_data(conn, buf, now)
391391
}
392392

393+
/// # Returns
394+
///
395+
/// `Ok(true)` if the datagram was queued and space remains, or `Ok(false)`
396+
/// if it was queued but the outgoing QUIC datagram queue is now full and the
397+
/// producer should stop sending until it receives an
398+
/// [`OutgoingDatagramSpaceAvailable`] event (backpressure). Datagrams sent as
399+
/// HTTP DATAGRAM Capsules always report `Ok(true)`; see
400+
/// [`Protocol::write_datagram_capsule`].
401+
///
402+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ClientEvent::OutgoingDatagramSpaceAvailable
403+
///
393404
/// # Errors
394405
///
395406
/// Returns an error if:
@@ -401,7 +412,7 @@ impl Session {
401412
buf: &[u8],
402413
id: I,
403414
now: Instant,
404-
) -> Res<()> {
415+
) -> Res<bool> {
405416
qtrace!("[{self}] send_datagram state={:?}", self.state);
406417
if self.state != State::Active {
407418
qdebug!("[{self}]: cannot send datagram in {:?} state.", self.state);
@@ -411,22 +422,22 @@ impl Session {
411422

412423
if conn.remote_datagram_size() == 0 && self.protocol.datagram_capsule_support() {
413424
qtrace!("[{self}] remote_datagram_size is 0, trying HTTP DATAGRAM Capsule");
414-
return self.protocol.write_datagram_capsule(
415-
&mut self.control_stream_send,
416-
conn,
417-
buf,
418-
now,
419-
);
425+
// The Capsule path does not report backpressure; see
426+
// `Protocol::write_datagram_capsule`.
427+
return self
428+
.protocol
429+
.write_datagram_capsule(&mut self.control_stream_send, conn, buf, now)
430+
.map(|()| true);
420431
}
421432

422433
let mut dgram_data = Encoder::default();
423434
dgram_data.encode_varint(self.id.as_u64() / 4);
424435
self.protocol.write_datagram_prefix(&mut dgram_data);
425436
dgram_data.encode(buf);
426437

427-
conn.send_datagram(dgram_data.into(), id)?;
428-
qtrace!("[{self}] sent datagram via QUIC datagram");
429-
Ok(())
438+
let has_space = conn.send_datagram(dgram_data.into(), id)?;
439+
qtrace!("[{self}] sent datagram via QUIC datagram, has_space={has_space}");
440+
Ok(has_space)
430441
}
431442

432443
pub(crate) fn datagram(&self, datagram: Bytes) {
@@ -652,6 +663,11 @@ pub(crate) trait Protocol: Debug + Display {
652663
fn datagram_capsule_support(&self) -> bool;
653664

654665
/// Write a datagram as an HTTP DATAGRAM Capsule to the control stream.
666+
///
667+
/// NOTE: On a full control stream this drops the datagram and still reports
668+
/// success, so the Capsule path applies no backpressure. Unlike the QUIC
669+
/// datagram queue, it neither reports "no space" nor emits a resume event.
670+
/// To be fixed in the future.
655671
fn write_datagram_capsule(
656672
&self,
657673
_control_stream_send: &mut Box<dyn SendStream>,

‎neqo-http3/src/features/extended_connect/tests/webtransport/datagrams.rs‎

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
// option. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7-
use neqo_common::{Encoder, to_u64};
7+
use neqo_common::{Encoder, event::Provider as _, to_u64};
88
use neqo_transport::ConnectionParameters;
99
use test_fixture::now;
1010

1111
use crate::{
12+
Http3ClientEvent, Http3ServerEvent,
1213
features::extended_connect::tests::webtransport::{
1314
DATAGRAM_SIZE, WtTest, wt_default_parameters,
1415
},
@@ -27,8 +28,8 @@ fn do_datagram_test(wt: &mut WtTest, wt_session: &ServerSession) {
2728
Ok(DATAGRAM_SIZE - to_u64(Encoder::varint_len(wt_session.stream_id().as_u64())))
2829
);
2930

30-
assert_eq!(wt_session.send_datagram(DGRAM, None, now()), Ok(()));
31-
assert_eq!(wt.send_datagram(wt_session.stream_id(), DGRAM), Ok(()));
31+
assert_eq!(wt_session.send_datagram(DGRAM, None, now()), Ok(true));
32+
assert_eq!(wt.send_datagram(wt_session.stream_id(), DGRAM), Ok(true));
3233

3334
wt.exchange_packets();
3435
wt.check_datagram_received_client(wt_session.stream_id(), DGRAM);
@@ -75,3 +76,70 @@ fn max_datagram_size_smaller_than_session_prefix() {
7576
assert_eq!(wt_session.max_datagram_size(), Ok(0));
7677
assert_eq!(wt.max_datagram_size(wt_session.stream_id()), Ok(0));
7778
}
79+
80+
/// Filling the client's outgoing QUIC datagram queue and then draining it must
81+
/// surface [`OutgoingDatagramSpaceAvailable`].
82+
///
83+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ClientEvent::OutgoingDatagramSpaceAvailable
84+
#[test]
85+
fn outgoing_datagram_space_available_forwarded() {
86+
let mut wt = WtTest::new_with_params(
87+
wt_default_parameters().connection_parameters(
88+
ConnectionParameters::default()
89+
.datagram_size(DATAGRAM_SIZE)
90+
.outgoing_datagram_queue(1),
91+
),
92+
wt_default_parameters(),
93+
);
94+
let wt_session = wt.create_wt_session();
95+
96+
assert_eq!(wt.send_datagram(wt_session.stream_id(), DGRAM), Ok(false));
97+
assert!(
98+
!wt.client
99+
.events()
100+
.any(|e| matches!(e, Http3ClientEvent::OutgoingDatagramSpaceAvailable)),
101+
"resume event fired before the queue drained"
102+
);
103+
104+
wt.exchange_packets();
105+
assert!(
106+
wt.client
107+
.events()
108+
.any(|e| matches!(e, Http3ClientEvent::OutgoingDatagramSpaceAvailable)),
109+
"OutgoingDatagramSpaceAvailable was not forwarded to the HTTP/3 client"
110+
);
111+
}
112+
113+
/// The server side must forward the resume signal too. Filling the server's
114+
/// outgoing QUIC datagram queue and then draining it has to surface
115+
/// [`OutgoingDatagramSpaceAvailable`].
116+
///
117+
/// [`OutgoingDatagramSpaceAvailable`]: crate::Http3ServerEvent::OutgoingDatagramSpaceAvailable
118+
#[test]
119+
fn outgoing_datagram_space_available_forwarded_server() {
120+
let mut wt = WtTest::new_with_params(
121+
wt_default_parameters(),
122+
wt_default_parameters().connection_parameters(
123+
ConnectionParameters::default()
124+
.datagram_size(DATAGRAM_SIZE)
125+
.outgoing_datagram_queue(1),
126+
),
127+
);
128+
let wt_session = wt.create_wt_session();
129+
130+
assert_eq!(wt_session.send_datagram(DGRAM, None, now()), Ok(false));
131+
assert!(
132+
!wt.server
133+
.events()
134+
.any(|e| matches!(e, Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. })),
135+
"server resume event fired before the queue drained"
136+
);
137+
138+
wt.exchange_packets();
139+
assert!(
140+
wt.server
141+
.events()
142+
.any(|e| matches!(e, Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. })),
143+
"OutgoingDatagramSpaceAvailable was not forwarded to the HTTP/3 server"
144+
);
145+
}

‎neqo-http3/src/features/extended_connect/tests/webtransport/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ impl WtTest {
619619
self.client.webtransport_max_datagram_size(stream_id)
620620
}
621621

622-
fn send_datagram(&mut self, stream_id: StreamId, buf: &[u8]) -> Result<(), Error> {
622+
fn send_datagram(&mut self, stream_id: StreamId, buf: &[u8]) -> Result<bool, Error> {
623623
self.client
624624
.webtransport_send_datagram(stream_id, buf, None, now())
625625
}

‎neqo-http3/src/server.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ impl Http3Server {
245245
remove = true;
246246
}
247247
}
248+
Http3ServerConnEvent::OutgoingDatagramSpaceAvailable => {
249+
self.events.datagram_space_available(conn.clone());
250+
}
248251
Http3ServerConnEvent::PriorityUpdate {
249252
stream_id,
250253
priority,
@@ -1014,6 +1017,7 @@ mod tests {
10141017
| Http3ServerEvent::StateChange { .. }
10151018
| Http3ServerEvent::PriorityUpdate { .. }
10161019
| Http3ServerEvent::WebTransport(_)
1020+
| Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. }
10171021
| Http3ServerEvent::ConnectUdp(_) => {}
10181022
}
10191023
}
@@ -1064,6 +1068,7 @@ mod tests {
10641068
| Http3ServerEvent::StateChange { .. }
10651069
| Http3ServerEvent::PriorityUpdate { .. }
10661070
| Http3ServerEvent::WebTransport(_)
1071+
| Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. }
10671072
| Http3ServerEvent::ConnectUdp(_) => {}
10681073
}
10691074
}
@@ -1092,6 +1097,7 @@ mod tests {
10921097
| Http3ServerEvent::StateChange { .. }
10931098
| Http3ServerEvent::PriorityUpdate { .. }
10941099
| Http3ServerEvent::WebTransport(_)
1100+
| Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. }
10951101
| Http3ServerEvent::ConnectUdp(_) => {}
10961102
}
10971103
}
@@ -1137,6 +1143,7 @@ mod tests {
11371143
| Http3ServerEvent::StateChange { .. }
11381144
| Http3ServerEvent::PriorityUpdate { .. }
11391145
| Http3ServerEvent::WebTransport(_)
1146+
| Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. }
11401147
| Http3ServerEvent::ConnectUdp(_) => {}
11411148
}
11421149
}
@@ -1355,6 +1362,7 @@ mod tests {
13551362
| Http3ServerEvent::StateChange { .. }
13561363
| Http3ServerEvent::PriorityUpdate { .. }
13571364
| Http3ServerEvent::WebTransport(_)
1365+
| Http3ServerEvent::OutgoingDatagramSpaceAvailable { .. }
13581366
| Http3ServerEvent::ConnectUdp(_) => {}
13591367
}
13601368
}

0 commit comments

Comments
 (0)