rwebtransport 0.2.2 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +523 -140
- package/Cargo.toml +5 -5
- package/README.md +8 -4
- package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
- package/crates/native/Cargo.toml +2 -0
- package/crates/native/src/config.rs +69 -8
- package/crates/native/src/driver.rs +253 -90
- package/crates/native/src/lib.rs +80 -57
- package/crates/native/src/parser_memory.rs +203 -0
- package/crates/native/src/server.rs +215 -183
- package/crates/native/src/session.rs +1341 -243
- package/crates/native/src/test_support.rs +119 -0
- package/crates/wtcore/Cargo.toml +1 -0
- package/crates/wtcore/src/h3.rs +250 -46
- package/dist/index.js +218 -75
- package/dist/index.js.map +7 -1
- package/dist/index.mjs +214 -69
- package/dist/index.mjs.map +7 -1
- package/dist/native.d.ts +10 -4
- package/dist/server.d.ts +1 -0
- package/package.json +84 -78
- package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-arm64/rwebtransport.node +0 -0
- package/prebuilds/linux-x64/rwebtransport.node +0 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/build-linux-prebuilt.sh +46 -0
- package/scripts/build-ts.js +41 -0
- package/scripts/build.js +10 -3
- package/vendor/quiche/AGENTS.md +10 -0
- package/vendor/quiche/Cargo.lock +239 -248
- package/vendor/quiche/Cargo.toml +23 -10
- package/vendor/quiche/examples/Makefile +4 -4
- package/vendor/quiche/examples/client.rs +10 -28
- package/vendor/quiche/examples/http3-client.rs +20 -34
- package/vendor/quiche/examples/http3-server.rs +47 -78
- package/vendor/quiche/examples/server.rs +19 -49
- package/vendor/quiche/include/quiche.h +35 -4
- package/vendor/quiche/src/build.rs +17 -0
- package/vendor/quiche/src/cid.rs +129 -61
- package/vendor/quiche/src/crypto/boringssl.rs +98 -86
- package/vendor/quiche/src/crypto/mod.rs +64 -77
- package/vendor/quiche/src/dgram.rs +2 -3
- package/vendor/quiche/src/error.rs +22 -20
- package/vendor/quiche/src/ffi.rs +558 -280
- package/vendor/quiche/src/flowcontrol.rs +0 -7
- package/vendor/quiche/src/frame.rs +134 -135
- package/vendor/quiche/src/h3/ffi.rs +73 -60
- package/vendor/quiche/src/h3/frame.rs +70 -90
- package/vendor/quiche/src/h3/mod.rs +1024 -593
- package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
- package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
- package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
- package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
- package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
- package/vendor/quiche/src/h3/stream.rs +1009 -194
- package/vendor/quiche/src/lib.rs +1361 -1195
- package/vendor/quiche/src/minmax.rs +2 -6
- package/vendor/quiche/src/packet.rs +420 -552
- package/vendor/quiche/src/path.rs +202 -72
- package/vendor/quiche/src/pmtud.rs +18 -16
- package/vendor/quiche/src/ranges.rs +184 -82
- package/vendor/quiche/src/recovery/AGENTS.md +5 -2
- package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
- package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
- package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
- package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
- package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
- package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
- package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
- package/vendor/quiche/src/recovery/mod.rs +414 -173
- package/vendor/quiche/src/recovery/rtt.rs +8 -5
- package/vendor/quiche/src/stream/mod.rs +864 -133
- package/vendor/quiche/src/stream/recv_buf.rs +476 -172
- package/vendor/quiche/src/stream/send_buf.rs +343 -81
- package/vendor/quiche/src/test_utils.rs +78 -64
- package/vendor/quiche/src/tests.rs +2559 -1335
- package/vendor/quiche/src/tls/boringssl.rs +60 -63
- package/vendor/quiche/src/tls/mod.rs +146 -193
- package/vendor/quiche/src/transport_params.rs +87 -103
|
@@ -40,14 +40,14 @@ use intrusive_collections::RBTreeAtomicLink;
|
|
|
40
40
|
use smallvec::SmallVec;
|
|
41
41
|
|
|
42
42
|
use crate::buffers::DefaultBufFactory;
|
|
43
|
+
use crate::ranges::RangeSet;
|
|
43
44
|
use crate::BufFactory;
|
|
44
45
|
use crate::Error;
|
|
45
46
|
use crate::Result;
|
|
46
47
|
|
|
47
|
-
|
|
48
|
+
pub(crate) use self::send_buf::PeerStopSendingState;
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
pub(crate) const DEFAULT_STREAM_WINDOW: u64 = 32 * 1024;
|
|
50
|
+
const DEFAULT_URGENCY: u8 = 127;
|
|
51
51
|
|
|
52
52
|
/// The maximum size of the receiver stream flow control window.
|
|
53
53
|
pub const MAX_STREAM_WINDOW: u64 = 16 * 1024 * 1024;
|
|
@@ -114,18 +114,54 @@ type BuildStreamIdHasher = std::hash::BuildHasherDefault<StreamIdHasher>;
|
|
|
114
114
|
pub type StreamIdHashMap<V> = HashMap<u64, V, BuildStreamIdHasher>;
|
|
115
115
|
pub type StreamIdHashSet = HashSet<u64, BuildStreamIdHasher>;
|
|
116
116
|
|
|
117
|
+
/// Tracks collected stream sequences separately for each stream type.
|
|
118
|
+
#[derive(Default)]
|
|
119
|
+
struct CollectedStreams {
|
|
120
|
+
exhausted: bool,
|
|
121
|
+
// Defer allocation until the first stream is collected. The range capacity
|
|
122
|
+
// is unlimited because evicting a tombstone would allow a collected stream
|
|
123
|
+
// to be recreated.
|
|
124
|
+
ranges: Option<Box<[RangeSet; 4]>>,
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
impl CollectedStreams {
|
|
128
|
+
fn insert(&mut self, stream_id: u64, bounded: bool) {
|
|
129
|
+
if self.exhausted {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
// Same-type stream IDs advance by four, so store their sequences to
|
|
133
|
+
// allow adjacent collected streams to merge into a single range.
|
|
134
|
+
let ranges = self.ranges.get_or_insert_with(Default::default);
|
|
135
|
+
ranges[(stream_id & 0x3) as usize].push_item(stream_id >> 2);
|
|
136
|
+
// Keep accurate history for contiguous streams indefinitely. If the
|
|
137
|
+
// history becomes too fragmented, close instead of evicting IDs that
|
|
138
|
+
// must not be accepted again.
|
|
139
|
+
self.exhausted = bounded && ranges.iter().map(RangeSet::len).sum::<usize>() > 4096;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
fn contains(&self, stream_id: u64) -> bool {
|
|
143
|
+
let Some(ranges) = &self.ranges else {
|
|
144
|
+
return false;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
ranges[(stream_id & 0x3) as usize].contains(stream_id >> 2)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
117
151
|
/// Keeps track of QUIC streams and enforces stream limits.
|
|
118
152
|
#[derive(Default)]
|
|
119
153
|
pub struct StreamMap<F: BufFactory = DefaultBufFactory> {
|
|
120
154
|
/// Map of streams indexed by stream ID.
|
|
121
155
|
streams: StreamIdHashMap<Stream<F>>,
|
|
122
156
|
|
|
157
|
+
pub(crate) receive_memory: Option<Arc<ReceiveMemory>>,
|
|
158
|
+
|
|
123
159
|
/// Set of streams that were completed and garbage collected.
|
|
124
160
|
///
|
|
125
161
|
/// Instead of keeping the full stream state forever, we collect completed
|
|
126
162
|
/// streams to save memory, but we still need to keep track of previously
|
|
127
163
|
/// created streams, to prevent peers from re-creating them.
|
|
128
|
-
collected:
|
|
164
|
+
collected: CollectedStreams,
|
|
129
165
|
|
|
130
166
|
/// Peer's maximum bidirectional stream count limit.
|
|
131
167
|
peer_max_streams_bidi: u64,
|
|
@@ -169,12 +205,13 @@ pub struct StreamMap<F: BufFactory = DefaultBufFactory> {
|
|
|
169
205
|
/// having to iterate over the full list of streams.
|
|
170
206
|
pub readable: RBTree<StreamReadablePriorityAdapter>,
|
|
171
207
|
|
|
172
|
-
/// Set of
|
|
173
|
-
///
|
|
174
|
-
/// a `StreamIter` of streams without having to iterate over the full list
|
|
175
|
-
/// of streams.
|
|
208
|
+
/// Set of streams queued for writable notification. A stopped stream can
|
|
209
|
+
/// be removed before its error is reported to the application.
|
|
176
210
|
pub writable: RBTree<StreamWritablePriorityAdapter>,
|
|
177
211
|
|
|
212
|
+
/// Stopped streams still queued for writable notification.
|
|
213
|
+
stopped_writable: RBTree<StreamStoppedWritablePriorityAdapter>,
|
|
214
|
+
|
|
178
215
|
/// Set of stream IDs corresponding to streams that are almost out of flow
|
|
179
216
|
/// control credit and need to send MAX_STREAM_DATA. This is used to
|
|
180
217
|
/// generate a `StreamIter` of streams without having to iterate over the
|
|
@@ -189,7 +226,7 @@ pub struct StreamMap<F: BufFactory = DefaultBufFactory> {
|
|
|
189
226
|
/// Set of stream IDs corresponding to streams that are reset. The value
|
|
190
227
|
/// of the map elements is a tuple of the error code and final size values
|
|
191
228
|
/// to include in the RESET_STREAM frame.
|
|
192
|
-
reset: StreamIdHashMap<(u64, u64)>,
|
|
229
|
+
reset: StreamIdHashMap<(u64, u64, Option<u64>)>,
|
|
193
230
|
|
|
194
231
|
/// Set of stream IDs corresponding to streams that are shutdown on the
|
|
195
232
|
/// receive side, and need to send a STOP_SENDING frame. The value of the
|
|
@@ -199,15 +236,12 @@ pub struct StreamMap<F: BufFactory = DefaultBufFactory> {
|
|
|
199
236
|
/// The maximum size of a stream window.
|
|
200
237
|
max_stream_window: u64,
|
|
201
238
|
|
|
202
|
-
///
|
|
203
|
-
|
|
204
|
-
use_initial_max_data_as_flow_control_win: bool,
|
|
239
|
+
/// Total number of bytes in send buffers across all streams.
|
|
240
|
+
tx_buffered: usize,
|
|
205
241
|
}
|
|
206
242
|
|
|
207
243
|
impl<F: BufFactory> StreamMap<F> {
|
|
208
|
-
pub fn new(
|
|
209
|
-
max_streams_bidi: u64, max_streams_uni: u64, max_stream_window: u64,
|
|
210
|
-
) -> Self {
|
|
244
|
+
pub fn new(max_streams_bidi: u64, max_streams_uni: u64, max_stream_window: u64) -> Self {
|
|
211
245
|
StreamMap {
|
|
212
246
|
local_max_streams_bidi: max_streams_bidi,
|
|
213
247
|
local_max_streams_bidi_next: max_streams_bidi,
|
|
@@ -236,23 +270,28 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
236
270
|
/// Returns the mutable stream with the given ID if it exists, or creates
|
|
237
271
|
/// a new one otherwise.
|
|
238
272
|
///
|
|
239
|
-
/// The `local` parameter indicates whether the stream
|
|
240
|
-
///
|
|
241
|
-
///
|
|
242
|
-
/// flow control values from the local and remote transport parameters
|
|
243
|
-
/// (also passed as arguments).
|
|
273
|
+
/// The `local` parameter indicates whether the stream is locally
|
|
274
|
+
/// initiated. It validates the stream ID and selects the initial flow
|
|
275
|
+
/// control values from the local and remote transport parameters.
|
|
244
276
|
///
|
|
245
277
|
/// This also takes care of enforcing both local and the peer's stream
|
|
246
278
|
/// count limits. If one of these limits is violated, the `StreamLimit`
|
|
247
279
|
/// error is returned.
|
|
248
280
|
pub(crate) fn get_or_create(
|
|
249
|
-
&mut self,
|
|
250
|
-
|
|
281
|
+
&mut self,
|
|
282
|
+
id: u64,
|
|
283
|
+
local_params: &crate::TransportParams,
|
|
284
|
+
peer_params: &crate::TransportParams,
|
|
285
|
+
local: bool,
|
|
286
|
+
is_server: bool,
|
|
251
287
|
) -> Result<&mut Stream<F>> {
|
|
252
288
|
let (stream, is_new_and_writable) = match self.streams.entry(id) {
|
|
253
289
|
hash_map::Entry::Vacant(v) => {
|
|
290
|
+
if self.collected.exhausted {
|
|
291
|
+
return Err(Error::StreamMemoryLimit);
|
|
292
|
+
}
|
|
254
293
|
// Stream has already been closed and garbage collected.
|
|
255
|
-
if self.collected.contains(
|
|
294
|
+
if self.collected.contains(id) {
|
|
256
295
|
return Err(Error::Done);
|
|
257
296
|
}
|
|
258
297
|
|
|
@@ -277,8 +316,7 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
277
316
|
),
|
|
278
317
|
|
|
279
318
|
// Remotely-initiated unidirectional stream.
|
|
280
|
-
(false, false) =>
|
|
281
|
-
(local_params.initial_max_stream_data_uni, 0),
|
|
319
|
+
(false, false) => (local_params.initial_max_stream_data_uni, 0),
|
|
282
320
|
};
|
|
283
321
|
|
|
284
322
|
// The two least significant bits from a stream id identify the
|
|
@@ -289,65 +327,48 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
289
327
|
// Enforce stream count limits.
|
|
290
328
|
match (is_local(id, is_server), is_bidi(id)) {
|
|
291
329
|
(true, true) => {
|
|
292
|
-
let n = cmp::max(
|
|
293
|
-
self.local_opened_streams_bidi,
|
|
294
|
-
stream_sequence + 1,
|
|
295
|
-
);
|
|
330
|
+
let n = cmp::max(self.local_opened_streams_bidi, stream_sequence + 1);
|
|
296
331
|
|
|
297
332
|
if n > self.peer_max_streams_bidi {
|
|
298
333
|
return Err(Error::StreamLimit);
|
|
299
334
|
}
|
|
300
335
|
|
|
301
336
|
self.local_opened_streams_bidi = n;
|
|
302
|
-
}
|
|
337
|
+
}
|
|
303
338
|
|
|
304
339
|
(true, false) => {
|
|
305
|
-
let n = cmp::max(
|
|
306
|
-
self.local_opened_streams_uni,
|
|
307
|
-
stream_sequence + 1,
|
|
308
|
-
);
|
|
340
|
+
let n = cmp::max(self.local_opened_streams_uni, stream_sequence + 1);
|
|
309
341
|
|
|
310
342
|
if n > self.peer_max_streams_uni {
|
|
311
343
|
return Err(Error::StreamLimit);
|
|
312
344
|
}
|
|
313
345
|
|
|
314
346
|
self.local_opened_streams_uni = n;
|
|
315
|
-
}
|
|
347
|
+
}
|
|
316
348
|
|
|
317
349
|
(false, true) => {
|
|
318
|
-
let n = cmp::max(
|
|
319
|
-
self.peer_opened_streams_bidi,
|
|
320
|
-
stream_sequence + 1,
|
|
321
|
-
);
|
|
350
|
+
let n = cmp::max(self.peer_opened_streams_bidi, stream_sequence + 1);
|
|
322
351
|
|
|
323
352
|
if n > self.local_max_streams_bidi {
|
|
324
353
|
return Err(Error::StreamLimit);
|
|
325
354
|
}
|
|
326
355
|
|
|
327
356
|
self.peer_opened_streams_bidi = n;
|
|
328
|
-
}
|
|
357
|
+
}
|
|
329
358
|
|
|
330
359
|
(false, false) => {
|
|
331
|
-
let n = cmp::max(
|
|
332
|
-
self.peer_opened_streams_uni,
|
|
333
|
-
stream_sequence + 1,
|
|
334
|
-
);
|
|
360
|
+
let n = cmp::max(self.peer_opened_streams_uni, stream_sequence + 1);
|
|
335
361
|
|
|
336
362
|
if n > self.local_max_streams_uni {
|
|
337
363
|
return Err(Error::StreamLimit);
|
|
338
364
|
}
|
|
339
365
|
|
|
340
366
|
self.peer_opened_streams_uni = n;
|
|
341
|
-
}
|
|
367
|
+
}
|
|
342
368
|
};
|
|
343
369
|
|
|
344
|
-
let initial_window =
|
|
345
|
-
|
|
346
|
-
max_rx_data
|
|
347
|
-
} else {
|
|
348
|
-
cmp::min(max_rx_data, DEFAULT_STREAM_WINDOW)
|
|
349
|
-
};
|
|
350
|
-
let s = Stream::new(
|
|
370
|
+
let initial_window = max_rx_data;
|
|
371
|
+
let mut s = Stream::new(
|
|
351
372
|
id,
|
|
352
373
|
max_rx_data,
|
|
353
374
|
max_tx_data,
|
|
@@ -355,11 +376,12 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
355
376
|
initial_window,
|
|
356
377
|
self.max_stream_window,
|
|
357
378
|
);
|
|
379
|
+
s.recv.set_memory_budget(self.receive_memory.clone());
|
|
358
380
|
|
|
359
381
|
let is_writable = s.is_writable();
|
|
360
382
|
|
|
361
383
|
(v.insert(s), is_writable)
|
|
362
|
-
}
|
|
384
|
+
}
|
|
363
385
|
|
|
364
386
|
hash_map::Entry::Occupied(v) => (v.into_mut(), false),
|
|
365
387
|
};
|
|
@@ -408,11 +430,30 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
408
430
|
}
|
|
409
431
|
}
|
|
410
432
|
|
|
433
|
+
/// Marks a newly stopped stream as queued for writable notification.
|
|
434
|
+
pub fn insert_stopped_writable(&mut self, priority_key: &Arc<StreamPriorityKey>) {
|
|
435
|
+
if !priority_key.stopped_writable.is_linked() {
|
|
436
|
+
self.stopped_writable.insert(Arc::clone(priority_key));
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
self.insert_writable(priority_key);
|
|
440
|
+
}
|
|
441
|
+
|
|
411
442
|
/// Removes the stream ID from the writable streams set.
|
|
412
443
|
///
|
|
413
444
|
/// This should also be called anytime an existing stream stops being
|
|
414
445
|
/// writable.
|
|
415
446
|
pub fn remove_writable(&mut self, priority_key: &Arc<StreamPriorityKey>) {
|
|
447
|
+
if priority_key.stopped_writable.is_linked() {
|
|
448
|
+
let ptr = Arc::as_ptr(priority_key);
|
|
449
|
+
// SAFETY: `priority_key` originates from this `StreamMap`, and its
|
|
450
|
+
// `stopped_writable` link is only inserted into this map's
|
|
451
|
+
// corresponding tree. The link is linked, and the `Arc` keeps the
|
|
452
|
+
// allocation alive, so `ptr` identifies an element of this tree.
|
|
453
|
+
let mut c = unsafe { self.stopped_writable.cursor_mut_from_ptr(ptr) };
|
|
454
|
+
c.remove();
|
|
455
|
+
}
|
|
456
|
+
|
|
416
457
|
if !priority_key.writable.is_linked() {
|
|
417
458
|
return;
|
|
418
459
|
}
|
|
@@ -453,17 +494,20 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
453
494
|
}
|
|
454
495
|
|
|
455
496
|
/// Updates the priorities of a stream.
|
|
456
|
-
pub fn update_priority(
|
|
457
|
-
&mut self, old: &Arc<StreamPriorityKey>, new: &Arc<StreamPriorityKey>,
|
|
458
|
-
) {
|
|
497
|
+
pub fn update_priority(&mut self, old: &Arc<StreamPriorityKey>, new: &Arc<StreamPriorityKey>) {
|
|
459
498
|
if old.readable.is_linked() {
|
|
460
499
|
self.remove_readable(old);
|
|
461
500
|
self.readable.insert(Arc::clone(new));
|
|
462
501
|
}
|
|
463
502
|
|
|
464
503
|
if old.writable.is_linked() {
|
|
504
|
+
let stopped = old.stopped_writable.is_linked();
|
|
465
505
|
self.remove_writable(old);
|
|
466
|
-
|
|
506
|
+
if stopped {
|
|
507
|
+
self.insert_stopped_writable(new);
|
|
508
|
+
} else {
|
|
509
|
+
self.insert_writable(new);
|
|
510
|
+
}
|
|
467
511
|
}
|
|
468
512
|
|
|
469
513
|
if old.flushable.is_linked() {
|
|
@@ -497,14 +541,17 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
497
541
|
self.blocked.remove(&stream_id);
|
|
498
542
|
}
|
|
499
543
|
|
|
500
|
-
///
|
|
501
|
-
///
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
544
|
+
/// Queues RESET_STREAM unless the send side is already complete or the
|
|
545
|
+
/// stream has been collected.
|
|
546
|
+
pub fn insert_reset(&mut self, stream_id: u64, error_code: u64, final_size: u64) {
|
|
547
|
+
if let Some(stream) = self.streams.get(&stream_id) {
|
|
548
|
+
if !stream.send.is_complete() && !stream.send.reset_acknowledged() {
|
|
549
|
+
self.reset.insert(
|
|
550
|
+
stream_id,
|
|
551
|
+
(error_code, final_size, stream.send.reliable_reset_size),
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
508
555
|
}
|
|
509
556
|
|
|
510
557
|
/// Removes the stream ID from the reset streams set.
|
|
@@ -589,10 +636,25 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
589
636
|
self.peer_max_streams_uni - self.local_opened_streams_uni
|
|
590
637
|
}
|
|
591
638
|
|
|
639
|
+
/// Collects or disarms a stream after reporting STOP_SENDING to the caller.
|
|
640
|
+
pub fn on_stop_sending_reported(&mut self, stream_id: u64) {
|
|
641
|
+
let Some(stream) = self.streams.get_mut(&stream_id) else {
|
|
642
|
+
return;
|
|
643
|
+
};
|
|
644
|
+
|
|
645
|
+
if stream.state() == StreamState::Collectable {
|
|
646
|
+
let local = stream.local;
|
|
647
|
+
self.collect(stream_id, local);
|
|
648
|
+
} else {
|
|
649
|
+
let priority_key = Arc::clone(&stream.priority_key);
|
|
650
|
+
self.remove_writable(&priority_key);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
592
654
|
/// Drops completed stream.
|
|
593
655
|
///
|
|
594
|
-
/// This should only be called when Stream::
|
|
595
|
-
/// the given stream.
|
|
656
|
+
/// This should only be called when Stream::state() returns Collectable
|
|
657
|
+
/// for the given stream.
|
|
596
658
|
pub fn collect(&mut self, stream_id: u64, local: bool) {
|
|
597
659
|
if !local {
|
|
598
660
|
// If the stream was created by the peer, give back a max streams
|
|
@@ -601,8 +663,7 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
601
663
|
self.local_max_streams_bidi_next =
|
|
602
664
|
self.local_max_streams_bidi_next.saturating_add(1);
|
|
603
665
|
} else {
|
|
604
|
-
self.local_max_streams_uni_next =
|
|
605
|
-
self.local_max_streams_uni_next.saturating_add(1);
|
|
666
|
+
self.local_max_streams_uni_next = self.local_max_streams_uni_next.saturating_add(1);
|
|
606
667
|
}
|
|
607
668
|
}
|
|
608
669
|
|
|
@@ -614,7 +675,25 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
614
675
|
|
|
615
676
|
self.remove_flushable(&s.priority_key);
|
|
616
677
|
|
|
617
|
-
self.collected
|
|
678
|
+
self.collected
|
|
679
|
+
.insert(stream_id, self.receive_memory.is_some());
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
pub(crate) fn collected_history_exhausted(&self) -> bool {
|
|
683
|
+
self.collected.exhausted
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/// Collects a completed stream when no application notification remains.
|
|
687
|
+
pub(crate) fn try_collect(&mut self, stream_id: u64) {
|
|
688
|
+
let Some(stream) = self.get(stream_id) else {
|
|
689
|
+
return;
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
let local = stream.local;
|
|
693
|
+
|
|
694
|
+
if stream.state() == StreamState::Collectable {
|
|
695
|
+
self.collect(stream_id, local);
|
|
696
|
+
}
|
|
618
697
|
}
|
|
619
698
|
|
|
620
699
|
/// Creates an iterator over streams that have outstanding data to read.
|
|
@@ -633,6 +712,39 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
633
712
|
}
|
|
634
713
|
}
|
|
635
714
|
|
|
715
|
+
/// Creates an iterator over stopped streams in the writable queue.
|
|
716
|
+
pub fn stopped_writable(&self) -> StreamIter {
|
|
717
|
+
if self.stopped_writable.is_empty() {
|
|
718
|
+
return StreamIter::default();
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
StreamIter {
|
|
722
|
+
streams: self.stopped_writable.iter().map(|key| key.id).collect(),
|
|
723
|
+
index: 0,
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/// Returns and unlinks the next stopped stream awaiting notification.
|
|
728
|
+
pub fn pop_stopped_writable(&mut self) -> Option<u64> {
|
|
729
|
+
let priority_key = self.stopped_writable.front().clone_pointer()?;
|
|
730
|
+
self.remove_writable(&priority_key);
|
|
731
|
+
Some(priority_key.id)
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/// Returns true if the given local stream has been opened.
|
|
735
|
+
///
|
|
736
|
+
/// Opening a local stream implicitly opens all lower-numbered local streams
|
|
737
|
+
/// of the same type, even if they do not have `Stream` objects yet.
|
|
738
|
+
pub fn local_stream_opened(&self, stream_id: u64) -> bool {
|
|
739
|
+
let opened = if is_bidi(stream_id) {
|
|
740
|
+
self.local_opened_streams_bidi
|
|
741
|
+
} else {
|
|
742
|
+
self.local_opened_streams_uni
|
|
743
|
+
};
|
|
744
|
+
|
|
745
|
+
stream_id >> 2 < opened
|
|
746
|
+
}
|
|
747
|
+
|
|
636
748
|
/// Creates an iterator over streams that need to send MAX_STREAM_DATA.
|
|
637
749
|
pub fn almost_full(&self) -> StreamIter {
|
|
638
750
|
StreamIter::from(&self.almost_full)
|
|
@@ -644,7 +756,7 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
644
756
|
}
|
|
645
757
|
|
|
646
758
|
/// Creates an iterator over streams that need to send RESET_STREAM.
|
|
647
|
-
pub fn reset(&self) -> hash_map::Iter<'_, u64, (u64, u64)> {
|
|
759
|
+
pub fn reset(&self) -> hash_map::Iter<'_, u64, (u64, u64, Option<u64>)> {
|
|
648
760
|
self.reset.iter()
|
|
649
761
|
}
|
|
650
762
|
|
|
@@ -655,7 +767,7 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
655
767
|
|
|
656
768
|
/// Returns true if the stream has been collected.
|
|
657
769
|
pub fn is_collected(&self, stream_id: u64) -> bool {
|
|
658
|
-
self.collected.contains(
|
|
770
|
+
self.collected.contains(stream_id)
|
|
659
771
|
}
|
|
660
772
|
|
|
661
773
|
/// Returns true if there are any streams that have data to write.
|
|
@@ -698,8 +810,8 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
698
810
|
let available = self
|
|
699
811
|
.local_max_streams_bidi
|
|
700
812
|
.saturating_sub(self.peer_opened_streams_bidi);
|
|
701
|
-
self.local_max_streams_bidi_next != self.local_max_streams_bidi
|
|
702
|
-
available <= self.initial_max_streams_bidi / 2
|
|
813
|
+
self.local_max_streams_bidi_next != self.local_max_streams_bidi
|
|
814
|
+
&& available <= self.initial_max_streams_bidi / 2
|
|
703
815
|
}
|
|
704
816
|
|
|
705
817
|
/// Returns true if the max unidirectional streams count needs to be updated
|
|
@@ -711,8 +823,8 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
711
823
|
let available = self
|
|
712
824
|
.local_max_streams_uni
|
|
713
825
|
.saturating_sub(self.peer_opened_streams_uni);
|
|
714
|
-
self.local_max_streams_uni_next != self.local_max_streams_uni
|
|
715
|
-
available <= self.initial_max_streams_uni / 2
|
|
826
|
+
self.local_max_streams_uni_next != self.local_max_streams_uni
|
|
827
|
+
&& available <= self.initial_max_streams_uni / 2
|
|
716
828
|
}
|
|
717
829
|
|
|
718
830
|
/// Returns the number of active streams in the map.
|
|
@@ -721,13 +833,78 @@ impl<F: BufFactory> StreamMap<F> {
|
|
|
721
833
|
self.streams.len()
|
|
722
834
|
}
|
|
723
835
|
|
|
724
|
-
///
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
836
|
+
/// Returns the total number of bytes buffered across all streams.
|
|
837
|
+
pub(crate) fn tx_buffered(&self) -> usize {
|
|
838
|
+
self.tx_buffered
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/// Computes the actual number of bytes in send buffers by summing across
|
|
842
|
+
/// all streams. This is used for debugging to verify that tx_buffered
|
|
843
|
+
/// is accurate.
|
|
844
|
+
fn tx_buffered_actual(&self) -> usize {
|
|
845
|
+
self.streams
|
|
846
|
+
.values()
|
|
847
|
+
.map(|s| s.send.buffered_bytes() as usize)
|
|
848
|
+
.sum()
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/// Checks if the stored tx_buffered matches the actual value.
|
|
852
|
+
/// Returns true if they match, false otherwise.
|
|
853
|
+
pub(crate) fn tx_buffered_is_consistent(&self) -> bool {
|
|
854
|
+
self.tx_buffered == self.tx_buffered_actual()
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/// Updates the tx_buffered value by adding the delta.
|
|
858
|
+
pub(crate) fn add_tx_buffered(&mut self, delta: usize) {
|
|
859
|
+
self.tx_buffered += delta;
|
|
860
|
+
|
|
861
|
+
#[cfg(debug_assertions)]
|
|
862
|
+
self.debug_check_tx_buffered_consistency();
|
|
730
863
|
}
|
|
864
|
+
|
|
865
|
+
/// Updates the tx_buffered value by subtracting the delta.
|
|
866
|
+
pub(crate) fn sub_tx_buffered(&mut self, delta: usize) {
|
|
867
|
+
debug_assert!(self.tx_buffered >= delta);
|
|
868
|
+
self.tx_buffered = self.tx_buffered.saturating_sub(delta);
|
|
869
|
+
|
|
870
|
+
#[cfg(debug_assertions)]
|
|
871
|
+
self.debug_check_tx_buffered_consistency();
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/// Verifies that the stored tx_buffered value matches the actual bytes in
|
|
875
|
+
/// send buffers across all streams. Enabled in debug builds to catch
|
|
876
|
+
/// inconsistencies early.
|
|
877
|
+
#[cfg(debug_assertions)]
|
|
878
|
+
pub(crate) fn debug_check_tx_buffered_consistency(&self) {
|
|
879
|
+
if !self.tx_buffered_is_consistent() {
|
|
880
|
+
let buffered_per_stream = self
|
|
881
|
+
.streams
|
|
882
|
+
.iter()
|
|
883
|
+
.map(|(id, s)| (*id, s.send.buffered_bytes()))
|
|
884
|
+
.collect::<Vec<_>>();
|
|
885
|
+
|
|
886
|
+
let actual = self.tx_buffered_actual();
|
|
887
|
+
let stored = self.tx_buffered;
|
|
888
|
+
panic!(
|
|
889
|
+
"tx_buffered mismatch: stored={}, actual={}, diff={}, buffered_per_stream={:?}",
|
|
890
|
+
stored,
|
|
891
|
+
actual,
|
|
892
|
+
stored as i64 - actual as i64,
|
|
893
|
+
buffered_per_stream
|
|
894
|
+
);
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
/// Completion derived from transport buffers and pending app notifications.
|
|
900
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
901
|
+
pub(crate) enum StreamState {
|
|
902
|
+
/// At least one relevant direction is not complete.
|
|
903
|
+
Incomplete,
|
|
904
|
+
/// Directions are complete, but receive data or a STOP error remains.
|
|
905
|
+
CompleteWithPendingDelivery,
|
|
906
|
+
/// No transport work or application notification remains.
|
|
907
|
+
Collectable,
|
|
731
908
|
}
|
|
732
909
|
|
|
733
910
|
/// A QUIC stream.
|
|
@@ -758,8 +935,12 @@ pub struct Stream<F: BufFactory = DefaultBufFactory> {
|
|
|
758
935
|
impl<F: BufFactory> Stream<F> {
|
|
759
936
|
/// Creates a new stream with the given flow control limits.
|
|
760
937
|
pub fn new(
|
|
761
|
-
id: u64,
|
|
762
|
-
|
|
938
|
+
id: u64,
|
|
939
|
+
max_rx_data: u64,
|
|
940
|
+
max_tx_data: u64,
|
|
941
|
+
local: bool,
|
|
942
|
+
initial_window: u64,
|
|
943
|
+
max_window: u64,
|
|
763
944
|
) -> Self {
|
|
764
945
|
let priority_key = Arc::new(StreamPriorityKey {
|
|
765
946
|
id,
|
|
@@ -786,10 +967,9 @@ impl<F: BufFactory> Stream<F> {
|
|
|
786
967
|
/// Returns true if the stream has enough flow control capacity to be
|
|
787
968
|
/// written to, and is not finished.
|
|
788
969
|
pub fn is_writable(&self) -> bool {
|
|
789
|
-
!self.send.is_shutdown()
|
|
790
|
-
!self.send.is_fin()
|
|
791
|
-
(self.send.off_back() + self.send_lowat as u64) <
|
|
792
|
-
self.send.max_off()
|
|
970
|
+
!self.send.is_shutdown()
|
|
971
|
+
&& !self.send.is_fin()
|
|
972
|
+
&& (self.send.off_back() + self.send_lowat as u64) < self.send.max_off()
|
|
793
973
|
}
|
|
794
974
|
|
|
795
975
|
/// Returns true if the stream has data to send and is allowed to send at
|
|
@@ -797,12 +977,10 @@ impl<F: BufFactory> Stream<F> {
|
|
|
797
977
|
pub fn is_flushable(&self) -> bool {
|
|
798
978
|
let off_front = self.send.off_front();
|
|
799
979
|
|
|
800
|
-
!self.send.is_empty() &&
|
|
801
|
-
off_front < self.send.off_back() &&
|
|
802
|
-
off_front < self.send.max_off()
|
|
980
|
+
!self.send.is_empty() && off_front < self.send.off_back() && off_front < self.send.max_off()
|
|
803
981
|
}
|
|
804
982
|
|
|
805
|
-
///
|
|
983
|
+
/// Derives completion without duplicating buffer or acknowledgement state.
|
|
806
984
|
///
|
|
807
985
|
/// For bidirectional streams this happens when both the receive and send
|
|
808
986
|
/// sides are complete. That is when all incoming data has been read by the
|
|
@@ -811,8 +989,8 @@ impl<F: BufFactory> Stream<F> {
|
|
|
811
989
|
/// For unidirectional streams this happens when either the receive or send
|
|
812
990
|
/// side is complete, depending on whether the stream was created locally
|
|
813
991
|
/// or not.
|
|
814
|
-
pub fn
|
|
815
|
-
match (self.bidi, self.local) {
|
|
992
|
+
pub(crate) fn state(&self) -> StreamState {
|
|
993
|
+
let complete = match (self.bidi, self.local) {
|
|
816
994
|
// For bidirectional streams we need to check both receive and send
|
|
817
995
|
// sides for completion.
|
|
818
996
|
(true, _) => self.recv.is_fin() && self.send.is_complete(),
|
|
@@ -824,6 +1002,19 @@ impl<F: BufFactory> Stream<F> {
|
|
|
824
1002
|
// For unidirectional streams generated by the peer, we only need
|
|
825
1003
|
// to check the receive side for completion.
|
|
826
1004
|
(false, false) => self.recv.is_fin(),
|
|
1005
|
+
};
|
|
1006
|
+
|
|
1007
|
+
let pending_stop_sending = match self.send.peer_stop_sending_state() {
|
|
1008
|
+
PeerStopSendingState::NotReported(_) => !self.send.is_shutdown(),
|
|
1009
|
+
PeerStopSendingState::NotReceived | PeerStopSendingState::Reported(_) => false,
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
if !complete {
|
|
1013
|
+
StreamState::Incomplete
|
|
1014
|
+
} else if self.is_readable() || pending_stop_sending {
|
|
1015
|
+
StreamState::CompleteWithPendingDelivery
|
|
1016
|
+
} else {
|
|
1017
|
+
StreamState::Collectable
|
|
827
1018
|
}
|
|
828
1019
|
}
|
|
829
1020
|
}
|
|
@@ -846,6 +1037,7 @@ pub struct StreamPriorityKey {
|
|
|
846
1037
|
|
|
847
1038
|
pub readable: RBTreeAtomicLink,
|
|
848
1039
|
pub writable: RBTreeAtomicLink,
|
|
1040
|
+
pub stopped_writable: RBTreeAtomicLink,
|
|
849
1041
|
pub flushable: RBTreeAtomicLink,
|
|
850
1042
|
}
|
|
851
1043
|
|
|
@@ -857,6 +1049,7 @@ impl Default for StreamPriorityKey {
|
|
|
857
1049
|
id: Default::default(),
|
|
858
1050
|
readable: Default::default(),
|
|
859
1051
|
writable: Default::default(),
|
|
1052
|
+
stopped_writable: Default::default(),
|
|
860
1053
|
flushable: Default::default(),
|
|
861
1054
|
}
|
|
862
1055
|
}
|
|
@@ -909,7 +1102,7 @@ impl Ord for StreamPriorityKey {
|
|
|
909
1102
|
}
|
|
910
1103
|
}
|
|
911
1104
|
|
|
912
|
-
intrusive_adapter!(pub StreamWritablePriorityAdapter = Arc<StreamPriorityKey>: StreamPriorityKey { writable
|
|
1105
|
+
intrusive_adapter!(pub StreamWritablePriorityAdapter = Arc<StreamPriorityKey>: StreamPriorityKey { writable => RBTreeAtomicLink });
|
|
913
1106
|
|
|
914
1107
|
impl KeyAdapter<'_> for StreamWritablePriorityAdapter {
|
|
915
1108
|
type Key = StreamPriorityKey;
|
|
@@ -919,7 +1112,17 @@ impl KeyAdapter<'_> for StreamWritablePriorityAdapter {
|
|
|
919
1112
|
}
|
|
920
1113
|
}
|
|
921
1114
|
|
|
922
|
-
intrusive_adapter!(pub
|
|
1115
|
+
intrusive_adapter!(pub StreamStoppedWritablePriorityAdapter = Arc<StreamPriorityKey>: StreamPriorityKey { stopped_writable => RBTreeAtomicLink });
|
|
1116
|
+
|
|
1117
|
+
impl KeyAdapter<'_> for StreamStoppedWritablePriorityAdapter {
|
|
1118
|
+
type Key = StreamPriorityKey;
|
|
1119
|
+
|
|
1120
|
+
fn get_key(&self, s: &StreamPriorityKey) -> Self::Key {
|
|
1121
|
+
s.clone()
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
intrusive_adapter!(pub StreamReadablePriorityAdapter = Arc<StreamPriorityKey>: StreamPriorityKey { readable => RBTreeAtomicLink });
|
|
923
1126
|
|
|
924
1127
|
impl KeyAdapter<'_> for StreamReadablePriorityAdapter {
|
|
925
1128
|
type Key = StreamPriorityKey;
|
|
@@ -929,7 +1132,7 @@ impl KeyAdapter<'_> for StreamReadablePriorityAdapter {
|
|
|
929
1132
|
}
|
|
930
1133
|
}
|
|
931
1134
|
|
|
932
|
-
intrusive_adapter!(pub StreamFlushablePriorityAdapter = Arc<StreamPriorityKey>: StreamPriorityKey { flushable
|
|
1135
|
+
intrusive_adapter!(pub StreamFlushablePriorityAdapter = Arc<StreamPriorityKey>: StreamPriorityKey { flushable => RBTreeAtomicLink });
|
|
933
1136
|
|
|
934
1137
|
impl KeyAdapter<'_> for StreamFlushablePriorityAdapter {
|
|
935
1138
|
type Key = StreamPriorityKey;
|
|
@@ -976,10 +1179,273 @@ impl ExactSizeIterator for StreamIter {
|
|
|
976
1179
|
|
|
977
1180
|
#[cfg(test)]
|
|
978
1181
|
mod tests {
|
|
1182
|
+
use rstest::rstest;
|
|
1183
|
+
|
|
979
1184
|
use crate::range_buf::RangeBuf;
|
|
1185
|
+
use crate::test_utils::Pipe;
|
|
980
1186
|
|
|
981
1187
|
use super::*;
|
|
982
1188
|
|
|
1189
|
+
/// The default size of the receiver stream flow control window.
|
|
1190
|
+
const DEFAULT_STREAM_WINDOW: u64 = 32 * 1024;
|
|
1191
|
+
|
|
1192
|
+
#[rstest]
|
|
1193
|
+
fn collected_streams_per_type(#[values(0, 1, 2, 3)] stream_type: u64) {
|
|
1194
|
+
let mut collected = CollectedStreams::default();
|
|
1195
|
+
assert!(collected.ranges.is_none());
|
|
1196
|
+
|
|
1197
|
+
for id in 0..4 {
|
|
1198
|
+
assert!(!collected.contains(id));
|
|
1199
|
+
}
|
|
1200
|
+
assert!(collected.ranges.is_none());
|
|
1201
|
+
|
|
1202
|
+
collected.insert(stream_type, false);
|
|
1203
|
+
assert!(collected.ranges.is_some());
|
|
1204
|
+
|
|
1205
|
+
for id in 0..4 {
|
|
1206
|
+
assert_eq!(collected.contains(id), id == stream_type);
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
collected.insert(8 | stream_type, false);
|
|
1210
|
+
assert!(!collected.contains(4 | stream_type));
|
|
1211
|
+
assert_eq!(
|
|
1212
|
+
collected.ranges.as_ref().unwrap()[stream_type as usize].len(),
|
|
1213
|
+
2
|
|
1214
|
+
);
|
|
1215
|
+
|
|
1216
|
+
collected.insert(4 | stream_type, false);
|
|
1217
|
+
collected.insert(4 | stream_type, false);
|
|
1218
|
+
assert_eq!(
|
|
1219
|
+
collected.ranges.as_ref().unwrap()[stream_type as usize],
|
|
1220
|
+
0..3
|
|
1221
|
+
);
|
|
1222
|
+
|
|
1223
|
+
for id in 0..12 {
|
|
1224
|
+
assert_eq!(collected.contains(id), id & 0x3 == stream_type);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
#[test]
|
|
1229
|
+
fn collected_streams_preserve_fragmented_and_large_ids() {
|
|
1230
|
+
let mut collected = CollectedStreams::default();
|
|
1231
|
+
|
|
1232
|
+
for sequence in (0..2048).step_by(2) {
|
|
1233
|
+
for stream_type in 0..4 {
|
|
1234
|
+
collected.insert((sequence << 2) | stream_type, false);
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
for sequence in 0..2048 {
|
|
1239
|
+
for stream_type in 0..4 {
|
|
1240
|
+
assert_eq!(
|
|
1241
|
+
collected.contains((sequence << 2) | stream_type),
|
|
1242
|
+
sequence % 2 == 0
|
|
1243
|
+
);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
for stream_type in 0..4 {
|
|
1248
|
+
assert_eq!(
|
|
1249
|
+
collected.ranges.as_ref().unwrap()[stream_type as usize].len(),
|
|
1250
|
+
1024
|
|
1251
|
+
);
|
|
1252
|
+
|
|
1253
|
+
let stream_id = ((1u64 << 62) - 4) | stream_type;
|
|
1254
|
+
assert!(!collected.contains(stream_id));
|
|
1255
|
+
collected.insert(stream_id, false);
|
|
1256
|
+
assert!(collected.contains(stream_id));
|
|
1257
|
+
assert!(!collected.contains(stream_id - 4));
|
|
1258
|
+
assert!(!collected.contains((1u64 << 40) | stream_type));
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/// Completes a client-initiated stream and processes returned stream
|
|
1263
|
+
/// credit.
|
|
1264
|
+
fn collect_pipe_stream(pipe: &mut Pipe, stream_id: u64) {
|
|
1265
|
+
let mut buf = [0; 1];
|
|
1266
|
+
|
|
1267
|
+
assert_eq!(pipe.client.stream_send(stream_id, b"a", true), Ok(1));
|
|
1268
|
+
assert_eq!(pipe.advance(), Ok(()));
|
|
1269
|
+
assert_eq!(pipe.server.stream_recv(stream_id, &mut buf), Ok((1, true)));
|
|
1270
|
+
|
|
1271
|
+
if is_bidi(stream_id) {
|
|
1272
|
+
assert_eq!(pipe.server.stream_send(stream_id, b"a", true), Ok(1));
|
|
1273
|
+
assert_eq!(pipe.advance(), Ok(()));
|
|
1274
|
+
assert_eq!(pipe.client.stream_recv(stream_id, &mut buf), Ok((1, true)));
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
assert_eq!(pipe.advance(), Ok(()));
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
#[rstest]
|
|
1281
|
+
fn collected_streams_out_of_order(
|
|
1282
|
+
#[values("cubic", "bbr2_gcongestion")] cc_algorithm_name: &str,
|
|
1283
|
+
#[values(0, 2)] stream_type: u64,
|
|
1284
|
+
) {
|
|
1285
|
+
let mut pipe = Pipe::new(cc_algorithm_name).unwrap();
|
|
1286
|
+
assert_eq!(pipe.handshake(), Ok(()));
|
|
1287
|
+
|
|
1288
|
+
for sequence in [2, 0, 1] {
|
|
1289
|
+
let stream_id = (sequence << 2) | stream_type;
|
|
1290
|
+
collect_pipe_stream(&mut pipe, stream_id);
|
|
1291
|
+
|
|
1292
|
+
for conn in [&mut pipe.client, &mut pipe.server] {
|
|
1293
|
+
assert!(conn.streams.get(stream_id).is_none());
|
|
1294
|
+
assert!(conn.streams.is_collected(stream_id));
|
|
1295
|
+
assert!(conn.stream_closed(stream_id));
|
|
1296
|
+
assert_eq!(
|
|
1297
|
+
conn.streams
|
|
1298
|
+
.get_or_create(
|
|
1299
|
+
stream_id,
|
|
1300
|
+
&conn.local_transport_params,
|
|
1301
|
+
&conn.peer_transport_params,
|
|
1302
|
+
!conn.is_server,
|
|
1303
|
+
conn.is_server,
|
|
1304
|
+
)
|
|
1305
|
+
.err(),
|
|
1306
|
+
Some(Error::Done)
|
|
1307
|
+
);
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
assert_eq!(
|
|
1312
|
+
pipe.server.streams.collected.ranges.as_ref().unwrap()[stream_type as usize],
|
|
1313
|
+
0..3
|
|
1314
|
+
);
|
|
1315
|
+
|
|
1316
|
+
// Late STREAM frames must not materialize a collected stream again.
|
|
1317
|
+
let frames = [crate::frame::Frame::Stream {
|
|
1318
|
+
stream_id: 8 | stream_type,
|
|
1319
|
+
data: RangeBuf::from(b"a", 0, true),
|
|
1320
|
+
}];
|
|
1321
|
+
assert!(pipe
|
|
1322
|
+
.send_pkt_to_server(crate::Type::Short, &frames, &mut [0; 1280])
|
|
1323
|
+
.is_ok());
|
|
1324
|
+
assert_eq!(pipe.server.streams.len(), 0);
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
#[rstest]
|
|
1328
|
+
fn collected_streams_sparse_peer_credit(
|
|
1329
|
+
#[values("cubic", "bbr2_gcongestion")] cc_algorithm_name: &str,
|
|
1330
|
+
#[values(0, 2)] stream_type: u64,
|
|
1331
|
+
#[values(1, 8)] initial_limit: u64,
|
|
1332
|
+
) {
|
|
1333
|
+
let mut config = Pipe::default_config(cc_algorithm_name).unwrap();
|
|
1334
|
+
config.set_initial_max_streams_bidi(initial_limit);
|
|
1335
|
+
config.set_initial_max_streams_uni(initial_limit);
|
|
1336
|
+
|
|
1337
|
+
let mut pipe = Pipe::with_config(&mut config).unwrap();
|
|
1338
|
+
assert_eq!(pipe.handshake(), Ok(()));
|
|
1339
|
+
|
|
1340
|
+
for index in 0..initial_limit {
|
|
1341
|
+
let stream_id = (index << 3) | stream_type;
|
|
1342
|
+
collect_pipe_stream(&mut pipe, stream_id);
|
|
1343
|
+
|
|
1344
|
+
assert_eq!(
|
|
1345
|
+
pipe.server.streams.collected.ranges.as_ref().unwrap()[stream_type as usize].len(),
|
|
1346
|
+
index as usize + 1
|
|
1347
|
+
);
|
|
1348
|
+
assert!(pipe.server.stream_closed(stream_id));
|
|
1349
|
+
assert!(pipe.client.stream_closed(stream_id));
|
|
1350
|
+
assert_eq!(pipe.server.streams.len(), 0);
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
let peer_limit = if is_bidi(stream_type) {
|
|
1354
|
+
pipe.client.streams.peer_max_streams_bidi()
|
|
1355
|
+
} else {
|
|
1356
|
+
pipe.client.streams.peer_max_streams_uni()
|
|
1357
|
+
};
|
|
1358
|
+
assert_eq!(peer_limit, 2 * initial_limit);
|
|
1359
|
+
|
|
1360
|
+
// The odd sequences consume credit but have never had Stream objects.
|
|
1361
|
+
for sequence in (1..2 * initial_limit - 1).step_by(2) {
|
|
1362
|
+
let stream_id = (sequence << 2) | stream_type;
|
|
1363
|
+
|
|
1364
|
+
for conn in [&pipe.client, &pipe.server] {
|
|
1365
|
+
assert!(conn.streams.get(stream_id).is_none());
|
|
1366
|
+
assert!(!conn.streams.is_collected(stream_id));
|
|
1367
|
+
assert!(!conn.stream_closed(stream_id));
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
// Filling the implicit gaps is still permitted and merges all ranges.
|
|
1372
|
+
for sequence in (1..2 * initial_limit - 1).step_by(2) {
|
|
1373
|
+
collect_pipe_stream(&mut pipe, (sequence << 2) | stream_type);
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
assert_eq!(
|
|
1377
|
+
pipe.server.streams.collected.ranges.as_ref().unwrap()[stream_type as usize],
|
|
1378
|
+
0..2 * initial_limit - 1
|
|
1379
|
+
);
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
#[rstest]
|
|
1383
|
+
fn collected_streams_fragmented_local(
|
|
1384
|
+
#[values("cubic", "bbr2_gcongestion")] cc_algorithm_name: &str,
|
|
1385
|
+
#[values(0, 2)] stream_type: u64,
|
|
1386
|
+
) {
|
|
1387
|
+
let mut client_config = Pipe::default_config(cc_algorithm_name).unwrap();
|
|
1388
|
+
client_config.set_initial_max_streams_bidi(1);
|
|
1389
|
+
client_config.set_initial_max_streams_uni(1);
|
|
1390
|
+
|
|
1391
|
+
let mut server_config = Pipe::default_config(cc_algorithm_name).unwrap();
|
|
1392
|
+
server_config.set_initial_max_streams_bidi(32);
|
|
1393
|
+
server_config.set_initial_max_streams_uni(32);
|
|
1394
|
+
|
|
1395
|
+
let mut pipe =
|
|
1396
|
+
Pipe::with_client_and_server_config(&mut client_config, &mut server_config).unwrap();
|
|
1397
|
+
assert_eq!(pipe.handshake(), Ok(()));
|
|
1398
|
+
|
|
1399
|
+
// Local fragmentation follows the peer's allowance and application
|
|
1400
|
+
// behavior, not the client's initial incoming stream limit of one.
|
|
1401
|
+
for index in 0..32 {
|
|
1402
|
+
collect_pipe_stream(&mut pipe, (index << 3) | stream_type);
|
|
1403
|
+
assert_eq!(
|
|
1404
|
+
pipe.client.streams.collected.ranges.as_ref().unwrap()[stream_type as usize].len(),
|
|
1405
|
+
index as usize + 1
|
|
1406
|
+
);
|
|
1407
|
+
assert_eq!(pipe.client.streams.len(), 0);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
#[rstest]
|
|
1412
|
+
fn stream_limit_does_not_collect(
|
|
1413
|
+
#[values(0, 1, 2, 3)] stream_type: u64,
|
|
1414
|
+
#[values(true, false)] local: bool,
|
|
1415
|
+
) {
|
|
1416
|
+
let params = crate::TransportParams::default();
|
|
1417
|
+
let mut streams = <StreamMap>::new(1, 1, DEFAULT_STREAM_WINDOW);
|
|
1418
|
+
streams.update_peer_max_streams_bidi(1);
|
|
1419
|
+
streams.update_peer_max_streams_uni(1);
|
|
1420
|
+
|
|
1421
|
+
let stream_id = 4 | stream_type;
|
|
1422
|
+
let is_server = (stream_type & 1 != 0) == local;
|
|
1423
|
+
assert_eq!(
|
|
1424
|
+
streams
|
|
1425
|
+
.get_or_create(stream_id, ¶ms, ¶ms, local, is_server)
|
|
1426
|
+
.err(),
|
|
1427
|
+
Some(Error::StreamLimit)
|
|
1428
|
+
);
|
|
1429
|
+
assert!(!streams.is_collected(stream_id));
|
|
1430
|
+
assert_eq!(streams.len(), 0);
|
|
1431
|
+
assert!(streams.collected.ranges.is_none());
|
|
1432
|
+
|
|
1433
|
+
if local {
|
|
1434
|
+
streams.update_peer_max_streams_bidi(2);
|
|
1435
|
+
streams.update_peer_max_streams_uni(2);
|
|
1436
|
+
} else {
|
|
1437
|
+
streams.local_max_streams_bidi_next = 2;
|
|
1438
|
+
streams.local_max_streams_uni_next = 2;
|
|
1439
|
+
streams.update_max_streams_bidi();
|
|
1440
|
+
streams.update_max_streams_uni();
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
assert!(streams
|
|
1444
|
+
.get_or_create(stream_id, ¶ms, ¶ms, local, is_server)
|
|
1445
|
+
.is_ok());
|
|
1446
|
+
assert!(streams.collected.ranges.is_none());
|
|
1447
|
+
}
|
|
1448
|
+
|
|
983
1449
|
#[test]
|
|
984
1450
|
fn recv_flow_control() {
|
|
985
1451
|
let mut stream = <Stream>::new(0, 15, 0, true, 15, DEFAULT_STREAM_WINDOW);
|
|
@@ -1375,8 +1841,7 @@ mod tests {
|
|
|
1375
1841
|
|
|
1376
1842
|
#[test]
|
|
1377
1843
|
fn stream_complete() {
|
|
1378
|
-
let mut stream =
|
|
1379
|
-
<Stream>::new(0, 30, 30, true, 30, DEFAULT_STREAM_WINDOW);
|
|
1844
|
+
let mut stream = <Stream>::new(0, 30, 30, true, 30, DEFAULT_STREAM_WINDOW);
|
|
1380
1845
|
|
|
1381
1846
|
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
1382
1847
|
assert_eq!(stream.send.write(b"world", false), Ok(5));
|
|
@@ -1389,6 +1854,8 @@ mod tests {
|
|
|
1389
1854
|
assert!(!stream.send.is_complete());
|
|
1390
1855
|
assert!(stream.send.is_fin());
|
|
1391
1856
|
|
|
1857
|
+
assert_eq!(stream.send.emit(&mut [0; 10]), Ok((10, true)));
|
|
1858
|
+
|
|
1392
1859
|
let buf = RangeBuf::from(b"hello", 0, true);
|
|
1393
1860
|
assert!(stream.recv.write(buf).is_ok());
|
|
1394
1861
|
assert!(!stream.recv.is_fin());
|
|
@@ -1404,15 +1871,44 @@ mod tests {
|
|
|
1404
1871
|
assert!(!stream.send.is_complete());
|
|
1405
1872
|
|
|
1406
1873
|
stream.send.ack(0, 1);
|
|
1874
|
+
assert!(!stream.send.is_complete());
|
|
1875
|
+
|
|
1876
|
+
stream.send.ack_fin();
|
|
1407
1877
|
assert!(stream.send.is_complete());
|
|
1408
1878
|
|
|
1409
|
-
|
|
1879
|
+
assert_eq!(stream.state(), StreamState::Incomplete);
|
|
1410
1880
|
|
|
1411
1881
|
let mut buf = [0; 3];
|
|
1412
1882
|
assert_eq!(stream.recv.emit(&mut buf), Ok((3, true)));
|
|
1413
1883
|
assert!(stream.recv.is_fin());
|
|
1414
1884
|
|
|
1415
|
-
|
|
1885
|
+
assert_eq!(stream.state(), StreamState::Collectable);
|
|
1886
|
+
|
|
1887
|
+
// A completed stream must retain each undelivered terminal event.
|
|
1888
|
+
stream.send.stop(42).unwrap();
|
|
1889
|
+
assert_eq!(stream.state(), StreamState::CompleteWithPendingDelivery);
|
|
1890
|
+
assert_eq!(
|
|
1891
|
+
stream.send.cap_and_report_stop_sending(),
|
|
1892
|
+
Err(Error::StreamStopped(42))
|
|
1893
|
+
);
|
|
1894
|
+
assert_eq!(stream.state(), StreamState::Collectable);
|
|
1895
|
+
|
|
1896
|
+
let mut stream = <Stream>::new(0, 30, 30, true, 30, DEFAULT_STREAM_WINDOW);
|
|
1897
|
+
stream.send.shutdown().unwrap();
|
|
1898
|
+
stream.send.ack_reset_stream();
|
|
1899
|
+
stream.recv.reset(43, 5).unwrap();
|
|
1900
|
+
assert_eq!(stream.state(), StreamState::CompleteWithPendingDelivery);
|
|
1901
|
+
assert_eq!(stream.recv.emit(&mut buf), Err(Error::StreamReset(43)));
|
|
1902
|
+
assert_eq!(stream.state(), StreamState::Collectable);
|
|
1903
|
+
|
|
1904
|
+
// Local shutdown permits collection without reporting a later peer
|
|
1905
|
+
// STOP.
|
|
1906
|
+
stream.send.stop(42).unwrap();
|
|
1907
|
+
assert_eq!(stream.state(), StreamState::Collectable);
|
|
1908
|
+
assert_eq!(
|
|
1909
|
+
stream.send.peer_stop_sending_state(),
|
|
1910
|
+
PeerStopSendingState::NotReported(42)
|
|
1911
|
+
);
|
|
1416
1912
|
}
|
|
1417
1913
|
|
|
1418
1914
|
#[test]
|
|
@@ -1441,8 +1937,7 @@ mod tests {
|
|
|
1441
1937
|
}
|
|
1442
1938
|
|
|
1443
1939
|
fn stream_send_ready(stream: &Stream) -> bool {
|
|
1444
|
-
!stream.send.is_empty() &&
|
|
1445
|
-
stream.send.off_front() < stream.send.off_back()
|
|
1940
|
+
!stream.send.is_empty() && stream.send.off_front() < stream.send.off_back()
|
|
1446
1941
|
}
|
|
1447
1942
|
|
|
1448
1943
|
#[test]
|
|
@@ -1568,14 +2063,8 @@ mod tests {
|
|
|
1568
2063
|
fn send_emit_retransmit() {
|
|
1569
2064
|
let mut buf = [0; 5];
|
|
1570
2065
|
|
|
1571
|
-
let mut stream =
|
|
1572
|
-
0,
|
|
1573
|
-
0,
|
|
1574
|
-
20,
|
|
1575
|
-
true,
|
|
1576
|
-
DEFAULT_STREAM_WINDOW,
|
|
1577
|
-
DEFAULT_STREAM_WINDOW,
|
|
1578
|
-
);
|
|
2066
|
+
let mut stream =
|
|
2067
|
+
<Stream>::new(0, 0, 20, true, DEFAULT_STREAM_WINDOW, DEFAULT_STREAM_WINDOW);
|
|
1579
2068
|
|
|
1580
2069
|
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
1581
2070
|
assert_eq!(stream.send.write(b"world", false), Ok(5));
|
|
@@ -1683,6 +2172,24 @@ mod tests {
|
|
|
1683
2172
|
assert_eq!(&buf[..2], b"ro");
|
|
1684
2173
|
}
|
|
1685
2174
|
|
|
2175
|
+
#[test]
|
|
2176
|
+
fn send_buf_empty_fin_next() {
|
|
2177
|
+
let mut stream = <Stream>::new(0, 0, 20, true, 0, DEFAULT_STREAM_WINDOW);
|
|
2178
|
+
|
|
2179
|
+
assert!(!stream.send.empty_fin_next());
|
|
2180
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2181
|
+
|
|
2182
|
+
let mut buf = [0; 5];
|
|
2183
|
+
assert_eq!(stream.send.emit(&mut buf), Ok((5, false)));
|
|
2184
|
+
assert!(!stream.send.empty_fin_next());
|
|
2185
|
+
|
|
2186
|
+
assert_eq!(stream.send.write(b"", true), Ok(0));
|
|
2187
|
+
assert!(stream.send.empty_fin_next());
|
|
2188
|
+
|
|
2189
|
+
let mut empty = [];
|
|
2190
|
+
assert_eq!(stream.send.emit(&mut empty), Ok((0, true)));
|
|
2191
|
+
}
|
|
2192
|
+
|
|
1686
2193
|
#[test]
|
|
1687
2194
|
fn rangebuf_split_off() {
|
|
1688
2195
|
let mut buf = <RangeBuf>::from(b"helloworld", 5, true);
|
|
@@ -1948,8 +2455,8 @@ mod tests {
|
|
|
1948
2455
|
|
|
1949
2456
|
let mut streams = <StreamMap>::new(100, 100, 100);
|
|
1950
2457
|
|
|
1951
|
-
// Streams where the urgency descends (becomes more important). No
|
|
1952
|
-
// shares an urgency.
|
|
2458
|
+
// Streams where the urgency descends (becomes more important). No
|
|
2459
|
+
// stream shares an urgency.
|
|
1953
2460
|
let input = vec![
|
|
1954
2461
|
(0, 100),
|
|
1955
2462
|
(4, 90),
|
|
@@ -1980,10 +2487,8 @@ mod tests {
|
|
|
1980
2487
|
..Default::default()
|
|
1981
2488
|
});
|
|
1982
2489
|
|
|
1983
|
-
let old_priority_key =
|
|
1984
|
-
&mut stream.priority_key,
|
|
1985
|
-
new_priority_key.clone(),
|
|
1986
|
-
);
|
|
2490
|
+
let old_priority_key =
|
|
2491
|
+
std::mem::replace(&mut stream.priority_key, new_priority_key.clone());
|
|
1987
2492
|
|
|
1988
2493
|
streams.update_priority(&old_priority_key, &new_priority_key);
|
|
1989
2494
|
}
|
|
@@ -2008,10 +2513,8 @@ mod tests {
|
|
|
2008
2513
|
..Default::default()
|
|
2009
2514
|
});
|
|
2010
2515
|
|
|
2011
|
-
let old_priority_key =
|
|
2012
|
-
&mut stream.priority_key,
|
|
2013
|
-
new_priority_key.clone(),
|
|
2014
|
-
);
|
|
2516
|
+
let old_priority_key =
|
|
2517
|
+
std::mem::replace(&mut stream.priority_key, new_priority_key.clone());
|
|
2015
2518
|
|
|
2016
2519
|
streams.update_priority(&old_priority_key, &new_priority_key);
|
|
2017
2520
|
}
|
|
@@ -2082,10 +2585,8 @@ mod tests {
|
|
|
2082
2585
|
..Default::default()
|
|
2083
2586
|
});
|
|
2084
2587
|
|
|
2085
|
-
let old_priority_key =
|
|
2086
|
-
&mut stream.priority_key,
|
|
2087
|
-
new_priority_key.clone(),
|
|
2088
|
-
);
|
|
2588
|
+
let old_priority_key =
|
|
2589
|
+
std::mem::replace(&mut stream.priority_key, new_priority_key.clone());
|
|
2089
2590
|
|
|
2090
2591
|
streams.update_priority(&old_priority_key, &new_priority_key);
|
|
2091
2592
|
}
|
|
@@ -2169,8 +2670,7 @@ mod tests {
|
|
|
2169
2670
|
|
|
2170
2671
|
#[test]
|
|
2171
2672
|
fn priority_tree_dupes() {
|
|
2172
|
-
let mut prioritized_writable: RBTree<StreamWritablePriorityAdapter> =
|
|
2173
|
-
Default::default();
|
|
2673
|
+
let mut prioritized_writable: RBTree<StreamWritablePriorityAdapter> = Default::default();
|
|
2174
2674
|
|
|
2175
2675
|
for id in [0, 4, 8, 12] {
|
|
2176
2676
|
let s = Arc::new(StreamPriorityKey {
|
|
@@ -2183,12 +2683,11 @@ mod tests {
|
|
|
2183
2683
|
prioritized_writable.insert(s);
|
|
2184
2684
|
}
|
|
2185
2685
|
|
|
2186
|
-
let walk_1: Vec<u64> =
|
|
2187
|
-
prioritized_writable.iter().map(|s| s.id).collect();
|
|
2686
|
+
let walk_1: Vec<u64> = prioritized_writable.iter().map(|s| s.id).collect();
|
|
2188
2687
|
assert_eq!(walk_1, vec![0, 4, 8, 12]);
|
|
2189
2688
|
|
|
2190
|
-
// Default keys could cause duplicate entries
|
|
2191
|
-
//
|
|
2689
|
+
// Default keys could cause duplicate entries. `StreamMap` normally
|
|
2690
|
+
// prevents this.
|
|
2192
2691
|
for id in [0, 4, 8, 12] {
|
|
2193
2692
|
let s = Arc::new(StreamPriorityKey {
|
|
2194
2693
|
urgency: 0,
|
|
@@ -2200,11 +2699,243 @@ mod tests {
|
|
|
2200
2699
|
prioritized_writable.insert(s);
|
|
2201
2700
|
}
|
|
2202
2701
|
|
|
2203
|
-
let walk_2: Vec<u64> =
|
|
2204
|
-
prioritized_writable.iter().map(|s| s.id).collect();
|
|
2702
|
+
let walk_2: Vec<u64> = prioritized_writable.iter().map(|s| s.id).collect();
|
|
2205
2703
|
assert_eq!(walk_2, vec![0, 0, 4, 4, 8, 8, 12, 12]);
|
|
2206
2704
|
}
|
|
2705
|
+
|
|
2706
|
+
#[test]
|
|
2707
|
+
fn retransmit_returns_zero_when_already_acked() {
|
|
2708
|
+
let mut stream = <Stream>::new(0, 15, 15, true, 0, 15);
|
|
2709
|
+
|
|
2710
|
+
// Write and emit some data.
|
|
2711
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2712
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2713
|
+
|
|
2714
|
+
let mut buf = [0; 10];
|
|
2715
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2716
|
+
assert_eq!(written, 5);
|
|
2717
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2718
|
+
|
|
2719
|
+
// Mark data for retransmission.
|
|
2720
|
+
let retransmitted = stream.send.retransmit(0, 5);
|
|
2721
|
+
assert_eq!(retransmitted, 5);
|
|
2722
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2723
|
+
|
|
2724
|
+
// Ack the data.
|
|
2725
|
+
stream.send.ack_and_drop(0, 5);
|
|
2726
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2727
|
+
|
|
2728
|
+
// Try to retransmit again - should return 0 since data is acked.
|
|
2729
|
+
let retransmitted = stream.send.retransmit(0, 5);
|
|
2730
|
+
assert_eq!(retransmitted, 0);
|
|
2731
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2732
|
+
}
|
|
2733
|
+
|
|
2734
|
+
#[test]
|
|
2735
|
+
fn retransmit_returns_partial_when_some_acked() {
|
|
2736
|
+
let mut stream = <Stream>::new(0, 15, 15, true, 0, 15);
|
|
2737
|
+
|
|
2738
|
+
// Write and emit 10 bytes.
|
|
2739
|
+
assert_eq!(stream.send.write(b"helloworld", false), Ok(10));
|
|
2740
|
+
assert_eq!(stream.send.buffered_bytes(), 10);
|
|
2741
|
+
|
|
2742
|
+
let mut buf = [0; 10];
|
|
2743
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2744
|
+
assert_eq!(written, 10);
|
|
2745
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2746
|
+
|
|
2747
|
+
// Mark all data for retransmission.
|
|
2748
|
+
let retransmitted = stream.send.retransmit(0, 10);
|
|
2749
|
+
assert_eq!(retransmitted, 10);
|
|
2750
|
+
assert_eq!(stream.send.buffered_bytes(), 10);
|
|
2751
|
+
|
|
2752
|
+
// Ack first 5 bytes and drop them.
|
|
2753
|
+
let dropped = stream.send.ack_and_drop(0, 5);
|
|
2754
|
+
assert_eq!(dropped, 5);
|
|
2755
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2756
|
+
|
|
2757
|
+
// Try to retransmit all 10 bytes - should return 5 since first 5 are
|
|
2758
|
+
// acked.
|
|
2759
|
+
let retransmitted = stream.send.retransmit(0, 10);
|
|
2760
|
+
assert_eq!(retransmitted, 0); // Already marked, so no change
|
|
2761
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2764
|
+
#[test]
|
|
2765
|
+
fn ack_and_drop_decrements_len_and_returns_dropped() {
|
|
2766
|
+
let mut stream = <Stream>::new(0, 15, 15, true, 0, 15);
|
|
2767
|
+
|
|
2768
|
+
// Write some data.
|
|
2769
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2770
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2771
|
+
|
|
2772
|
+
// Emit it.
|
|
2773
|
+
let mut buf = [0; 10];
|
|
2774
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2775
|
+
assert_eq!(written, 5);
|
|
2776
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2777
|
+
|
|
2778
|
+
// Mark for retransmission.
|
|
2779
|
+
let retransmitted = stream.send.retransmit(0, 5);
|
|
2780
|
+
assert_eq!(retransmitted, 5);
|
|
2781
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2782
|
+
|
|
2783
|
+
// Ack and drop - should decrement len and return dropped amount.
|
|
2784
|
+
let dropped = stream.send.ack_and_drop(0, 5);
|
|
2785
|
+
assert_eq!(dropped, 5);
|
|
2786
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
#[test]
|
|
2790
|
+
fn ack_and_drop_partial_buffer() {
|
|
2791
|
+
let mut stream = <Stream>::new(0, 30, 30, true, 0, 30);
|
|
2792
|
+
|
|
2793
|
+
// Write and emit two chunks.
|
|
2794
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2795
|
+
assert_eq!(stream.send.write(b"world", false), Ok(5));
|
|
2796
|
+
assert_eq!(stream.send.buffered_bytes(), 10);
|
|
2797
|
+
|
|
2798
|
+
let mut buf = [0; 10];
|
|
2799
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2800
|
+
assert_eq!(written, 10);
|
|
2801
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2802
|
+
|
|
2803
|
+
// Mark both chunks for retransmission.
|
|
2804
|
+
let retransmitted = stream.send.retransmit(0, 10);
|
|
2805
|
+
assert_eq!(retransmitted, 10);
|
|
2806
|
+
assert_eq!(stream.send.buffered_bytes(), 10);
|
|
2807
|
+
|
|
2808
|
+
// Ack and drop only first chunk.
|
|
2809
|
+
let dropped = stream.send.ack_and_drop(0, 5);
|
|
2810
|
+
assert_eq!(dropped, 5);
|
|
2811
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2812
|
+
|
|
2813
|
+
// Ack and drop second chunk.
|
|
2814
|
+
let dropped = stream.send.ack_and_drop(5, 5);
|
|
2815
|
+
assert_eq!(dropped, 5);
|
|
2816
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2817
|
+
}
|
|
2818
|
+
|
|
2819
|
+
#[test]
|
|
2820
|
+
fn ack_and_drop_returns_zero_when_nothing_dropped() {
|
|
2821
|
+
let mut stream = <Stream>::new(0, 15, 15, true, 0, 15);
|
|
2822
|
+
|
|
2823
|
+
// Write and emit data.
|
|
2824
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2825
|
+
let mut buf = [0; 10];
|
|
2826
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2827
|
+
assert_eq!(written, 5);
|
|
2828
|
+
|
|
2829
|
+
// Ack data that's already been fully emitted and not retransmitted.
|
|
2830
|
+
// Nothing should be dropped since there's no buffered data.
|
|
2831
|
+
let dropped = stream.send.ack_and_drop(0, 5);
|
|
2832
|
+
assert_eq!(dropped, 0);
|
|
2833
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2834
|
+
}
|
|
2835
|
+
|
|
2836
|
+
#[test]
|
|
2837
|
+
fn cache_consistency_through_full_lifecycle() {
|
|
2838
|
+
// This test verifies that StreamMap.tx_buffered stays in sync with
|
|
2839
|
+
// actual buffered data through a full lifecycle: write → emit →
|
|
2840
|
+
// retransmit → ack.
|
|
2841
|
+
let mut streams = <StreamMap>::new(5, 5, 15);
|
|
2842
|
+
|
|
2843
|
+
// Create a stream using low-level StreamMap interface.
|
|
2844
|
+
let local_params = crate::TransportParams {
|
|
2845
|
+
initial_max_data: 30,
|
|
2846
|
+
initial_max_stream_data_bidi_local: 15,
|
|
2847
|
+
initial_max_stream_data_bidi_remote: 15,
|
|
2848
|
+
initial_max_stream_data_uni: 10,
|
|
2849
|
+
initial_max_streams_bidi: 5,
|
|
2850
|
+
initial_max_streams_uni: 5,
|
|
2851
|
+
..Default::default()
|
|
2852
|
+
};
|
|
2853
|
+
let peer_params = local_params.clone();
|
|
2854
|
+
|
|
2855
|
+
// Update peer stream limits to allow locally-initiated streams.
|
|
2856
|
+
streams.update_peer_max_streams_bidi(5);
|
|
2857
|
+
streams.update_peer_max_streams_uni(5);
|
|
2858
|
+
|
|
2859
|
+
let stream_id = 0u64;
|
|
2860
|
+
|
|
2861
|
+
// Writing raises `stream.send.buffered_bytes()` and `tx_buffered`.
|
|
2862
|
+
{
|
|
2863
|
+
let stream = streams
|
|
2864
|
+
.get_or_create(stream_id, &local_params, &peer_params, true, false)
|
|
2865
|
+
.unwrap();
|
|
2866
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2867
|
+
}
|
|
2868
|
+
streams.add_tx_buffered(5);
|
|
2869
|
+
assert_eq!(streams.get(stream_id).unwrap().send.buffered_bytes(), 5);
|
|
2870
|
+
assert_eq!(streams.tx_buffered(), 5);
|
|
2871
|
+
assert!(streams.tx_buffered_is_consistent());
|
|
2872
|
+
|
|
2873
|
+
// Emitting lowers `stream.send.buffered_bytes()` and `tx_buffered`.
|
|
2874
|
+
let mut buf = [0; 10];
|
|
2875
|
+
let written = {
|
|
2876
|
+
let stream = streams.get_mut(stream_id).unwrap();
|
|
2877
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2878
|
+
written
|
|
2879
|
+
};
|
|
2880
|
+
assert_eq!(written, 5);
|
|
2881
|
+
streams.sub_tx_buffered(5);
|
|
2882
|
+
assert_eq!(streams.get(stream_id).unwrap().send.buffered_bytes(), 0);
|
|
2883
|
+
assert_eq!(streams.tx_buffered(), 0);
|
|
2884
|
+
assert!(streams.tx_buffered_is_consistent());
|
|
2885
|
+
|
|
2886
|
+
// Retransmitting raises both values by the amount retransmitted.
|
|
2887
|
+
let retransmitted = {
|
|
2888
|
+
let stream = streams.get_mut(stream_id).unwrap();
|
|
2889
|
+
stream.send.retransmit(0, 5)
|
|
2890
|
+
};
|
|
2891
|
+
assert_eq!(retransmitted, 5);
|
|
2892
|
+
streams.add_tx_buffered(retransmitted);
|
|
2893
|
+
assert_eq!(streams.get(stream_id).unwrap().send.buffered_bytes(), 5);
|
|
2894
|
+
assert_eq!(streams.tx_buffered(), 5);
|
|
2895
|
+
assert!(streams.tx_buffered_is_consistent());
|
|
2896
|
+
|
|
2897
|
+
// Ack and drop: both stream.send.buffered_bytes() and tx_buffered
|
|
2898
|
+
// decrease by actual amount dropped.
|
|
2899
|
+
let dropped = {
|
|
2900
|
+
let stream = streams.get_mut(stream_id).unwrap();
|
|
2901
|
+
stream.send.ack_and_drop(0, 5)
|
|
2902
|
+
};
|
|
2903
|
+
assert_eq!(dropped, 5);
|
|
2904
|
+
streams.sub_tx_buffered(dropped);
|
|
2905
|
+
assert_eq!(streams.get(stream_id).unwrap().send.buffered_bytes(), 0);
|
|
2906
|
+
assert_eq!(streams.tx_buffered(), 0);
|
|
2907
|
+
assert!(streams.tx_buffered_is_consistent());
|
|
2908
|
+
}
|
|
2909
|
+
|
|
2910
|
+
#[test]
|
|
2911
|
+
fn send_buf_len_reflects_buffered_data() {
|
|
2912
|
+
let mut stream = <Stream>::new(0, 15, 15, true, 0, 15);
|
|
2913
|
+
|
|
2914
|
+
// Initially empty.
|
|
2915
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2916
|
+
|
|
2917
|
+
// After write.
|
|
2918
|
+
assert_eq!(stream.send.write(b"hello", false), Ok(5));
|
|
2919
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2920
|
+
|
|
2921
|
+
// After emit.
|
|
2922
|
+
let mut buf = [0; 10];
|
|
2923
|
+
let (written, _) = stream.send.emit(&mut buf).unwrap();
|
|
2924
|
+
assert_eq!(written, 5);
|
|
2925
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2926
|
+
|
|
2927
|
+
// After retransmit.
|
|
2928
|
+
let retransmitted = stream.send.retransmit(0, 5);
|
|
2929
|
+
assert_eq!(retransmitted, 5);
|
|
2930
|
+
assert_eq!(stream.send.buffered_bytes(), 5);
|
|
2931
|
+
|
|
2932
|
+
// After ack_and_drop.
|
|
2933
|
+
let dropped = stream.send.ack_and_drop(0, 5);
|
|
2934
|
+
assert_eq!(dropped, 5);
|
|
2935
|
+
assert_eq!(stream.send.buffered_bytes(), 0);
|
|
2936
|
+
}
|
|
2207
2937
|
}
|
|
2208
2938
|
|
|
2209
2939
|
mod recv_buf;
|
|
2940
|
+
pub(crate) use recv_buf::ReceiveMemory;
|
|
2210
2941
|
mod send_buf;
|