Skip to main content

serf_rpc/
coordinates.rs

1use std::time::Duration;
2
3use crate::protocol::Coordinate;
4
5impl Coordinate {
6    pub fn estimate_rtt(&self, other: &Coordinate) -> Duration {
7        // calculate euclidean distance
8        let dist = self
9            .vec
10            .iter()
11            .zip(other.vec.iter())
12            .map(|(l, r)| (*l - *r).powi(2))
13            .sum::<f32>()
14            .sqrt();
15
16        // add heights
17        let unadjusted = dist + self.height + other.height;
18
19        // adjust by adjustment factors
20        let adjusted = unadjusted + self.adjustment + other.adjustment;
21
22        // guard against negative adjustments
23        let rtt = if adjusted.is_sign_positive() {
24            adjusted
25        } else {
26            unadjusted
27        };
28
29        Duration::from_secs_f32(rtt)
30    }
31}