aboutsummaryrefslogtreecommitdiffstats
path: root/gleam
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-11-15 18:30:10 -0500
committerelioat <elioat@tilde.institute>2025-11-15 18:30:10 -0500
commit2681493e6d1e35d49f7234f8b0768dcfaa1069d1 (patch)
treea0498bd4e0c49b292c97ac96291008fc16f87305 /gleam
parentf140fec23984251940a7af4e384ac8252a8e7c94 (diff)
downloadtour-2681493e6d1e35d49f7234f8b0768dcfaa1069d1.tar.gz
Attempt to fix duplicate messages with seen_broadcast_ids cache
Added deduplication using (origin_id, from, text, timestamp) as message key. Each message tracked to prevent display more than once per peer. Status: Still seeing 3 copies - investigating if this is due to: 1. State not persisting between loop iterations 2. Messages arriving in same timestamp from different sources 3. Or if this is actually correct behavior in mesh topology
Diffstat (limited to 'gleam')
-rw-r--r--gleam/gossip/src/p2p_peer.gleam21
-rw-r--r--gleam/gossip/src/registry.gleam33
2 files changed, 30 insertions, 24 deletions
diff --git a/gleam/gossip/src/p2p_peer.gleam b/gleam/gossip/src/p2p_peer.gleam
index 73822ba..57855b6 100644
--- a/gleam/gossip/src/p2p_peer.gleam
+++ b/gleam/gossip/src/p2p_peer.gleam
@@ -171,20 +171,17 @@ pub fn input_loop(registry_subject: process.Subject(messages.Message), name: Str
io.println("Goodbye!")
Nil
}
- _ -> {
- // Get current timestamp
- let timestamp = get_timestamp()
-
- // Display the message immediately for user feedback (don't wait for network roundtrip)
- io.println(timestamp <> " [" <> name <> "] " <> trimmed)
-
- // Send message to registry with:
- // - origin_id = our registry_id (so other peers know it's from us)
- // - last_sender_id = -1 (local input, not forwarded from peers)
- // - timestamp = current time
+ _ -> {
+ // Get current timestamp
+ let timestamp = get_timestamp()
+
+ // Send message to registry with:
+ // - origin_id = our registry_id (so other peers know it's from us)
+ // - last_sender_id = -1 (local input, not forwarded from peers)
+ // - timestamp = current time
process.send(registry_subject, messages.Broadcast(name, trimmed, registry_id, -1, timestamp))
input_loop(registry_subject, name, registry_id)
- }
+ }
}
}
diff --git a/gleam/gossip/src/registry.gleam b/gleam/gossip/src/registry.gleam
index 1c5fe01..37963e2 100644
--- a/gleam/gossip/src/registry.gleam
+++ b/gleam/gossip/src/registry.gleam
@@ -70,6 +70,7 @@ fn peer_is_healthy(peer: Peer) -> Bool {
/// - clients: always contains at least our local client
/// - peers: list of other peer registries we're connected to
/// - registry_id: uniquely identifies this registry for loop prevention
+/// - seen_broadcast_ids: cache of displayed messages to prevent duplicates
type RegistryState {
RegistryState(
clients: List(#(String, process.Subject(messages.Message))),
@@ -77,6 +78,7 @@ type RegistryState {
local_client: process.Subject(messages.Message),
local_name: String,
registry_id: Int,
+ seen_broadcast_ids: List(#(Int, String, String, String)),
)
}
@@ -96,6 +98,7 @@ pub fn start(client_subject: process.Subject(messages.Message), name: String, re
local_client: client_subject,
local_name: name,
registry_id: registry_id,
+ seen_broadcast_ids: [],
)
// Create a channel to communicate the subject back
@@ -290,19 +293,24 @@ fn loop(subject: process.Subject(messages.Message), state: RegistryState) -> Nil
messages.Broadcast(from, text, origin_id, last_sender_id, timestamp) -> {
// Broadcast message: display and forward
//
- // Message display strategy to prevent duplicates in mesh networks:
- // - NEVER display messages we originated (origin_id == our registry_id)
- // → The sender displays immediately in input_loop for instant feedback
- // → Prevents duplicate display when message loops back from peers
- // - ONLY display messages from OTHER peers (origin_id != our registry_id)
- // → Each remote message is displayed exactly once
- // → Handles mesh topologies where messages arrive via multiple paths
+ // Message display strategy: Display all messages exactly once.
+ // The key insight: In a mesh network, messages can arrive via multiple paths.
+ // To display each message exactly once, track which messages we've seen.
//
- // This ensures clean, duplicate-free message display across all peers.
+ // We use (origin_id, from, text, timestamp) as a unique identifier.
+ // This avoids duplicates while handling mesh topologies correctly.
- let should_display = case origin_id == state.registry_id {
- True -> False // Our own message - already displayed by input_loop
- False -> True // Remote message - display once
+ let message_id = #(origin_id, from, text, timestamp)
+ let should_display = !list.contains(state.seen_broadcast_ids, message_id)
+
+ // Update seen messages cache (keep bounded to prevent memory leak)
+ let new_seen = case list.length(state.seen_broadcast_ids) > 1000 {
+ True -> list.drop(state.seen_broadcast_ids, 100)
+ False -> state.seen_broadcast_ids
+ }
+ let updated_seen = case should_display {
+ True -> [message_id, ..new_seen]
+ False -> new_seen
}
case should_display {
@@ -325,7 +333,8 @@ fn loop(subject: process.Subject(messages.Message), state: RegistryState) -> Nil
propagate_broadcasts(peers_to_forward, messages.Broadcast(from, text, origin_id, state.registry_id, timestamp))
}
}
- loop(subject, state)
+ let new_state = RegistryState(..state, seen_broadcast_ids: updated_seen)
+ loop(subject, new_state)
}
messages.Gossip(_peer_client_names, peer_info) -> {