-
Notifications
You must be signed in to change notification settings - Fork 59
/
resender.rs
393 lines (298 loc) Β· 13 KB
/
resender.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use super::{SendEvent, SequenceNumber, UdpConnectionVars, UdpPacket};
use log::*;
use std::sync::{Arc, Mutex};
use std::time::Duration;
impl UdpConnectionVars {
pub(super) fn update_peer_ack_number(&mut self, ack_number: SequenceNumber) {
if ack_number > self.peer_ack_number {
self.peer_ack_number = ack_number;
self.clear_acknowledged_packets();
self.wake_pending_send_events();
}
}
fn clear_acknowledged_packets(&mut self) {
let peer_ack_number = self.peer_ack_number;
self.sent_packets
.retain(|sequence_number, _| *sequence_number > peer_ack_number);
}
fn normalised_rtt_estimate(&self) -> Duration {
std::cmp::max(Duration::from_millis(100), self.rtt_estimate)
}
fn update_resending_packet(&self, packet: &mut UdpPacket) {
// Ensure that the packet contains the latest
// ack and window information before resending
packet.ack_number = self.ack_number;
packet.window = self.calculate_recv_window();
packet.checksum = packet.calculate_checksum();
}
}
pub(super) fn schedule_resend_if_dropped(
con: Arc<Mutex<UdpConnectionVars>>,
sent_packet: UdpPacket,
) {
let packet_key = sent_packet.sequence_number;
let resend_count = sent_packet.resend_count;
let (rtt_estimate, position_delay) = {
let mut con = con.lock().unwrap();
con.sent_packets.insert(packet_key, sent_packet);
let position_delay = Duration::from_millis(con.sent_packets.len() as u64);
(con.normalised_rtt_estimate(), position_delay)
};
tokio::spawn(async move {
// It's important we only scope the lock of the connection to this block.
// Since we may need to send an event to the orchestration loop which could attempt
// to lock the connection for other purposes, this avoids a potential deadlock.
let (packet, event_sender) = loop {
let resend_delay = (rtt_estimate * 2 * (resend_count + 1) as u32) + position_delay;
tokio::time::delay_for(resend_delay).await;
if con.is_poisoned() {
warn!("cannot resend packet: lock poisoned");
return;
}
let mut con = loop {
match con.try_lock() {
Ok(con) => break con,
Err(_) => {}
};
tokio::time::delay_for(position_delay).await;
};
if !con.is_connected() {
return;
}
if !con.sent_packets.contains_key(&packet_key) {
return;
}
let is_first_dropped = con.peer_ack_number + SequenceNumber(1) == packet_key;
if is_first_dropped {
let sent_packet = con.sent_packets.remove(&packet_key);
break (sent_packet, con.event_sender());
}
};
if let Some(mut packet) = packet {
// Packet has still not been acknowledged after 2*RTT
// likely that is was dropped so we resend it
packet.resend_count += 1;
{
let mut con = con.lock().unwrap();
if packet.resend_count >= con.config().packet_resend_limit() {
warn!(
"packet [{}, {}] was resent too many times ({}), closing connection.",
packet.sequence_number,
packet.end_sequence_number(),
resend_count
);
con.event_sender()
.send(SendEvent::Close)
.unwrap_or_else(|err| warn!("failed to send close message: {}", err));
return;
}
con.update_resending_packet(&mut packet);
con.decrease_transit_window_after_drop();
warn!(
"resending dropped packet: [{}, {}] (peer ack: {})",
packet.sequence_number,
packet.end_sequence_number(),
con.peer_ack_number
);
}
let result = event_sender.send(SendEvent::Resend(packet));
if result.is_err() {
warn!("failed to resend packet: {:?}", result);
}
}
});
}
#[cfg(test)]
mod tests {
use super::super::{SequenceNumber, UdpConnectionConfig, UdpConnectionState};
use super::*;
use tokio::runtime::Runtime;
#[test]
fn test_update_peer_ack_number_old_packet() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.peer_ack_number = SequenceNumber(50);
con.update_peer_ack_number(SequenceNumber(40));
assert_eq!(con.peer_ack_number, SequenceNumber(50));
}
#[test]
fn test_update_peer_ack_number_new_packet() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.peer_ack_number = SequenceNumber(50);
con.update_peer_ack_number(SequenceNumber(100));
assert_eq!(con.peer_ack_number, SequenceNumber(100));
}
#[test]
fn test_update_peer_ack_number_new_packet_with_wrapping() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.state = UdpConnectionState::Connected;
con.peer_ack_number = SequenceNumber(u32::MAX);
con.update_peer_ack_number(SequenceNumber(50));
assert_eq!(con.peer_ack_number, SequenceNumber(50));
}
#[test]
fn test_update_peer_ack_number_removes_acknowledged_packets() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.state = UdpConnectionState::Connected;
con.sent_packets.insert(
SequenceNumber(10),
UdpPacket::data(SequenceNumber(10), SequenceNumber(0), 0, &[]),
);
con.sent_packets.insert(
SequenceNumber(30),
UdpPacket::data(SequenceNumber(30), SequenceNumber(0), 0, &[]),
);
con.peer_ack_number = SequenceNumber(0);
con.update_peer_ack_number(SequenceNumber(20));
assert_eq!(con.peer_ack_number, SequenceNumber(20));
assert_eq!(con.sent_packets.contains_key(&SequenceNumber(10)), false);
assert_eq!(con.sent_packets.contains_key(&SequenceNumber(30)), true);
}
#[test]
fn test_normalised_rtt_estimate() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.rtt_estimate = Duration::from_millis(200);
assert_eq!(con.normalised_rtt_estimate(), Duration::from_millis(200));
con.rtt_estimate = Duration::from_millis(50);
assert_eq!(con.normalised_rtt_estimate(), Duration::from_millis(100));
}
#[test]
fn test_update_resending_packet() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default().with_recv_window(1000));
con.ack_number = SequenceNumber(100);
let mut packet = UdpPacket::data(SequenceNumber(0), SequenceNumber(0), 0, &[1]);
con.update_resending_packet(&mut packet);
assert_eq!(
packet,
UdpPacket::data(SequenceNumber(0), SequenceNumber(100), 1000, &[1])
);
}
#[test]
fn test_schedule_resend_does_not_resend_if_packet_is_acknowledged() {
Runtime::new().unwrap().block_on(async {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.state = UdpConnectionState::Connected;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
con.event_sender = Some(tx);
con.rtt_estimate = Duration::from_millis(10);
let con = Arc::from(Mutex::from(con));
let sent_packet = UdpPacket::data(SequenceNumber(10), SequenceNumber(0), 0, &[]);
schedule_resend_if_dropped(Arc::clone(&con), sent_packet.clone());
// Should store packet in sent_packets map
{
let con = con.lock().unwrap();
assert_eq!(
con.sent_packets.get(&SequenceNumber(10)),
Some(&sent_packet)
);
}
// Acknowledge sent packet
{
let mut con = con.lock().unwrap();
con.update_peer_ack_number(SequenceNumber(10));
}
// Wait for resend task to fire
tokio::time::delay_for(Duration::from_millis(25)).await;
// Should not have removed from sent_packets
let result = rx.try_recv();
assert!(result.is_err());
});
}
#[test]
fn test_schedule_resend_does_resend_if_packet_is_not_acknowledged() {
// TODO: fix flaky test
if std::env::var("CI").is_ok() {
return;
}
Runtime::new().unwrap().block_on(async {
let mut con =
UdpConnectionVars::new(UdpConnectionConfig::default().with_recv_window(1000));
con.state = UdpConnectionState::Connected;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
con.event_sender = Some(tx);
con.rtt_estimate = Duration::from_millis(100);
let con = Arc::from(Mutex::from(con));
let sent_packet = UdpPacket::data(SequenceNumber(1), SequenceNumber(0), 1000, &[]);
schedule_resend_if_dropped(Arc::clone(&con), sent_packet.clone());
// Should store packet in sent_packets map
{
let con = con.lock().unwrap();
assert_eq!(con.sent_packets.get(&SequenceNumber(1)), Some(&sent_packet));
}
// Mock ack update to ensure that the packet is updated when reset
{
let mut con = con.lock().unwrap();
con.ack_number = SequenceNumber(50);
}
// Wait for resend task to fire
tokio::time::delay_for(Duration::from_millis(250)).await;
// Should remove packet from sent_packets map
{
let con = con.lock().unwrap();
assert_eq!(con.sent_packets.get(&SequenceNumber(1)), None);
}
// Should send resend event with updated packet
let result = rx.try_recv();
let mut expected_packet =
UdpPacket::data(SequenceNumber(1), SequenceNumber(50), 1000, &[]);
expected_packet.resend_count = 1;
assert_eq!(
result.expect("should have sent Resend event"),
SendEvent::Resend(expected_packet)
);
});
}
#[test]
fn test_schedule_resend_does_resend_if_packet_connection_is_dropped() {
Runtime::new().unwrap().block_on(async {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.state = UdpConnectionState::Connected;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
con.event_sender = Some(tx);
con.rtt_estimate = Duration::from_millis(100);
let con = Arc::from(Mutex::from(con));
let sent_packet = UdpPacket::data(SequenceNumber(10), SequenceNumber(0), 0, &[]);
schedule_resend_if_dropped(Arc::clone(&con), sent_packet.clone());
// Should store packet in sent_packets map
{
let mut con = con.lock().unwrap();
con.state = UdpConnectionState::Disconnected;
}
// Wait for resend task to fire
tokio::time::delay_for(Duration::from_millis(250)).await;
// Should not remove packet from sent_packets map
{
let con = con.lock().unwrap();
assert_eq!(
con.sent_packets.get(&SequenceNumber(10)),
Some(&sent_packet)
);
}
// Should not send resend event
let result = rx.try_recv();
assert_eq!(result.is_err(), true);
});
}
#[test]
fn test_schedule_resend_closes_connection_if_exceeds_resend_limit() {
Runtime::new().unwrap().block_on(async {
let mut con =
UdpConnectionVars::new(UdpConnectionConfig::default().with_packet_resend_limit(3));
con.state = UdpConnectionState::Connected;
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
con.event_sender = Some(tx);
con.rtt_estimate = Duration::from_millis(100);
let con = Arc::from(Mutex::from(con));
let mut sent_packet = UdpPacket::data(SequenceNumber(1), SequenceNumber(0), 0, &[]);
sent_packet.resend_count = 3;
schedule_resend_if_dropped(Arc::clone(&con), sent_packet.clone());
// Wait for resend task to fire
tokio::time::delay_for(Duration::from_millis(1010)).await;
// Should send close event
let result = rx.try_recv();
match result {
Ok(SendEvent::Close) => {}
result @ _ => panic!("resend should have triggered close event, got {:?}", result),
}
});
}
}