diff --git a/src/traft/node.rs b/src/traft/node.rs index 8543e87e5dc6d094ddefd2f3eb83da26a9d67f38..d346c8553fcf42e4d899eae7471965789a1b3f2e 100644 --- a/src/traft/node.rs +++ b/src/traft/node.rs @@ -32,6 +32,7 @@ use crate::traft::RaftSpaceAccess; use crate::traft::RaftTerm; use crate::traft::Topology; use crate::unwrap_some_or; +use crate::util::instant_saturating_add; use crate::warn_or_panic; use ::raft::prelude as raft; use ::raft::Error as RaftError; @@ -369,7 +370,7 @@ impl NodeImpl { let pool = ConnectionPool::builder(storage.clone()) .handler_name(stringify_cfunc!(proc_raft_interact)) - .call_timeout(MainLoop::TICK * 4) + .call_timeout(MainLoop::TICK.saturating_mul(4)) .build(); let cfg = raft::Config { @@ -999,7 +1000,7 @@ impl MainLoop { let now = Instant::now(); if now > state.next_tick { - state.next_tick = now + Self::TICK; + state.next_tick = instant_saturating_add(now, Self::TICK); node_impl.raw_node.tick(); } diff --git a/src/util.rs b/src/util.rs index cf42087f035e1f62eaa221599ea3ff64418d382d..486b059cc205e1640532de3bc5d556c5b96be305 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,6 +3,14 @@ pub use Either::{Left, Right}; use crate::traft::error::Error; use std::any::{Any, TypeId}; +use std::time::{Duration, Instant}; + +const INFINITY: Duration = Duration::from_secs(30 * 365 * 24 * 60 * 60); + +pub fn instant_saturating_add(t: Instant, d: Duration) -> Instant { + t.checked_add(d) + .unwrap_or_else(|| t.checked_add(INFINITY).expect("that's too much, man")) +} //////////////////////////////////////////////////////////////////////////////// /// A generic enum that contains exactly one of two possible types. Equivalent