-
Notifications
You must be signed in to change notification settings - Fork 59
/
rtt_estimator.rs
137 lines (109 loc) Β· 4.08 KB
/
rtt_estimator.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
use super::{UdpConnectionVars, UdpPacket};
use std::time::{Duration, Instant};
impl UdpConnectionVars {
pub(super) fn store_send_time_of_packet(&mut self, sent_packet: &UdpPacket) {
let corresponding_ack_number = sent_packet.end_sequence_number();
self.send_times
.insert(corresponding_ack_number, Instant::now());
}
pub(super) fn adjust_rtt_estimate(&mut self, recv_packet: &UdpPacket) {
let sent_at = match self.send_times.get(&recv_packet.ack_number) {
Some(sent_at) => sent_at,
None => return,
};
let rtt_measurement = Instant::now().duration_since(*sent_at);
// The rtt estimate is kept by using a moving average.
// This is calculated using the average between the previous value and the new measurement.
if self.rtt_estimate.as_nanos() == 0 {
self.rtt_estimate = rtt_measurement;
} else {
self.rtt_estimate = Duration::from_nanos(
((self.rtt_estimate.as_nanos() + rtt_measurement.as_nanos()) / 2) as u64,
);
}
// We also clear any sent times from sent packets which may have
// not been acknowledged individually.
self.send_times.retain(|k, _| *k > recv_packet.ack_number)
}
}
#[cfg(test)]
mod tests {
use super::super::{SequenceNumber, UdpConnectionConfig};
use super::*;
#[test]
fn test_store_send_time_of_packet() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.store_send_time_of_packet(&UdpPacket::data(
SequenceNumber(50),
SequenceNumber(0),
0,
&[1],
));
assert_eq!(con.send_times.get(&SequenceNumber(51)).is_some(), true);
}
#[test]
fn test_adjust_rtt_estimate_with_packet_not_in_sent_time() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.rtt_estimate = Duration::from_millis(100);
con.adjust_rtt_estimate(&UdpPacket::data(
SequenceNumber(50),
SequenceNumber(10),
0,
&[],
));
assert_eq!(con.rtt_estimate, Duration::from_millis(100));
}
#[test]
fn test_adjust_rtt_estimate_with_sent_packet() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.rtt_estimate = Duration::from_millis(0);
con.send_times.insert(
SequenceNumber(100),
Instant::now() - Duration::from_millis(100),
);
con.adjust_rtt_estimate(&UdpPacket::data(
SequenceNumber(50),
SequenceNumber(100),
0,
&[],
));
assert_eq!((con.rtt_estimate.as_millis() as i32 - 100).abs() < 5, true);
}
#[test]
fn test_adjust_rtt_estimate_with_sent_packet_average() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.rtt_estimate = Duration::from_millis(150);
con.send_times.insert(
SequenceNumber(100),
Instant::now() - Duration::from_millis(100),
);
con.adjust_rtt_estimate(&UdpPacket::data(
SequenceNumber(50),
SequenceNumber(100),
0,
&[],
));
assert_eq!((con.rtt_estimate.as_millis() as i32 - 125).abs() < 5, true);
}
#[test]
fn test_adjust_rtt_estimate_removes_acknowledged_sent_times() {
let mut con = UdpConnectionVars::new(UdpConnectionConfig::default());
con.rtt_estimate = Duration::from_millis(150);
con.send_times.insert(SequenceNumber(50), Instant::now());
con.send_times.insert(SequenceNumber(100), Instant::now());
con.send_times.insert(SequenceNumber(150), Instant::now());
con.adjust_rtt_estimate(&UdpPacket::data(
SequenceNumber(50),
SequenceNumber(100),
0,
&[],
));
assert_eq!(
con.send_times
.keys()
.copied()
.collect::<Vec<SequenceNumber>>(),
vec![SequenceNumber(150)]
);
}
}