4 releases (breaking)

0.4.0 Mar 2, 2024
0.3.0 Dec 9, 2020
0.2.0 Dec 8, 2020
0.1.0 Dec 7, 2020

#909 in Algorithms

Download history 2/week @ 2024-01-29 1/week @ 2024-02-05 179/week @ 2024-02-26 76/week @ 2024-03-04 65/week @ 2024-03-11 58/week @ 2024-04-01

139 downloads per month
Used in 2 crates

MIT license

40KB
105 lines

phi-detector

Crates.io documentation MIT licensed

This is a Rust implemention of Phi Accrual Failure Detector algorithm which is clearly described in the figure below. Using this algorithm you can detect remote server failure.

Algorithm


lib.rs:

This is an implementation of Phi Accrual Failure Detector.

To reduce the memory footprint, pings or intervals aren't actually stored but only two values to calculate normal distribution are maintained. This not only reduces the memory footprint to the constant value but also the computational cost for each ping down to constant.

Why does the memory footprint matter? Think about your application communicates with thousand of remote servers and you want to maintain failure detector for each server. Apparently, it is too expensive to cost 100MB for a failure detector per server.

use phi_detector::PingWindow;
use std::time::Duration;

// Create a window with a interval 100ms.
let mut window = PingWindow::new(Duration::from_millis(100));

window.add_ping(Duration::from_millis(150));
window.add_ping(Duration::from_millis(80));
// Now the window has intervals [100ms, 150ms, 80ms]. Average is 110ms.

let normal_dist = window.normal_dist();
// If the server is down for 5s, it should be failure.
let phi = normal_dist.phi(Duration::from_millis(5000));
if phi > 12. {
    panic!("The server is down");
}

No runtime deps