Make ping estimation less chatty

This commit is contained in:
Roderick van Domburg 2021-12-19 00:11:16 +01:00
parent 0d51fd43dc
commit a297c68913
No known key found for this signature in database
GPG key ID: A9EF5222A26F0451

View file

@ -280,6 +280,8 @@ impl AudioFileFetch {
fn handle_file_data(&mut self, data: ReceivedData) -> Result<ControlFlow, AudioFileError> { fn handle_file_data(&mut self, data: ReceivedData) -> Result<ControlFlow, AudioFileError> {
match data { match data {
ReceivedData::ResponseTime(response_time) => { ReceivedData::ResponseTime(response_time) => {
let old_ping_time_ms = self.shared.ping_time_ms.load(Ordering::Relaxed);
// prune old response times. Keep at most two so we can push a third. // prune old response times. Keep at most two so we can push a third.
while self.network_response_times.len() >= 3 { while self.network_response_times.len() >= 3 {
self.network_response_times.remove(0); self.network_response_times.remove(0);
@ -289,7 +291,8 @@ impl AudioFileFetch {
self.network_response_times.push(response_time); self.network_response_times.push(response_time);
// stats::median is experimental. So we calculate the median of up to three ourselves. // stats::median is experimental. So we calculate the median of up to three ourselves.
let ping_time = match self.network_response_times.len() { let ping_time_ms = {
let response_time = match self.network_response_times.len() {
1 => self.network_response_times[0], 1 => self.network_response_times[0],
2 => (self.network_response_times[0] + self.network_response_times[1]) / 2, 2 => (self.network_response_times[0] + self.network_response_times[1]) / 2,
3 => { 3 => {
@ -299,13 +302,21 @@ impl AudioFileFetch {
} }
_ => unreachable!(), _ => unreachable!(),
}; };
response_time.as_millis() as usize
};
trace!("Ping time estimated as: {} ms", ping_time.as_millis()); // print when the new estimate deviates by more than 10% from the last
if f32::abs(
(ping_time_ms as f32 - old_ping_time_ms as f32) / old_ping_time_ms as f32,
) > 0.1
{
debug!("Ping time now estimated as: {} ms", ping_time_ms);
}
// store our new estimate for everyone to see // store our new estimate for everyone to see
self.shared self.shared
.ping_time_ms .ping_time_ms
.store(ping_time.as_millis() as usize, Ordering::Relaxed); .store(ping_time_ms, Ordering::Relaxed);
} }
ReceivedData::Data(data) => { ReceivedData::Data(data) => {
match self.output.as_mut() { match self.output.as_mut() {