rwebtransport 0.1.0 → 0.1.2

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 (113) hide show
  1. package/Cargo.lock +666 -666
  2. package/Cargo.toml +31 -31
  3. package/LICENSE +202 -202
  4. package/NOTICE +41 -41
  5. package/README.md +550 -550
  6. package/crates/echo-server/Cargo.toml +20 -20
  7. package/crates/echo-server/src/main.rs +666 -666
  8. package/crates/native/Cargo.toml +23 -23
  9. package/crates/native/src/config.rs +129 -129
  10. package/crates/native/src/driver.rs +463 -463
  11. package/crates/native/src/lib.rs +821 -821
  12. package/crates/native/src/server.rs +718 -718
  13. package/crates/native/src/session.rs +1191 -1191
  14. package/crates/native/src/tls.rs +96 -96
  15. package/crates/wtcore/Cargo.toml +15 -15
  16. package/crates/wtcore/src/h3.rs +226 -226
  17. package/crates/wtcore/src/lib.rs +9 -9
  18. package/dist/index.js.map +1 -1
  19. package/dist/index.mjs.map +1 -1
  20. package/package.json +80 -79
  21. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  23. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  24. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  25. package/scripts/bench.mjs +180 -180
  26. package/scripts/build.js +67 -67
  27. package/scripts/postinstall.js +67 -67
  28. package/scripts/setup-unix.sh +129 -129
  29. package/scripts/setup-windows.ps1 +229 -229
  30. package/vendor/quiche/AGENTS.md +75 -75
  31. package/vendor/quiche/COPYING +23 -23
  32. package/vendor/quiche/Cargo.lock +1212 -1212
  33. package/vendor/quiche/Cargo.toml +215 -215
  34. package/vendor/quiche/README.md +466 -466
  35. package/vendor/quiche/examples/Makefile +46 -46
  36. package/vendor/quiche/examples/README.md +20 -20
  37. package/vendor/quiche/examples/cert-big.crt +90 -90
  38. package/vendor/quiche/examples/cert.crt +18 -18
  39. package/vendor/quiche/examples/cert.key +28 -28
  40. package/vendor/quiche/examples/client.c +343 -343
  41. package/vendor/quiche/examples/client.rs +281 -281
  42. package/vendor/quiche/examples/gen-certs.sh +15 -15
  43. package/vendor/quiche/examples/http3-client.c +473 -473
  44. package/vendor/quiche/examples/http3-client.rs +349 -349
  45. package/vendor/quiche/examples/http3-server.c +614 -614
  46. package/vendor/quiche/examples/http3-server.rs +680 -680
  47. package/vendor/quiche/examples/qpack-decode.rs +87 -87
  48. package/vendor/quiche/examples/qpack-encode.rs +88 -88
  49. package/vendor/quiche/examples/rootca.crt +21 -21
  50. package/vendor/quiche/examples/server.c +529 -529
  51. package/vendor/quiche/examples/server.rs +529 -529
  52. package/vendor/quiche/include/quiche.h +1282 -1282
  53. package/vendor/quiche/src/buffers.rs +105 -105
  54. package/vendor/quiche/src/build.rs +66 -66
  55. package/vendor/quiche/src/cid.rs +1079 -1079
  56. package/vendor/quiche/src/crypto/boringssl.rs +373 -373
  57. package/vendor/quiche/src/crypto/mod.rs +671 -671
  58. package/vendor/quiche/src/dgram.rs +118 -118
  59. package/vendor/quiche/src/error.rs +263 -263
  60. package/vendor/quiche/src/ffi.rs +2290 -2290
  61. package/vendor/quiche/src/flowcontrol.rs +246 -246
  62. package/vendor/quiche/src/frame.rs +2172 -2172
  63. package/vendor/quiche/src/h3/AGENTS.md +54 -54
  64. package/vendor/quiche/src/h3/ffi.rs +442 -442
  65. package/vendor/quiche/src/h3/frame.rs +1404 -1404
  66. package/vendor/quiche/src/h3/mod.rs +8223 -8223
  67. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -288
  68. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -235
  69. package/vendor/quiche/src/h3/qpack/mod.rs +202 -202
  70. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -323
  71. package/vendor/quiche/src/h3/stream.rs +1595 -1595
  72. package/vendor/quiche/src/lib.rs +9556 -9556
  73. package/vendor/quiche/src/minmax.rs +446 -446
  74. package/vendor/quiche/src/packet.rs +2334 -2334
  75. package/vendor/quiche/src/path.rs +1374 -1374
  76. package/vendor/quiche/src/pmtud.rs +652 -652
  77. package/vendor/quiche/src/rand.rs +64 -64
  78. package/vendor/quiche/src/range_buf.rs +186 -186
  79. package/vendor/quiche/src/ranges.rs +755 -755
  80. package/vendor/quiche/src/recovery/AGENTS.md +80 -80
  81. package/vendor/quiche/src/recovery/bandwidth.rs +565 -565
  82. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -179
  83. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -1038
  84. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -514
  85. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -241
  86. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -361
  87. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -238
  88. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -1170
  89. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -291
  90. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -201
  91. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -1947
  92. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -158
  93. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -37
  94. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -120
  95. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -318
  96. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -784
  97. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -729
  98. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -175
  99. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -190
  100. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -855
  101. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -275
  102. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -294
  103. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -1384
  104. package/vendor/quiche/src/recovery/mod.rs +3086 -3086
  105. package/vendor/quiche/src/recovery/rtt.rs +152 -152
  106. package/vendor/quiche/src/stream/mod.rs +2210 -2210
  107. package/vendor/quiche/src/stream/recv_buf.rs +1236 -1236
  108. package/vendor/quiche/src/stream/send_buf.rs +849 -849
  109. package/vendor/quiche/src/test_utils.rs +681 -681
  110. package/vendor/quiche/src/tests.rs +12922 -12922
  111. package/vendor/quiche/src/tls/boringssl.rs +375 -375
  112. package/vendor/quiche/src/tls/mod.rs +1245 -1245
  113. package/vendor/quiche/src/transport_params.rs +628 -628
@@ -1,294 +1,294 @@
1
- // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2
- // Use of this source code is governed by a BSD-style license that can be
3
- // found in the LICENSE file.
4
-
5
- // Copyright (C) 2023, Cloudflare, Inc.
6
- // All rights reserved.
7
- //
8
- // Redistribution and use in source and binary forms, with or without
9
- // modification, are permitted provided that the following conditions are
10
- // met:
11
- //
12
- // * Redistributions of source code must retain the above copyright notice,
13
- // this list of conditions and the following disclaimer.
14
- //
15
- // * Redistributions in binary form must reproduce the above copyright
16
- // notice, this list of conditions and the following disclaimer in the
17
- // documentation and/or other materials provided with the distribution.
18
- //
19
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
- // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
- // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
- // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
- // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
- // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
- // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
- // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
- // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
- // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
- // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-
31
- use std::time::Instant;
32
-
33
- use crate::recovery::gcongestion::bbr2::BBRv2;
34
- use crate::recovery::gcongestion::Bandwidth;
35
- use crate::recovery::gcongestion::CongestionControl;
36
- use crate::recovery::rtt::RttStats;
37
- use crate::recovery::RecoveryStats;
38
- use crate::recovery::ReleaseDecision;
39
- use crate::recovery::ReleaseTime;
40
-
41
- use super::Acked;
42
- use super::Lost;
43
-
44
- /// Congestion window fraction that the pacing sender allows in bursts during
45
- /// pacing.
46
- const LUMPY_PACING_CWND_FRACTION: f64 = 0.25;
47
-
48
- /// Number of packets that the pacing sender allows in bursts during pacing.
49
- /// This is ignored if a flow's estimated bandwidth is lower than 1200 kbps.
50
- const LUMPY_PACING_SIZE: usize = 2;
51
-
52
- /// The minimum estimated client bandwidth below which the pacing sender will
53
- /// not allow bursts.
54
- const LUMPY_PACING_MIN_BANDWIDTH_KBPS: Bandwidth =
55
- Bandwidth::from_kbits_per_second(1_200);
56
-
57
- /// Configured maximum size of the burst coming out of quiescence. The burst is
58
- /// never larger than the current CWND in packets.
59
- const INITIAL_UNPACED_BURST: usize = 10;
60
-
61
- #[derive(Debug)]
62
- pub struct Pacer {
63
- /// Should this [`Pacer`] be making any release decisions?
64
- enabled: bool,
65
- /// Underlying sender
66
- sender: BBRv2,
67
- /// The maximum rate the [`Pacer`] will use.
68
- max_pacing_rate: Option<Bandwidth>,
69
- /// Number of unpaced packets to be sent before packets are delayed.
70
- burst_tokens: usize,
71
- /// When can the next packet be sent.
72
- ideal_next_packet_send_time: ReleaseTime,
73
- initial_burst_size: usize,
74
- /// Number of unpaced packets to be sent before packets are delayed. This
75
- /// token is consumed after [`Self::burst_tokens`] ran out.
76
- lumpy_tokens: usize,
77
- /// Indicates whether pacing throttles the sending. If true, make up for
78
- /// lost time.
79
- pacing_limited: bool,
80
- }
81
-
82
- impl Pacer {
83
- /// Create a new [`Pacer`] with and underlying [`Congestion`]
84
- /// implementation, and an optional throttling as specified by
85
- /// `max_pacing_rate`.
86
- pub(crate) fn new(
87
- enabled: bool, congestion: BBRv2, max_pacing_rate: Option<Bandwidth>,
88
- ) -> Self {
89
- Pacer {
90
- enabled,
91
- sender: congestion,
92
- max_pacing_rate,
93
- burst_tokens: INITIAL_UNPACED_BURST,
94
- ideal_next_packet_send_time: ReleaseTime::Immediate,
95
- initial_burst_size: INITIAL_UNPACED_BURST,
96
- lumpy_tokens: 0,
97
- pacing_limited: false,
98
- }
99
- }
100
-
101
- pub fn get_next_release_time(&self) -> ReleaseDecision {
102
- if !self.enabled {
103
- return ReleaseDecision {
104
- time: ReleaseTime::Immediate,
105
- allow_burst: true,
106
- };
107
- }
108
-
109
- let allow_burst = self.burst_tokens > 0 || self.lumpy_tokens > 0;
110
- ReleaseDecision {
111
- time: self.ideal_next_packet_send_time,
112
- allow_burst,
113
- }
114
- }
115
-
116
- #[cfg(feature = "qlog")]
117
- pub fn state_str(&self) -> &'static str {
118
- self.sender.state_str()
119
- }
120
-
121
- pub fn get_congestion_window(&self) -> usize {
122
- self.sender.get_congestion_window()
123
- }
124
-
125
- pub fn on_packet_sent(
126
- &mut self, sent_time: Instant, bytes_in_flight: usize,
127
- packet_number: u64, bytes: usize, is_retransmissible: bool,
128
- rtt_stats: &RttStats,
129
- ) {
130
- self.sender.on_packet_sent(
131
- sent_time,
132
- bytes_in_flight,
133
- packet_number,
134
- bytes,
135
- is_retransmissible,
136
- );
137
-
138
- if !self.enabled || !is_retransmissible {
139
- return;
140
- }
141
-
142
- // If in recovery, the connection is not coming out of quiescence.
143
- if bytes_in_flight == 0 && !self.sender.is_in_recovery() {
144
- // Add more burst tokens anytime the connection is leaving quiescence,
145
- // but limit it to the equivalent of a single bulk write,
146
- // not exceeding the current CWND in packets.
147
- self.burst_tokens = self
148
- .initial_burst_size
149
- .min(self.sender.get_congestion_window_in_packets());
150
- }
151
-
152
- if self.burst_tokens > 0 {
153
- self.burst_tokens -= 1;
154
- self.ideal_next_packet_send_time = ReleaseTime::Immediate;
155
- self.pacing_limited = false;
156
- return;
157
- }
158
-
159
- // The next packet should be sent as soon as the current packet has been
160
- // transferred. PacingRate is based on bytes in flight including this
161
- // packet.
162
- let delay = self
163
- .pacing_rate(bytes_in_flight + bytes, rtt_stats)
164
- .transfer_time(bytes as u64);
165
-
166
- if !self.pacing_limited || self.lumpy_tokens == 0 {
167
- // Reset lumpy_tokens_ if either application or cwnd throttles sending
168
- // or token runs out.
169
- self.lumpy_tokens = 1.max(LUMPY_PACING_SIZE.min(
170
- (self.sender.get_congestion_window_in_packets() as f64 *
171
- LUMPY_PACING_CWND_FRACTION) as usize,
172
- ));
173
-
174
- if self.sender.bandwidth_estimate(rtt_stats) <
175
- LUMPY_PACING_MIN_BANDWIDTH_KBPS
176
- {
177
- // Below 1.2Mbps, send 1 packet at once, because one full-sized
178
- // packet is about 10ms of queueing.
179
- self.lumpy_tokens = 1;
180
- }
181
-
182
- if bytes_in_flight + bytes >= self.sender.get_congestion_window() {
183
- // Don't add lumpy_tokens if the congestion controller is CWND
184
- // limited.
185
- self.lumpy_tokens = 1;
186
- }
187
- }
188
-
189
- self.lumpy_tokens -= 1;
190
- self.ideal_next_packet_send_time.set_max(sent_time);
191
- self.ideal_next_packet_send_time.inc(delay);
192
- // Stop making up for lost time if underlying sender prevents sending.
193
- self.pacing_limited = self.sender.can_send(bytes_in_flight + bytes);
194
- }
195
-
196
- #[allow(clippy::too_many_arguments)]
197
- #[inline]
198
- pub fn on_congestion_event(
199
- &mut self, rtt_updated: bool, prior_in_flight: usize,
200
- bytes_in_flight: usize, event_time: Instant, acked_packets: &[Acked],
201
- lost_packets: &[Lost], least_unacked: u64, rtt_stats: &RttStats,
202
- recovery_stats: &mut RecoveryStats,
203
- ) {
204
- self.sender.on_congestion_event(
205
- rtt_updated,
206
- prior_in_flight,
207
- bytes_in_flight,
208
- event_time,
209
- acked_packets,
210
- lost_packets,
211
- least_unacked,
212
- rtt_stats,
213
- recovery_stats,
214
- );
215
-
216
- if !self.enabled {
217
- return;
218
- }
219
-
220
- if !lost_packets.is_empty() {
221
- // Clear any burst tokens when entering recovery.
222
- self.burst_tokens = 0;
223
- }
224
-
225
- if let Some(max_pacing_rate) = self.max_pacing_rate {
226
- if rtt_updated {
227
- let max_rate = max_pacing_rate * 1.25f32;
228
- let max_cwnd =
229
- max_rate.to_bytes_per_period(rtt_stats.smoothed_rtt);
230
- self.sender.limit_cwnd(max_cwnd as usize);
231
- }
232
- }
233
- }
234
-
235
- pub fn on_packet_neutered(&mut self, packet_number: u64) {
236
- self.sender.on_packet_neutered(packet_number);
237
- }
238
-
239
- pub fn on_retransmission_timeout(&mut self, packets_retransmitted: bool) {
240
- self.sender.on_retransmission_timeout(packets_retransmitted)
241
- }
242
-
243
- pub fn pacing_rate(
244
- &self, bytes_in_flight: usize, rtt_stats: &RttStats,
245
- ) -> Bandwidth {
246
- let sender_rate = self.sender.pacing_rate(bytes_in_flight, rtt_stats);
247
- match self.max_pacing_rate {
248
- Some(rate) if self.enabled => rate.min(sender_rate),
249
- _ => sender_rate,
250
- }
251
- }
252
-
253
- pub fn bandwidth_estimate(&self, rtt_stats: &RttStats) -> Bandwidth {
254
- self.sender.bandwidth_estimate(rtt_stats)
255
- }
256
-
257
- pub fn max_bandwidth(&self) -> Bandwidth {
258
- self.sender.max_bandwidth()
259
- }
260
-
261
- #[cfg(feature = "qlog")]
262
- pub fn send_rate(&self) -> Option<Bandwidth> {
263
- self.sender.send_rate()
264
- }
265
-
266
- #[cfg(feature = "qlog")]
267
- pub fn ack_rate(&self) -> Option<Bandwidth> {
268
- self.sender.ack_rate()
269
- }
270
-
271
- pub fn on_app_limited(&mut self, bytes_in_flight: usize) {
272
- self.pacing_limited = false;
273
- self.sender.on_app_limited(bytes_in_flight);
274
- }
275
-
276
- pub fn update_mss(&mut self, new_mss: usize) {
277
- self.sender.update_mss(new_mss)
278
- }
279
-
280
- #[cfg(feature = "qlog")]
281
- pub fn ssthresh(&self) -> Option<u64> {
282
- self.sender.ssthresh()
283
- }
284
-
285
- #[cfg(test)]
286
- pub fn is_app_limited(&self, bytes_in_flight: usize) -> bool {
287
- !self.is_cwnd_limited(bytes_in_flight)
288
- }
289
-
290
- #[cfg(test)]
291
- fn is_cwnd_limited(&self, bytes_in_flight: usize) -> bool {
292
- !self.pacing_limited && self.sender.is_cwnd_limited(bytes_in_flight)
293
- }
294
- }
1
+ // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style license that can be
3
+ // found in the LICENSE file.
4
+
5
+ // Copyright (C) 2023, Cloudflare, Inc.
6
+ // All rights reserved.
7
+ //
8
+ // Redistribution and use in source and binary forms, with or without
9
+ // modification, are permitted provided that the following conditions are
10
+ // met:
11
+ //
12
+ // * Redistributions of source code must retain the above copyright notice,
13
+ // this list of conditions and the following disclaimer.
14
+ //
15
+ // * Redistributions in binary form must reproduce the above copyright
16
+ // notice, this list of conditions and the following disclaimer in the
17
+ // documentation and/or other materials provided with the distribution.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
+ // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
+ // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ use std::time::Instant;
32
+
33
+ use crate::recovery::gcongestion::bbr2::BBRv2;
34
+ use crate::recovery::gcongestion::Bandwidth;
35
+ use crate::recovery::gcongestion::CongestionControl;
36
+ use crate::recovery::rtt::RttStats;
37
+ use crate::recovery::RecoveryStats;
38
+ use crate::recovery::ReleaseDecision;
39
+ use crate::recovery::ReleaseTime;
40
+
41
+ use super::Acked;
42
+ use super::Lost;
43
+
44
+ /// Congestion window fraction that the pacing sender allows in bursts during
45
+ /// pacing.
46
+ const LUMPY_PACING_CWND_FRACTION: f64 = 0.25;
47
+
48
+ /// Number of packets that the pacing sender allows in bursts during pacing.
49
+ /// This is ignored if a flow's estimated bandwidth is lower than 1200 kbps.
50
+ const LUMPY_PACING_SIZE: usize = 2;
51
+
52
+ /// The minimum estimated client bandwidth below which the pacing sender will
53
+ /// not allow bursts.
54
+ const LUMPY_PACING_MIN_BANDWIDTH_KBPS: Bandwidth =
55
+ Bandwidth::from_kbits_per_second(1_200);
56
+
57
+ /// Configured maximum size of the burst coming out of quiescence. The burst is
58
+ /// never larger than the current CWND in packets.
59
+ const INITIAL_UNPACED_BURST: usize = 10;
60
+
61
+ #[derive(Debug)]
62
+ pub struct Pacer {
63
+ /// Should this [`Pacer`] be making any release decisions?
64
+ enabled: bool,
65
+ /// Underlying sender
66
+ sender: BBRv2,
67
+ /// The maximum rate the [`Pacer`] will use.
68
+ max_pacing_rate: Option<Bandwidth>,
69
+ /// Number of unpaced packets to be sent before packets are delayed.
70
+ burst_tokens: usize,
71
+ /// When can the next packet be sent.
72
+ ideal_next_packet_send_time: ReleaseTime,
73
+ initial_burst_size: usize,
74
+ /// Number of unpaced packets to be sent before packets are delayed. This
75
+ /// token is consumed after [`Self::burst_tokens`] ran out.
76
+ lumpy_tokens: usize,
77
+ /// Indicates whether pacing throttles the sending. If true, make up for
78
+ /// lost time.
79
+ pacing_limited: bool,
80
+ }
81
+
82
+ impl Pacer {
83
+ /// Create a new [`Pacer`] with and underlying [`Congestion`]
84
+ /// implementation, and an optional throttling as specified by
85
+ /// `max_pacing_rate`.
86
+ pub(crate) fn new(
87
+ enabled: bool, congestion: BBRv2, max_pacing_rate: Option<Bandwidth>,
88
+ ) -> Self {
89
+ Pacer {
90
+ enabled,
91
+ sender: congestion,
92
+ max_pacing_rate,
93
+ burst_tokens: INITIAL_UNPACED_BURST,
94
+ ideal_next_packet_send_time: ReleaseTime::Immediate,
95
+ initial_burst_size: INITIAL_UNPACED_BURST,
96
+ lumpy_tokens: 0,
97
+ pacing_limited: false,
98
+ }
99
+ }
100
+
101
+ pub fn get_next_release_time(&self) -> ReleaseDecision {
102
+ if !self.enabled {
103
+ return ReleaseDecision {
104
+ time: ReleaseTime::Immediate,
105
+ allow_burst: true,
106
+ };
107
+ }
108
+
109
+ let allow_burst = self.burst_tokens > 0 || self.lumpy_tokens > 0;
110
+ ReleaseDecision {
111
+ time: self.ideal_next_packet_send_time,
112
+ allow_burst,
113
+ }
114
+ }
115
+
116
+ #[cfg(feature = "qlog")]
117
+ pub fn state_str(&self) -> &'static str {
118
+ self.sender.state_str()
119
+ }
120
+
121
+ pub fn get_congestion_window(&self) -> usize {
122
+ self.sender.get_congestion_window()
123
+ }
124
+
125
+ pub fn on_packet_sent(
126
+ &mut self, sent_time: Instant, bytes_in_flight: usize,
127
+ packet_number: u64, bytes: usize, is_retransmissible: bool,
128
+ rtt_stats: &RttStats,
129
+ ) {
130
+ self.sender.on_packet_sent(
131
+ sent_time,
132
+ bytes_in_flight,
133
+ packet_number,
134
+ bytes,
135
+ is_retransmissible,
136
+ );
137
+
138
+ if !self.enabled || !is_retransmissible {
139
+ return;
140
+ }
141
+
142
+ // If in recovery, the connection is not coming out of quiescence.
143
+ if bytes_in_flight == 0 && !self.sender.is_in_recovery() {
144
+ // Add more burst tokens anytime the connection is leaving quiescence,
145
+ // but limit it to the equivalent of a single bulk write,
146
+ // not exceeding the current CWND in packets.
147
+ self.burst_tokens = self
148
+ .initial_burst_size
149
+ .min(self.sender.get_congestion_window_in_packets());
150
+ }
151
+
152
+ if self.burst_tokens > 0 {
153
+ self.burst_tokens -= 1;
154
+ self.ideal_next_packet_send_time = ReleaseTime::Immediate;
155
+ self.pacing_limited = false;
156
+ return;
157
+ }
158
+
159
+ // The next packet should be sent as soon as the current packet has been
160
+ // transferred. PacingRate is based on bytes in flight including this
161
+ // packet.
162
+ let delay = self
163
+ .pacing_rate(bytes_in_flight + bytes, rtt_stats)
164
+ .transfer_time(bytes as u64);
165
+
166
+ if !self.pacing_limited || self.lumpy_tokens == 0 {
167
+ // Reset lumpy_tokens_ if either application or cwnd throttles sending
168
+ // or token runs out.
169
+ self.lumpy_tokens = 1.max(LUMPY_PACING_SIZE.min(
170
+ (self.sender.get_congestion_window_in_packets() as f64 *
171
+ LUMPY_PACING_CWND_FRACTION) as usize,
172
+ ));
173
+
174
+ if self.sender.bandwidth_estimate(rtt_stats) <
175
+ LUMPY_PACING_MIN_BANDWIDTH_KBPS
176
+ {
177
+ // Below 1.2Mbps, send 1 packet at once, because one full-sized
178
+ // packet is about 10ms of queueing.
179
+ self.lumpy_tokens = 1;
180
+ }
181
+
182
+ if bytes_in_flight + bytes >= self.sender.get_congestion_window() {
183
+ // Don't add lumpy_tokens if the congestion controller is CWND
184
+ // limited.
185
+ self.lumpy_tokens = 1;
186
+ }
187
+ }
188
+
189
+ self.lumpy_tokens -= 1;
190
+ self.ideal_next_packet_send_time.set_max(sent_time);
191
+ self.ideal_next_packet_send_time.inc(delay);
192
+ // Stop making up for lost time if underlying sender prevents sending.
193
+ self.pacing_limited = self.sender.can_send(bytes_in_flight + bytes);
194
+ }
195
+
196
+ #[allow(clippy::too_many_arguments)]
197
+ #[inline]
198
+ pub fn on_congestion_event(
199
+ &mut self, rtt_updated: bool, prior_in_flight: usize,
200
+ bytes_in_flight: usize, event_time: Instant, acked_packets: &[Acked],
201
+ lost_packets: &[Lost], least_unacked: u64, rtt_stats: &RttStats,
202
+ recovery_stats: &mut RecoveryStats,
203
+ ) {
204
+ self.sender.on_congestion_event(
205
+ rtt_updated,
206
+ prior_in_flight,
207
+ bytes_in_flight,
208
+ event_time,
209
+ acked_packets,
210
+ lost_packets,
211
+ least_unacked,
212
+ rtt_stats,
213
+ recovery_stats,
214
+ );
215
+
216
+ if !self.enabled {
217
+ return;
218
+ }
219
+
220
+ if !lost_packets.is_empty() {
221
+ // Clear any burst tokens when entering recovery.
222
+ self.burst_tokens = 0;
223
+ }
224
+
225
+ if let Some(max_pacing_rate) = self.max_pacing_rate {
226
+ if rtt_updated {
227
+ let max_rate = max_pacing_rate * 1.25f32;
228
+ let max_cwnd =
229
+ max_rate.to_bytes_per_period(rtt_stats.smoothed_rtt);
230
+ self.sender.limit_cwnd(max_cwnd as usize);
231
+ }
232
+ }
233
+ }
234
+
235
+ pub fn on_packet_neutered(&mut self, packet_number: u64) {
236
+ self.sender.on_packet_neutered(packet_number);
237
+ }
238
+
239
+ pub fn on_retransmission_timeout(&mut self, packets_retransmitted: bool) {
240
+ self.sender.on_retransmission_timeout(packets_retransmitted)
241
+ }
242
+
243
+ pub fn pacing_rate(
244
+ &self, bytes_in_flight: usize, rtt_stats: &RttStats,
245
+ ) -> Bandwidth {
246
+ let sender_rate = self.sender.pacing_rate(bytes_in_flight, rtt_stats);
247
+ match self.max_pacing_rate {
248
+ Some(rate) if self.enabled => rate.min(sender_rate),
249
+ _ => sender_rate,
250
+ }
251
+ }
252
+
253
+ pub fn bandwidth_estimate(&self, rtt_stats: &RttStats) -> Bandwidth {
254
+ self.sender.bandwidth_estimate(rtt_stats)
255
+ }
256
+
257
+ pub fn max_bandwidth(&self) -> Bandwidth {
258
+ self.sender.max_bandwidth()
259
+ }
260
+
261
+ #[cfg(feature = "qlog")]
262
+ pub fn send_rate(&self) -> Option<Bandwidth> {
263
+ self.sender.send_rate()
264
+ }
265
+
266
+ #[cfg(feature = "qlog")]
267
+ pub fn ack_rate(&self) -> Option<Bandwidth> {
268
+ self.sender.ack_rate()
269
+ }
270
+
271
+ pub fn on_app_limited(&mut self, bytes_in_flight: usize) {
272
+ self.pacing_limited = false;
273
+ self.sender.on_app_limited(bytes_in_flight);
274
+ }
275
+
276
+ pub fn update_mss(&mut self, new_mss: usize) {
277
+ self.sender.update_mss(new_mss)
278
+ }
279
+
280
+ #[cfg(feature = "qlog")]
281
+ pub fn ssthresh(&self) -> Option<u64> {
282
+ self.sender.ssthresh()
283
+ }
284
+
285
+ #[cfg(test)]
286
+ pub fn is_app_limited(&self, bytes_in_flight: usize) -> bool {
287
+ !self.is_cwnd_limited(bytes_in_flight)
288
+ }
289
+
290
+ #[cfg(test)]
291
+ fn is_cwnd_limited(&self, bytes_in_flight: usize) -> bool {
292
+ !self.pacing_limited && self.sender.is_cwnd_limited(bytes_in_flight)
293
+ }
294
+ }