Skip to main content

next_backoff

Function next_backoff 

Source
pub fn next_backoff(
    previous: Duration,
    connected: bool,
    max: Duration,
) -> Duration
Expand description

Returns the delay to wait before the next connection attempt.

The delay starts at INITIAL_BACKOFF and doubles with every attempt that never reached Event::Connected, saturating at max. previous is the delay used before the failed attempt, or Duration::ZERO for the first one. A connected session resets the delay: the address and the handshake worked, so a later failure is retried without a growing penalty.

The crate keeps the sequence across the attempts it makes itself and reports each step in Event::Disconnected. A host that drives its own loop, for example because it wants to stop retrying, can use this function for the same policy.

let max = Duration::from_secs(30);
assert_eq!(next_backoff(Duration::ZERO, false, max), Duration::from_secs(1));
assert_eq!(next_backoff(Duration::from_secs(1), false, max), Duration::from_secs(2));
assert_eq!(next_backoff(Duration::from_secs(16), true, max), Duration::from_secs(1));