Skip to content

Commit

Permalink
Fix RTX history not updating resulting in stale RTX ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlapa committed Sep 25, 2024
1 parent 0740273 commit 46a96e3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Ensure compatibility with some 32-bit targets #533
* Fix bug using unreliable channels by default #548
* New add_channel_with_config() for configured data channels #548
* Fix RTX stops working after packet loss spike #566

# 0.6.1
* Force openssl to be >=0.10.66 #545
Expand Down
4 changes: 2 additions & 2 deletions src/streams/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ impl StreamTx {
}

// bytes stats refer to the last second by default
let bytes_transmitted = self.stats.bytes_transmitted.sum();
let bytes_retransmitted = self.stats.bytes_retransmitted.sum();
let bytes_transmitted = self.stats.bytes_transmitted.sum(now);
let bytes_retransmitted = self.stats.bytes_retransmitted.sum(now);
let ratio = bytes_retransmitted as f32 / (bytes_retransmitted + bytes_transmitted) as f32;
let ratio = if ratio.is_finite() { ratio } else { 0_f32 };
self.rtx_ratio = (ratio, now);
Expand Down
31 changes: 23 additions & 8 deletions src/util/value_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ where
pub fn push(&mut self, t: Instant, v: T) {
self.value += v;
self.history.push_back((t, v));
self.drain(t);
}

/// Returns the sum of all values in the history up to max_time
/// This is more efficient than sum_since() as it does not need to iterate over the history
pub fn sum(&self) -> T {
/// Returns the sum of all values in the history up to now - max_time.
pub fn sum(&mut self, now: Instant) -> T {
self.drain(now);

self.value
}

Expand All @@ -63,7 +63,7 @@ mod test {
use super::ValueHistory;

#[test]
fn test() {
fn with_value_test() {
let now = Instant::now();

let mut h = ValueHistory {
Expand All @@ -72,11 +72,26 @@ mod test {
..Default::default()
};

assert_eq!(h.sum(), 11);
assert_eq!(h.sum(now), h.value);
h.push(now - Duration::from_millis(1500), 22);
h.push(now - Duration::from_millis(500), 22);
assert_eq!(h.sum(), 55);
assert_eq!(h.sum(now), h.value + 22);
h.push(now, 0);
assert_eq!(h.sum(), 33);
assert_eq!(h.sum(now), h.value + 22);
}

#[test]
fn test() {
let now = Instant::now();
let mut h = ValueHistory::default();

assert_eq!(h.sum(now), 0);
h.push(now - Duration::from_millis(1500), 22);
assert_eq!(h.sum(now), 0);
h.push(now - Duration::from_millis(700), 22);
h.push(now - Duration::from_millis(500), 33);
assert_eq!(h.sum(now), 22 + 33);
assert_eq!(h.sum(now + Duration::from_millis(400)), 33);
assert_eq!(h.sum(now + Duration::from_millis(600)), 0);
}
}

0 comments on commit 46a96e3

Please sign in to comment.