use std::time::Duration;
use log::{error, info, warn};
use crate::messages::Notice;
use crate::subscriptions::common::RoutedItem;
pub(crate) fn log_unrouted_notice(notice: &Notice) {
if notice.is_warning() {
warn!("warning: {notice}");
} else {
error!("error: {notice}");
}
}
pub(crate) fn log_orphan(request_id: i32, item: &RoutedItem) {
match item {
RoutedItem::Notice(n) => info!("no recipient for notice (id={request_id}): {n}"),
RoutedItem::Error(e) => info!("no recipient for error (id={request_id}): {e}"),
RoutedItem::Response(_) => {}
}
}
pub(crate) const MAX_RECONNECT_ATTEMPTS: i32 = 20;
pub(crate) struct FibonacciBackoff {
previous: u64,
current: u64,
max: u64,
}
impl FibonacciBackoff {
pub(crate) fn new(max: u64) -> Self {
FibonacciBackoff {
previous: 0,
current: 1,
max,
}
}
pub(crate) fn next_delay(&mut self) -> Duration {
let next = self.previous + self.current;
self.previous = self.current;
self.current = next;
if next > self.max {
Duration::from_secs(self.max)
} else {
Duration::from_secs(next)
}
}
}
#[cfg(test)]
#[path = "common_tests.rs"]
mod tests;