Skip to content

Commit f186f34

Browse files
Merge pull request #189 from FastTrackStudios/refactor/clock-from-architect
refactor(clock): session::clock goes; the shared clock is Architect's presence clock
2 parents 84530a1 + 42925f6 commit f186f34

13 files changed

Lines changed: 166 additions & 268 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
- name: clippy
5252
run: |
5353
timeout 30m cargo clippy --workspace --all-features --all-targets -- -D warnings
54-
for f in sync architect service extension vox clock; do
54+
for f in sync architect service extension vox lead; do
5555
timeout 15m cargo clippy -p session --no-default-features --features "$f" --all-targets -- -D warnings
5656
done
5757
timeout 15m cargo clippy -p session --no-default-features --all-targets -- -D warnings

‎Cargo.lock‎

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ web-time = "1"
2323
# and Studio pin.
2424
loro = { version = "1.12", default-features = false }
2525
# Architect's documents: the kind and schema stamp, `History`, and (feature
26-
# `vox`, tests) the sync transport. The tag Studio pins; bump them together.
27-
crdt = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.12.6", default-features = false }
28-
architect = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.12.6", default-features = false }
29-
architect-permissions = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.12.6" }
26+
# `vox`, tests) the sync transport, and the shared clock presence is
27+
# stamped in (architect#845), by the release tag that carries it; Studio
28+
# pins the same tag.
29+
crdt = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.14.0", default-features = false }
30+
architect = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.14.0", default-features = false }
31+
architect-permissions = { git = "https://github.com/FastTrackStudios/architect", tag = "v0.14.0" }
3032
thiserror = "2"
3133
# Architect's pin (its root Cargo.toml).
3234
vox = { version = "0.10.0-rc.7", default-features = false, features = ["runtime"] }

‎crates/session/Cargo.toml‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@ service = ["architect"]
2828
# `fts.session` as an Architect extension service: the provider's actions
2929
# hand `session::extension::answer` their JSON and the caller's id.
3030
extension = ["service", "dep:facet-json"]
31-
# The session host's clock and a peer's estimate of it (`session::clock`):
32-
# what a follower on another machine needs to follow a playhead.
33-
clock = ["vox", "sync", "architect/platform"]
3431
# The session host's transport, published (`session::lead`, native only):
35-
# its presence and clock served, `lead/play` published.
36-
lead = ["clock", "crdt/vox", "dep:tokio"]
32+
# its presence served (Architect's, which carries its clock), `lead/play`
33+
# published.
34+
lead = ["vox", "sync", "crdt/vox", "dep:tokio"]
3735
vox = ["service", "dep:architect", "architect/vox", "dep:architect-permissions", "dep:thiserror", "dep:vox"]
3836

3937
[dependencies]
@@ -84,7 +82,8 @@ architect-permissions = { workspace = true }
8482
name = "sync_e2e"
8583
required-features = ["architect", "sync", "service"]
8684

87-
# Following a playhead across two processes or machines (the shared clock).
85+
# Following a playhead across two processes or machines (the shared clock:
86+
# Architect's presence clock, architect#845).
8887
[[example]]
8988
name = "follow"
90-
required-features = ["clock"]
89+
required-features = ["vox", "sync"]

‎crates/session/examples/follow.rs‎

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
//! follow follow ws://HOST:4141/vox 30 # follows for 30 s, then reports
77
//! ```
88
//!
9-
//! The leader serves `session::clock::SessionClock` and Architect's
10-
//! presence (a `DocRegistry`'s) over vox on a WebSocket
9+
//! The leader serves Architect's presence (a `DocRegistry`'s, which also
10+
//! serves its clock: `DocPresence::now`) over vox on a WebSocket
1111
//! (`architect::host::EngineHost`), joins its own presence as peer
1212
//! `lead`, and plays a loop: play from 0 at 1 s, seek to 42 s at 5 s, stop
1313
//! at 8 s, every 10 s. It is the session host, so its clock is the shared
14-
//! clock and its `PlayState::at_ms` is stamped in it.
14+
//! clock, and `Here::play_now` stamps its `PlayState::at_ms` in it.
1515
//!
16-
//! The follower dials it, pings the clock ten times a second
17-
//! (`SharedClock::run`), reads `lead/play` from its presence, and draws
16+
//! The follower dials it (its presence driver pings the host's clock ten
17+
//! times a second into `PresencePeer::clock`), reads `lead/play`, and draws
1818
//! the playhead with `sync::Tracker` every 16 ms. It reports the clock
1919
//! estimate and how far its drawn playhead was from the leader's.
2020
//!
@@ -52,15 +52,16 @@ use crdt::sync::{
5252
DocPresenceClient, DocPresenceDispatcher, PresencePeer, doc_presence_service_descriptor,
5353
};
5454
use session::Transport;
55-
use session::clock::{
56-
ClockHost, SessionClockClient, SharedClock, serve as serve_clock,
57-
session_clock_rpc_service_descriptor,
58-
};
5955
use session::doc::transport_of;
6056
use session::sync::Tracker;
61-
use session::sync::clock::now_micros_f64;
6257
use uuid::Uuid;
6358

59+
/// This process's clock, microseconds: the one the presence clock's
60+
/// offset is to (`crdt::presence::clock_ms`).
61+
fn now_micros_f64() -> f64 {
62+
crdt::presence::clock_ms() * 1000.0
63+
}
64+
6465
/// The one presence channel both join: a fixed document id.
6566
const DOC: Uuid = Uuid::from_u128(0x5e55_1011_0000_4000_8000_0000_0000_0001);
6667

@@ -89,15 +90,10 @@ async fn join(url: &str, me: &str) -> PresencePeer {
8990

9091
async fn lead(bind: &str) {
9192
let registry = DocRegistry::new(|_| Box::pin(async { Ok(CrdtDoc::ephemeral()) }));
92-
let router = LayerRouter::new()
93-
.with(
94-
doc_presence_service_descriptor(),
95-
DocPresenceDispatcher::new(registry),
96-
)
97-
.with(
98-
session_clock_rpc_service_descriptor(),
99-
serve_clock(ClockHost),
100-
);
93+
let router = LayerRouter::new().with(
94+
doc_presence_service_descriptor(),
95+
DocPresenceDispatcher::new(registry),
96+
);
10197
let bind = bind.to_owned();
10298
tokio::spawn(async move {
10399
EngineHost::ungated(router, bind)
@@ -119,7 +115,11 @@ async fn lead(bind: &str) {
119115
"lead: serving on {url}; epoch {:.0} µs",
120116
epoch_wall_micros()
121117
);
122-
// The host's clock is the shared clock: its stamps need no offset.
118+
// Stamped in the host's clock by the peer's own estimate of it (the
119+
// leader is the host here, so the estimate is its own clock).
120+
while !here.play_now(false, 0.0, 1.0) {
121+
tokio::time::sleep(Duration::from_millis(50)).await;
122+
}
123123
let mut cycle = 0_u32;
124124
loop {
125125
let start = now_micros_f64();
@@ -132,13 +132,7 @@ async fn lead(bind: &str) {
132132
if wait > 0.0 {
133133
tokio::time::sleep(Duration::from_secs_f64(wait / 1e6)).await;
134134
}
135-
let now = now_micros_f64();
136-
here.play(&crdt::presence::PlayState {
137-
playing,
138-
position,
139-
at_ms: now / 1000.0,
140-
rate: 1.0,
141-
});
135+
here.play_now(playing, position, 1.0);
142136
println!("lead: cycle {cycle} {what} at {position} s");
143137
}
144138
let rest = start + 10e6 - now_micros_f64();
@@ -149,16 +143,8 @@ async fn lead(bind: &str) {
149143

150144
#[allow(clippy::too_many_lines)]
151145
async fn follow(url: &str, seconds: f64) {
152-
let clock = SharedClock::default();
153-
let host: SessionClockClient = pool()
154-
.client(&Endpoint::url(url))
155-
.await
156-
.expect("dial clock");
157-
let pinger = clock.clone();
158-
tokio::spawn(async move {
159-
let _ = pinger.run(&host).await;
160-
});
161146
let peer = join(url, "follow").await;
147+
let clock = peer.clock().clone();
162148
let mut tracker = Tracker::default();
163149
let mut errors: Vec<f64> = Vec::new();
164150
let mut offset_errors: Vec<f64> = Vec::new();
@@ -170,7 +156,7 @@ async fn follow(url: &str, seconds: f64) {
170156
tokio::time::sleep(Duration::from_millis(16)).await;
171157
let states = peer.states();
172158
let (Some(offset), Some(leader), Some(lead_epoch)) = (
173-
clock.offset_micros(),
159+
clock.offset_ms().map(|ms| ms * 1000.0),
174160
states.get("lead/play").and_then(transport_of),
175161
states
176162
.get("lead/epoch")
@@ -197,7 +183,7 @@ async fn follow(url: &str, seconds: f64) {
197183
offset_errors.push((offset - true_offset).abs());
198184
}
199185
}
200-
let rtt = clock.round_trip_micros().unwrap_or(f64::NAN);
186+
let rtt = clock.round_trip_ms().map_or(f64::NAN, |ms| ms * 1000.0);
201187
let stat = |v: &mut Vec<f64>| -> (f64, f64, f64) {
202188
v.sort_by(f64::total_cmp);
203189
if v.is_empty() {

‎crates/session/src/clock.rs‎

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)