Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/stitch-p2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stitch-p2p"
version = "0.1.0"
version = "0.1.1"
edition = "2024"
rust-version = "1.90"
license = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions crates/stitch-p2p/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Implemented and tested (the full verified core, transport excluded):
`stitch-tasks` demo. `tests/discovery_broker.rs` runs two peers through a real
`mqdb` broker — register, discover, NAT-traverse to QUIC, and converge — end to
end.
`Swarm::spawn` polls the broker for peers at the sync `pull_interval`; `Swarm::spawn_with_discovery_interval` sets a separate, usually longer, discovery interval so the broker is not queried as often as sessions pull.

- `store` — `Store`, the app-facing document facade over `SyncNode`. JSON
records keyed by `(entity, id)`; `create`/`read`/`update` (read-merge-write)/
Expand Down
14 changes: 13 additions & 1 deletion crates/stitch-p2p/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ fn track(bridges: &Bridges, handle: JoinHandle<()>) {
impl Swarm {
#[must_use]
pub fn spawn(peer: Arc<Peer>, node: SyncNode, pull_interval: Duration) -> Self {
Self::spawn_with_discovery_interval(peer, node, pull_interval, pull_interval)
}

#[must_use]
pub fn spawn_with_discovery_interval(
peer: Arc<Peer>,
node: SyncNode,
pull_interval: Duration,
discovery_interval: Duration,
) -> Self {
let connected: Arc<Mutex<HashSet<String>>> = Arc::new(Mutex::new(HashSet::new()));
let bridges: Bridges = Arc::new(Mutex::new(Vec::new()));
let accept = tokio::spawn(accept_loop(
Expand All @@ -76,6 +86,7 @@ impl Swarm {
connected,
Arc::clone(&bridges),
pull_interval,
discovery_interval,
));
Self {
handles: vec![accept, connect],
Expand Down Expand Up @@ -135,6 +146,7 @@ async fn connect_loop(
connected: Arc<Mutex<HashSet<String>>>,
bridges: Bridges,
pull_interval: Duration,
discovery_interval: Duration,
) {
let my_id = match peer.peer_id() {
Some(id) => id.to_string(),
Expand Down Expand Up @@ -171,7 +183,7 @@ async fn connect_loop(
}
}
}
tokio::time::sleep(pull_interval).await;
tokio::time::sleep(discovery_interval).await;
}
}

Expand Down
20 changes: 17 additions & 3 deletions crates/stitch-p2p/tests/discovery_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ async fn make_peer(name: &str, broker: &str) -> (Arc<Peer>, SyncNode) {

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn two_peers_discover_and_sync_over_broker() {
let pull = Duration::from_millis(500);
discover_and_sync(|peer, node| Swarm::spawn(peer, node, pull)).await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn discovery_polls_on_its_own_interval() {
let pull = Duration::from_millis(500);
let discovery = Duration::from_secs(3);
discover_and_sync(|peer, node| {
Swarm::spawn_with_discovery_interval(peer, node, pull, discovery)
})
.await;
}

async fn discover_and_sync(spawn: impl Fn(Arc<Peer>, SyncNode) -> Swarm) {
if !mqdb_available() {
eprintln!("skipping: `mqdb` binary not found on PATH");
return;
Expand All @@ -112,9 +127,8 @@ async fn two_peers_discover_and_sync_over_broker() {
let (peer_a, node_a) = make_peer("alice", &broker_addr).await;
let (peer_b, node_b) = make_peer("bob", &broker_addr).await;

let pull = Duration::from_millis(500);
let swarm_a = Swarm::spawn(peer_a, node_a.clone(), pull);
let swarm_b = Swarm::spawn(peer_b, node_b.clone(), pull);
let swarm_a = spawn(peer_a, node_a.clone());
let swarm_b = spawn(peer_b, node_b.clone());

node_a
.local_write(Op::Insert, "task", "t1", b"from-alice".to_vec())
Expand Down
Loading