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.
Files changed (99) hide show
  1. package/Cargo.lock +523 -140
  2. package/Cargo.toml +5 -5
  3. package/README.md +8 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +2 -0
  6. package/crates/native/src/config.rs +69 -8
  7. package/crates/native/src/driver.rs +253 -90
  8. package/crates/native/src/lib.rs +80 -57
  9. package/crates/native/src/parser_memory.rs +203 -0
  10. package/crates/native/src/server.rs +215 -183
  11. package/crates/native/src/session.rs +1341 -243
  12. package/crates/native/src/test_support.rs +119 -0
  13. package/crates/wtcore/Cargo.toml +1 -0
  14. package/crates/wtcore/src/h3.rs +250 -46
  15. package/dist/index.js +218 -75
  16. package/dist/index.js.map +7 -1
  17. package/dist/index.mjs +214 -69
  18. package/dist/index.mjs.map +7 -1
  19. package/dist/native.d.ts +10 -4
  20. package/dist/server.d.ts +1 -0
  21. package/package.json +84 -78
  22. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  24. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  25. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  26. package/scripts/build-linux-prebuilt.sh +46 -0
  27. package/scripts/build-ts.js +41 -0
  28. package/scripts/build.js +10 -3
  29. package/vendor/quiche/AGENTS.md +10 -0
  30. package/vendor/quiche/Cargo.lock +239 -248
  31. package/vendor/quiche/Cargo.toml +23 -10
  32. package/vendor/quiche/examples/Makefile +4 -4
  33. package/vendor/quiche/examples/client.rs +10 -28
  34. package/vendor/quiche/examples/http3-client.rs +20 -34
  35. package/vendor/quiche/examples/http3-server.rs +47 -78
  36. package/vendor/quiche/examples/server.rs +19 -49
  37. package/vendor/quiche/include/quiche.h +35 -4
  38. package/vendor/quiche/src/build.rs +17 -0
  39. package/vendor/quiche/src/cid.rs +129 -61
  40. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  41. package/vendor/quiche/src/crypto/mod.rs +64 -77
  42. package/vendor/quiche/src/dgram.rs +2 -3
  43. package/vendor/quiche/src/error.rs +22 -20
  44. package/vendor/quiche/src/ffi.rs +558 -280
  45. package/vendor/quiche/src/flowcontrol.rs +0 -7
  46. package/vendor/quiche/src/frame.rs +134 -135
  47. package/vendor/quiche/src/h3/ffi.rs +73 -60
  48. package/vendor/quiche/src/h3/frame.rs +70 -90
  49. package/vendor/quiche/src/h3/mod.rs +1024 -593
  50. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  51. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  52. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  53. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  54. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  55. package/vendor/quiche/src/h3/stream.rs +1009 -194
  56. package/vendor/quiche/src/lib.rs +1361 -1195
  57. package/vendor/quiche/src/minmax.rs +2 -6
  58. package/vendor/quiche/src/packet.rs +420 -552
  59. package/vendor/quiche/src/path.rs +202 -72
  60. package/vendor/quiche/src/pmtud.rs +18 -16
  61. package/vendor/quiche/src/ranges.rs +184 -82
  62. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  63. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  64. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  65. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  66. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  67. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  68. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  69. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  70. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  71. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  72. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  73. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  74. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  84. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  85. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  86. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  87. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  88. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  89. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  90. package/vendor/quiche/src/recovery/mod.rs +414 -173
  91. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  92. package/vendor/quiche/src/stream/mod.rs +864 -133
  93. package/vendor/quiche/src/stream/recv_buf.rs +476 -172
  94. package/vendor/quiche/src/stream/send_buf.rs +343 -81
  95. package/vendor/quiche/src/test_utils.rs +78 -64
  96. package/vendor/quiche/src/tests.rs +2559 -1335
  97. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  98. package/vendor/quiche/src/tls/mod.rs +146 -193
  99. package/vendor/quiche/src/transport_params.rs +87 -103
@@ -47,6 +47,7 @@ use crate::recovery::MINIMUM_WINDOW_PACKETS;
47
47
 
48
48
  pub(crate) static CUBIC: CongestionControlOps = CongestionControlOps {
49
49
  on_init,
50
+ on_mtu_update: |_| {},
50
51
  on_packet_sent,
51
52
  on_packets_acked,
52
53
  congestion_event,
@@ -140,23 +141,18 @@ impl State {
140
141
  fn w_cubic(&self, t: Duration, max_datagram_size: usize) -> f64 {
141
142
  let w_max = self.w_max / max_datagram_size as f64;
142
143
 
143
- (C * (t.as_secs_f64() - self.k).powi(3) + w_max) *
144
- max_datagram_size as f64
144
+ (C * (t.as_secs_f64() - self.k).powi(3) + w_max) * max_datagram_size as f64
145
145
  }
146
146
 
147
147
  // W_est = W_est + alpha_aimd * (segments_acked / cwnd) (Eq. 4)
148
- fn w_est_inc(
149
- &self, acked: usize, cwnd: usize, max_datagram_size: usize,
150
- ) -> f64 {
148
+ fn w_est_inc(&self, acked: usize, cwnd: usize, max_datagram_size: usize) -> f64 {
151
149
  self.alpha_aimd * (acked as f64 / cwnd as f64) * max_datagram_size as f64
152
150
  }
153
151
  }
154
152
 
155
153
  fn on_init(_r: &mut Congestion) {}
156
154
 
157
- fn on_packet_sent(
158
- r: &mut Congestion, sent_bytes: usize, bytes_in_flight: usize, now: Instant,
159
- ) {
155
+ fn on_packet_sent(r: &mut Congestion, sent_bytes: usize, bytes_in_flight: usize, now: Instant) {
160
156
  reno::on_packet_sent(r, sent_bytes, bytes_in_flight, now);
161
157
 
162
158
  // Don't adjust epoch or track send time for non-data packets
@@ -187,8 +183,7 @@ fn on_packet_sent(
187
183
  if let Some(idle_start) = idle_start {
188
184
  if idle_start < now {
189
185
  let delta = now - idle_start;
190
- r.congestion_recovery_start_time =
191
- Some(recovery_start_time + delta);
186
+ r.congestion_recovery_start_time = Some(recovery_start_time + delta);
192
187
  }
193
188
  }
194
189
  }
@@ -198,8 +193,11 @@ fn on_packet_sent(
198
193
  }
199
194
 
200
195
  fn on_packets_acked(
201
- r: &mut Congestion, bytes_in_flight: usize, packets: &mut Vec<Acked>,
202
- now: Instant, rtt_stats: &RttStats,
196
+ r: &mut Congestion,
197
+ bytes_in_flight: usize,
198
+ packets: &mut Vec<Acked>,
199
+ now: Instant,
200
+ rtt_stats: &RttStats,
203
201
  ) {
204
202
  r.cubic_state.last_ack_time = Some(now);
205
203
 
@@ -209,7 +207,10 @@ fn on_packets_acked(
209
207
  }
210
208
 
211
209
  fn on_packet_acked(
212
- r: &mut Congestion, bytes_in_flight: usize, packet: &Acked, now: Instant,
210
+ r: &mut Congestion,
211
+ bytes_in_flight: usize,
212
+ packet: &Acked,
213
+ now: Instant,
213
214
  rtt_stats: &RttStats,
214
215
  ) {
215
216
  let in_congestion_recovery = r.in_congestion_recovery(packet.time_sent);
@@ -232,15 +233,14 @@ fn on_packet_acked(
232
233
  // Detecting spurious congestion events.
233
234
  // <https://tools.ietf.org/id/draft-ietf-tcpm-rfc8312bis-00.html#section-4.9>
234
235
  //
235
- // When the recovery episode ends with recovering
236
- // a few packets (less than cwnd / mss * ROLLBACK_THRESHOLD_PERCENT(%)), it's
237
- // considered as spurious and restore to the previous state.
236
+ // If recovery ends after fewer than
237
+ // `cwnd / mss * ROLLBACK_THRESHOLD_PERCENT` packets, treat the congestion
238
+ // event as spurious and restore the previous state.
238
239
  if r.congestion_recovery_start_time.is_some() {
239
240
  let new_lost = r.lost_count - r.cubic_state.prior.lost_count;
240
241
 
241
- let rollback_threshold = (r.congestion_window / r.max_datagram_size) *
242
- ROLLBACK_THRESHOLD_PERCENT /
243
- 100;
242
+ let rollback_threshold =
243
+ (r.congestion_window / r.max_datagram_size) * ROLLBACK_THRESHOLD_PERCENT / 100;
244
244
 
245
245
  let rollback_threshold = rollback_threshold.max(MIN_ROLLBACK_THRESHOLD);
246
246
 
@@ -259,8 +259,7 @@ fn on_packet_acked(
259
259
 
260
260
  if r.bytes_acked_sl >= r.max_datagram_size {
261
261
  if r.hystart.in_css() {
262
- r.congestion_window +=
263
- r.hystart.css_cwnd_inc(r.max_datagram_size);
262
+ r.congestion_window += r.hystart.css_cwnd_inc(r.max_datagram_size);
264
263
  } else {
265
264
  r.congestion_window += r.max_datagram_size;
266
265
  }
@@ -302,7 +301,7 @@ fn on_packet_acked(
302
301
 
303
302
  r.cubic_state.w_est = r.congestion_window as f64;
304
303
  r.cubic_state.alpha_aimd = ALPHA_AIMD;
305
- },
304
+ }
306
305
  }
307
306
  }
308
307
 
@@ -318,11 +317,9 @@ fn on_packet_acked(
318
317
  let target = f64::min(target, r.congestion_window as f64 * 1.5);
319
318
 
320
319
  // Update w_est.
321
- let w_est_inc = r.cubic_state.w_est_inc(
322
- packet.size,
323
- r.congestion_window,
324
- r.max_datagram_size,
325
- );
320
+ let w_est_inc =
321
+ r.cubic_state
322
+ .w_est_inc(packet.size, r.congestion_window, r.max_datagram_size);
326
323
  r.cubic_state.w_est += w_est_inc;
327
324
 
328
325
  if r.cubic_state.w_est >= r.cubic_state.w_max {
@@ -336,8 +333,7 @@ fn on_packet_acked(
336
333
  cubic_cwnd = cmp::max(cubic_cwnd, r.cubic_state.w_est as usize);
337
334
  } else {
338
335
  // Concave region or convex region use same increment.
339
- let cubic_inc =
340
- r.max_datagram_size * (target as usize - cubic_cwnd) / cubic_cwnd;
336
+ let cubic_inc = r.max_datagram_size * (target as usize - cubic_cwnd) / cubic_cwnd;
341
337
 
342
338
  cubic_cwnd += cubic_inc;
343
339
  }
@@ -353,8 +349,11 @@ fn on_packet_acked(
353
349
  }
354
350
 
355
351
  fn congestion_event(
356
- r: &mut Congestion, bytes_in_flight: usize, _lost_bytes: usize,
357
- largest_lost_pkt: &Sent, now: Instant,
352
+ r: &mut Congestion,
353
+ bytes_in_flight: usize,
354
+ _lost_bytes: usize,
355
+ largest_lost_pkt: &Sent,
356
+ now: Instant,
358
357
  ) {
359
358
  let time_sent = largest_lost_pkt.time_sent;
360
359
  let in_congestion_recovery = r.in_congestion_recovery(time_sent);
@@ -366,15 +365,13 @@ fn congestion_event(
366
365
 
367
366
  // Fast convergence
368
367
  if (r.congestion_window as f64) < r.cubic_state.w_max {
369
- r.cubic_state.w_max =
370
- r.congestion_window as f64 * (1.0 + BETA_CUBIC) / 2.0;
368
+ r.cubic_state.w_max = r.congestion_window as f64 * (1.0 + BETA_CUBIC) / 2.0;
371
369
  } else {
372
370
  r.cubic_state.w_max = r.congestion_window as f64;
373
371
  }
374
372
 
375
373
  let ssthresh = (r.congestion_window as f64 * BETA_CUBIC) as usize;
376
- let ssthresh =
377
- cmp::max(ssthresh, r.max_datagram_size * MINIMUM_WINDOW_PACKETS);
374
+ let ssthresh = cmp::max(ssthresh, r.max_datagram_size * MINIMUM_WINDOW_PACKETS);
378
375
  r.ssthresh.update(ssthresh, r.hystart.in_css());
379
376
  r.congestion_window = ssthresh;
380
377
 
@@ -385,8 +382,7 @@ fn congestion_event(
385
382
  .cubic_k(r.congestion_window, r.max_datagram_size)
386
383
  };
387
384
 
388
- r.cubic_state.cwnd_inc =
389
- (r.cubic_state.cwnd_inc as f64 * BETA_CUBIC) as usize;
385
+ r.cubic_state.cwnd_inc = (r.cubic_state.cwnd_inc as f64 * BETA_CUBIC) as usize;
390
386
 
391
387
  r.cubic_state.w_est = r.congestion_window as f64;
392
388
  r.cubic_state.alpha_aimd = ALPHA_AIMD;
@@ -652,8 +648,7 @@ mod tests {
652
648
  assert!(sender.hystart.css_start_time().is_none());
653
649
  assert_eq!(
654
650
  sender.congestion_window(),
655
- cwnd_prev +
656
- size / hystart::CSS_GROWTH_DIVISOR * hystart::N_RTT_SAMPLE
651
+ cwnd_prev + size / hystart::CSS_GROWTH_DIVISOR * hystart::N_RTT_SAMPLE
657
652
  );
658
653
  }
659
654
 
@@ -886,8 +881,7 @@ mod tests {
886
881
  sender.lose_n_packets(1, size, None);
887
882
  let post_loss_cwnd = sender.congestion_window;
888
883
  assert_eq!(post_loss_cwnd, (initial_cwnd as f64 * BETA_CUBIC) as usize);
889
- let initial_recovery_start_time =
890
- sender.congestion_recovery_start_time.unwrap();
884
+ let initial_recovery_start_time = sender.congestion_recovery_start_time.unwrap();
891
885
  assert_eq!(initial_recovery_start_time, sender.time);
892
886
 
893
887
  // ACK remaining in-flight packets to exit recovery.
@@ -919,8 +913,7 @@ mod tests {
919
913
  );
920
914
  // Recovery start hasn't changed.
921
915
  assert_eq!(
922
- sender.congestion_recovery_start_time.unwrap() -
923
- initial_recovery_start_time,
916
+ sender.congestion_recovery_start_time.unwrap() - initial_recovery_start_time,
924
917
  Duration::ZERO,
925
918
  "epoch should not have shifted without idle gap",
926
919
  );
@@ -944,8 +937,7 @@ mod tests {
944
937
  sender.advance_time(rtt);
945
938
  sender.lose_n_packets(1, size, None);
946
939
 
947
- let initial_recovery_start =
948
- sender.congestion_recovery_start_time.unwrap();
940
+ let initial_recovery_start = sender.congestion_recovery_start_time.unwrap();
949
941
  assert_eq!(
950
942
  initial_recovery_start,
951
943
  test_start + rtt,
@@ -979,8 +971,7 @@ mod tests {
979
971
  // idle shift logic ran).
980
972
  let recovery_start = sender.congestion_recovery_start_time.unwrap();
981
973
  assert!(
982
- recovery_start >
983
- sender.cubic_state.last_sent_time.unwrap() - idle_duration,
974
+ recovery_start > sender.cubic_state.last_sent_time.unwrap() - idle_duration,
984
975
  "epoch should have been shifted forward by idle period",
985
976
  );
986
977
  assert_eq!(
@@ -81,9 +81,7 @@ impl Default for Rate {
81
81
  }
82
82
 
83
83
  impl Rate {
84
- pub fn on_packet_sent(
85
- &mut self, pkt: &mut Sent, bytes_in_flight: usize, bytes_lost: u64,
86
- ) {
84
+ pub fn on_packet_sent(&mut self, pkt: &mut Sent, bytes_in_flight: usize, bytes_lost: u64) {
87
85
  // No packets in flight.
88
86
  if bytes_in_flight == 0 {
89
87
  self.first_sent_time = pkt.time_sent;
@@ -107,8 +105,8 @@ impl Rate {
107
105
 
108
106
  // Update info using the newest packet. If rate_sample is not yet
109
107
  // initialized, initialize with the first packet.
110
- if self.rate_sample.prior_time.is_none() ||
111
- pkt.delivered >= self.rate_sample.prior_delivered
108
+ if self.rate_sample.prior_time.is_none()
109
+ || pkt.delivered >= self.rate_sample.prior_delivered
112
110
  {
113
111
  self.rate_sample.prior_delivered = pkt.delivered;
114
112
  self.rate_sample.prior_time = Some(pkt.delivered_time);
@@ -138,8 +136,7 @@ impl Rate {
138
136
  .send_elapsed
139
137
  .max(self.rate_sample.ack_elapsed);
140
138
 
141
- self.rate_sample.delivered =
142
- self.delivered - self.rate_sample.prior_delivered;
139
+ self.rate_sample.delivered = self.delivered - self.rate_sample.prior_delivered;
143
140
  self.rate_sample.interval = interval;
144
141
 
145
142
  if interval < min_rtt {
@@ -151,10 +148,8 @@ impl Rate {
151
148
 
152
149
  if !interval.is_zero() {
153
150
  let rate_sample_bandwidth = {
154
- let rate_sample_bytes_per_second = (self.rate_sample.delivered
155
- as f64 /
156
- interval.as_secs_f64())
157
- as u64;
151
+ let rate_sample_bytes_per_second =
152
+ (self.rate_sample.delivered as f64 / interval.as_secs_f64()) as u64;
158
153
 
159
154
  Bandwidth::from_bytes_per_second(rate_sample_bytes_per_second)
160
155
  };
@@ -165,8 +160,8 @@ impl Rate {
165
160
  // - the new rate is higher than the previous value
166
161
  //
167
162
  // [linux] https://github.com/torvalds/linux/commit/eb8329e0a04db0061f714f033b4454326ba147f4
168
- if !self.rate_sample.is_app_limited ||
169
- rate_sample_bandwidth > self.rate_sample.bandwidth
163
+ if !self.rate_sample.is_app_limited
164
+ || rate_sample_bandwidth > self.rate_sample.bandwidth
170
165
  {
171
166
  self.update_delivery_rate(rate_sample_bandwidth);
172
167
  }
@@ -179,8 +174,7 @@ impl Rate {
179
174
  }
180
175
 
181
176
  pub fn update_app_limited(&mut self, v: bool) {
182
- self.end_of_app_limited =
183
- if v { self.last_sent_packet.max(1) } else { 0 };
177
+ self.end_of_app_limited = if v { self.last_sent_packet.max(1) } else { 0 };
184
178
  }
185
179
 
186
180
  pub fn app_limited(&mut self) -> bool {
@@ -254,11 +248,10 @@ mod tests {
254
248
  use crate::OnAckReceivedOutcome;
255
249
  use std::ops::Range;
256
250
 
257
- // A [RateSample](delivery_rate::RateSample) is app_limited if it was
258
- // generated when the [Rate](delivery_rate::Rate) was app_limited.
251
+ // A `RateSample` generated while `Rate` is app-limited inherits that state.
259
252
  //
260
- // The following test generates RateSamples before and after Rate is
261
- // app_limited and asserts on app_limited status for samples.
253
+ // This test generates samples before and after `Rate` becomes app-limited
254
+ // and checks each sample's state.
262
255
  #[test]
263
256
  fn sample_is_app_limited() {
264
257
  let config = Config::new(0xbabababa).unwrap();
@@ -266,57 +259,56 @@ mod tests {
266
259
  let mut now = Instant::now();
267
260
  let mss = r.max_datagram_size();
268
261
 
269
- // Not App Limited prior to any activity
262
+ // `Rate` is not app-limited before any activity.
270
263
  assert!(!r.congestion.delivery_rate.app_limited());
271
264
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
272
265
  assert!(!r.congestion.delivery_rate.sample_is_app_limited());
273
266
 
274
- // Send/Ack first batch to generate a new delivery_rate sample.
267
+ // Generate a delivery-rate sample from the first batch.
275
268
  let rtt = Duration::from_secs(2);
276
269
  helper_send_and_ack_packets(&mut r, 0..4, now, rtt, mss);
277
270
 
278
- // Marking Rate as app_limited.
271
+ // Mark `Rate` as app-limited.
279
272
  r.delivery_rate_update_app_limited(true);
280
273
  assert!(r.congestion.delivery_rate.app_limited());
281
274
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 3);
282
275
 
283
- // Rate is app_limited.
276
+ // `Rate` is app-limited.
284
277
  assert!(r.congestion.delivery_rate.app_limited());
285
278
  assert!(!r.congestion.delivery_rate.sample_is_app_limited());
286
279
 
287
- // Send/Ack second batch to generate a new delivery_rate sample.
280
+ // Send and acknowledge the second batch to generate another sample.
288
281
  now += rtt;
289
282
  helper_send_and_ack_packets(&mut r, 4..8, now, rtt, mss);
290
283
 
291
- // Rate is no longer app limited since we sent a packet larger than
292
- // `end_of_app_limited`
284
+ // `Rate` is no longer app-limited after sending a packet beyond
285
+ // `end_of_app_limited`.
293
286
  assert!(!r.congestion.delivery_rate.app_limited());
294
287
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
295
- // The RateSample is also app_limited
288
+ // The resulting `RateSample` remains app-limited.
296
289
  assert!(r.congestion.delivery_rate.sample_is_app_limited());
297
290
  }
298
291
 
299
- // A [RateSample](delivery_rate::RateSample) is is only updated if either not
300
- // app_limited or greater than the previous value.
292
+ // A `RateSample` updates the delivery rate only when it is not app-limited
293
+ // or its rate exceeds the previous value.
301
294
  #[test]
302
295
  fn app_limited_delivery_rate() {
303
- // confirm that rate sample is not generated when app limited
296
+ // Confirm that a rate sample is not generated when app-limited.
304
297
  let config = Config::new(0xbabababa).unwrap();
305
298
  let mut r = LegacyRecovery::new(&config);
306
299
  let mut now = Instant::now();
307
300
  let mss = r.max_datagram_size();
308
301
 
309
- // Not App Limited prior to any activity
302
+ // `Rate` is not app-limited before any activity.
310
303
  assert!(!r.congestion.delivery_rate.app_limited());
311
304
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
312
305
  assert!(!r.congestion.delivery_rate.sample_is_app_limited());
313
306
 
314
- // First batch
315
- // Send/Ack first batch to generate a new delivery_rate sample.
307
+ // Generate a delivery-rate sample from the first batch.
316
308
  let mut rtt = Duration::from_secs(2);
317
309
  helper_send_and_ack_packets(&mut r, 0..2, now, rtt, mss);
318
310
 
319
- // Marking Rate as app_limited.
311
+ // Mark `Rate` as app-limited.
320
312
  r.delivery_rate_update_app_limited(true);
321
313
  assert!(r.congestion.delivery_rate.app_limited());
322
314
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 1);
@@ -327,50 +319,46 @@ mod tests {
327
319
  assert_eq!(expected_delivery_rate, 1200);
328
320
  assert_eq!(first_delivery_rate, expected_delivery_rate);
329
321
 
330
- // Second batch
331
- // Since Rtt is larger, the delivery_rate will be smaller and not generate
332
- // a new RateSample.
322
+ // A larger RTT produces a lower delivery rate, which does not replace
323
+ // the app-limited sample.
333
324
  now += rtt;
334
325
  rtt = Duration::from_secs(4);
335
326
  helper_send_and_ack_packets(&mut r, 2..4, now, rtt, mss);
336
327
 
337
- // Rate is no longer app limited since we sent a packet larger than
338
- // `end_of_app_limited`
328
+ // `Rate` is no longer app-limited after sending a packet beyond
329
+ // `end_of_app_limited`.
339
330
  assert!(!r.congestion.delivery_rate.app_limited());
340
331
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
341
- // The RateSample is also app_limited
332
+ // The resulting `RateSample` remains app-limited.
342
333
  assert!(r.congestion.delivery_rate.sample_is_app_limited());
343
334
 
344
- // Delivery rate NOT updated since the delivery rate is less than previous
345
- // value
335
+ // The lower delivery rate does not replace the previous value.
346
336
  let expected_delivery_rate = (mss * 2) as u64 / rtt.as_secs();
347
337
  assert_eq!(expected_delivery_rate, 600);
348
338
  let app_limited_delivery_rate = r.delivery_rate().to_bytes_per_second();
349
339
  assert_eq!(app_limited_delivery_rate, first_delivery_rate);
350
340
 
351
- // Marking Rate as app_limited.
341
+ // Mark `Rate` as app-limited.
352
342
  r.delivery_rate_update_app_limited(true);
353
343
  assert!(r.congestion.delivery_rate.app_limited());
354
344
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 3);
355
- // The RateSample is also app_limited
345
+ // The resulting `RateSample` remains app-limited.
356
346
  assert!(r.congestion.delivery_rate.sample_is_app_limited());
357
347
 
358
- // Third batch
359
- // Since Rtt is smaller, the delivery_rate will be larger and not generate
360
- // a new RateSample even when app_limited.
348
+ // A smaller RTT produces a higher delivery rate, which replaces the
349
+ // previous value even while app-limited.
361
350
  now += rtt;
362
351
  rtt = Duration::from_secs(1);
363
352
  helper_send_and_ack_packets(&mut r, 4..6, now, rtt, mss);
364
353
 
365
- // Rate is no longer app limited since we sent a packet larger than
366
- // `end_of_app_limited`
354
+ // `Rate` is no longer app-limited after sending a packet beyond
355
+ // `end_of_app_limited`.
367
356
  assert!(!r.congestion.delivery_rate.app_limited());
368
357
  assert_eq!(r.congestion.delivery_rate.end_of_app_limited, 0);
369
- // The RateSample is also app_limited
358
+ // The resulting `RateSample` remains app-limited.
370
359
  assert!(r.congestion.delivery_rate.sample_is_app_limited());
371
360
 
372
- // Delivery rate NOT updated since the delivery rate is less than previous
373
- // value
361
+ // The higher delivery rate replaces the previous value.
374
362
  let expected_delivery_rate = (mss * 2) as u64 / rtt.as_secs();
375
363
  assert_eq!(expected_delivery_rate, 2400);
376
364
  let app_limited_delivery_rate = r.delivery_rate().to_bytes_per_second();
@@ -472,8 +460,11 @@ mod tests {
472
460
  }
473
461
 
474
462
  fn helper_send_and_ack_packets(
475
- recovery: &mut LegacyRecovery, range: Range<u64>, now: Instant,
476
- rtt: Duration, mss: usize,
463
+ recovery: &mut LegacyRecovery,
464
+ range: Range<u64>,
465
+ now: Instant,
466
+ rtt: Duration,
467
+ mss: usize,
477
468
  ) {
478
469
  for pn in range.clone() {
479
470
  let pkt = test_utils::helper_packet_sent(pn, now, mss);
@@ -504,11 +495,14 @@ mod tests {
504
495
  )
505
496
  .unwrap();
506
497
 
507
- assert_eq!(ack_outcome, OnAckReceivedOutcome {
508
- lost_packets: 0,
509
- lost_bytes: 0,
510
- acked_bytes: mss * packet_count,
511
- spurious_losses: 0,
512
- });
498
+ assert_eq!(
499
+ ack_outcome,
500
+ OnAckReceivedOutcome {
501
+ lost_packets: 0,
502
+ lost_bytes: 0,
503
+ acked_bytes: mss * packet_count,
504
+ spurious_losses: 0,
505
+ }
506
+ );
513
507
  }
514
508
  }
@@ -120,9 +120,7 @@ impl Hystart {
120
120
  }
121
121
 
122
122
  // On receiving ACK. Returns true if need to enter Congestion Avoidance.
123
- pub fn on_packet_acked(
124
- &mut self, packet: &Acked, rtt: Duration, now: Instant,
125
- ) -> bool {
123
+ pub fn on_packet_acked(&mut self, packet: &Acked, rtt: Duration, now: Instant) -> bool {
126
124
  if !self.enabled {
127
125
  return false;
128
126
  }
@@ -133,19 +131,17 @@ impl Hystart {
133
131
 
134
132
  // Slow Start.
135
133
  if self.css_start_time().is_none() {
136
- if self.rtt_sample_count >= N_RTT_SAMPLE &&
137
- self.current_round_min_rtt != Duration::MAX &&
138
- self.last_round_min_rtt != Duration::MAX
134
+ if self.rtt_sample_count >= N_RTT_SAMPLE
135
+ && self.current_round_min_rtt != Duration::MAX
136
+ && self.last_round_min_rtt != Duration::MAX
139
137
  {
140
138
  // clamp(min_rtt_thresh, last_round_min_rtt/8,
141
139
  // max_rtt_thresh)
142
- let rtt_thresh =
143
- cmp::max(self.last_round_min_rtt / 8, MIN_RTT_THRESH);
140
+ let rtt_thresh = cmp::max(self.last_round_min_rtt / 8, MIN_RTT_THRESH);
144
141
  let rtt_thresh = cmp::min(rtt_thresh, MAX_RTT_THRESH);
145
142
 
146
143
  // Check if we can exit to CSS.
147
- if self.current_round_min_rtt >=
148
- self.last_round_min_rtt.saturating_add(rtt_thresh)
144
+ if self.current_round_min_rtt >= self.last_round_min_rtt.saturating_add(rtt_thresh)
149
145
  {
150
146
  self.css_baseline_min_rtt = self.current_round_min_rtt;
151
147
  self.css_start_time = Some(now);