summaryrefslogtreecommitdiffstats
path: root/src/state.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-25 23:00:56 +0100
committertv <tv@krebsco.de>2026-10-25 23:00:56 +0100
commitc52d4b5ae43c202569c8801c312c2b56954ff8a0 (patch)
treedac7221b966d9f4fdb8e1b91b1e20bce607de6fc /src/state.rs
parent2f7265795b7f6ddf460bf93c5f880fb24bde9d3a (diff)
add clock module
The Clock module is the system clock that every unix timestamp new goes through. It comes with an offset that only tests can set.
Diffstat (limited to 'src/state.rs')
-rw-r--r--src/state.rs15
1 files changed, 7 insertions, 8 deletions
diff --git a/src/state.rs b/src/state.rs
index 78b058c..46ebfb9 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -676,6 +676,8 @@ pub(crate) struct AppState {
/// A catch-up source given up for not answering our sync messages, and until when it
/// isn't chosen again (`swarm::source`).
pub(crate) catch_up_avoided: Arc<std::sync::Mutex<Option<(String, Instant)>>>,
+ /// The node's wall clock (`clock`).
+ pub(crate) clock: Arc<crate::clock::Clock>,
}
/// The peer a catching-up node syncs with first (`swarm::source`).
@@ -913,7 +915,7 @@ impl AppState {
// how long the change took to reach them (`swarm::stats`).
replica.doc.commit_with(
automerge::transaction::CommitOptions::default()
- .with_time(unix_millis() as i64),
+ .with_time(state.clock.now_millis() as i64),
);
}
// Discard partial ops (e.g. old value deleted, new one not written).
@@ -1087,19 +1089,16 @@ impl AppState {
}
}
-pub(crate) fn unix_millis() -> u64 {
- std::time::SystemTime::now()
- .duration_since(std::time::UNIX_EPOCH)
- .map(|d| d.as_millis() as u64)
- .unwrap_or(0)
-}
-
/// The node as seen by the remake module.
impl Host for AppState {
fn remake(&self) -> &RemakeState {
&self.remake
}
+ fn clock(&self) -> Arc<crate::clock::Clock> {
+ Arc::clone(&self.clock)
+ }
+
fn node_id(&self) -> String {
self.local_id().to_owned()
}