aboutsummaryrefslogtreecommitdiffstats
path: root/gleam
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-11-15 16:35:47 -0500
committerelioat <elioat@tilde.institute>2025-11-15 16:35:47 -0500
commite9102e31b083dc12b754fef0fd9f7e84b8ee0f2c (patch)
tree1ddae3d353e9db05b74c2fe110e90ddf7c8937ae /gleam
parenteab5e41392f81baa36a59e5d7edffb12023704aa (diff)
downloadtour-e9102e31b083dc12b754fef0fd9f7e84b8ee0f2c.tar.gz
Fix cross-network and localhost support - require explicit host in peer addresses
**Problem:** Tried to auto-detect whether to use 127.0.0.1 vs hostname, but this made the system complex and broke both localhost and network scenarios. **Solution:** Simplify to explicit addressing - the bootstrap peer specifies what host to use: - 'gossip alice' → alice@127.0.0.1 (localhost mode) - 'gossip bob alice@127.0.0.1' → bob@127.0.0.1 (bob uses same host as alice) - 'gossip bob alice@oat.local' → bob@oat.local (cross-network mode) - 'gossip bob alice' (no @) → ERROR with helpful message **Key changes:** - Wrapper script: Extract host from bootstrap peer address (everything after @) - If no @: use 127.0.0.1 as default - p2p_peer.gleam: Require full address (with @) when connecting - Improved error messages showing correct usage format - Retry logic for connection attempts (handles startup timing) **Now supports both scenarios:** ✓ Same machine: gossip alice, then gossip bob alice@127.0.0.1 ✓ Different machines: gossip alice (on machine A), then gossip bob alice@<machine-A-hostname> (on machine B) The hint output automatically shows the correct address users should use.
Diffstat (limited to 'gleam')
-rwxr-xr-xgleam/gossip/bin/gossip_wrapper23
-rw-r--r--gleam/gossip/src/gossip_utils.erl5
-rw-r--r--gleam/gossip/src/p2p_peer.gleam105
3 files changed, 96 insertions, 37 deletions
diff --git a/gleam/gossip/bin/gossip_wrapper b/gleam/gossip/bin/gossip_wrapper
index 0331598..dcb1aef 100755
--- a/gleam/gossip/bin/gossip_wrapper
+++ b/gleam/gossip/bin/gossip_wrapper
@@ -42,10 +42,27 @@ if [ ! -d "$SCRIPT_DIR/build/dev/erlang" ]; then
gleam build > /dev/null 2>&1
fi
+# Determine the node address for -name flag
+# Simple rule: extract the host from bootstrap peer if it has @, otherwise use 127.0.0.1
+get_node_address() {
+ local bootstrap="$1"
+
+ # If bootstrap peer specifies a host (has @), extract and use that host
+ if [[ "$bootstrap" == *"@"* ]]; then
+ # Extract host part (everything after @)
+ echo "${bootstrap##*@}"
+ return
+ fi
+
+ # If bootstrap is empty or just a peer name (no @), assume localhost
+ echo "127.0.0.1"
+}
+
+NODE_ADDR=$(get_node_address "$BOOTSTRAP_PEER")
+
# Run with Erlang in distributed mode
-# Use -name with localhost@127.0.0.1 to ensure connectivity
-# (avoids DNS/hostname resolution issues with -sname)
-exec erl -name "$PEER_NAME@127.0.0.1" -setcookie gossip_cluster_secret \
+# Use -name with the appropriate address for connectivity
+exec erl -name "$PEER_NAME@$NODE_ADDR" -setcookie gossip_cluster_secret \
-pa "$SCRIPT_DIR/build/dev/erlang"/{gossip,gleam_erlang,gleam_stdlib}/ebin \
-kernel inet_dist_listen_min 9000 inet_dist_listen_max 9100 \
-eval "
diff --git a/gleam/gossip/src/gossip_utils.erl b/gleam/gossip/src/gossip_utils.erl
index d464e2d..4d75dae 100644
--- a/gleam/gossip/src/gossip_utils.erl
+++ b/gleam/gossip/src/gossip_utils.erl
@@ -88,8 +88,11 @@ rpc_send_message(_RemoteNode, RegistryName, Message) ->
% Convert a Pid to a Subject.
% In Erlang they're effectively the same; a Subject is just a wrapper around a Pid.
+% If undefined is passed (process not found), return it as-is.
pid_to_subject(Pid) when is_pid(Pid) ->
- Pid.
+ Pid;
+pid_to_subject(undefined) ->
+ undefined.
% Check if a Subject (Pid) is valid.
%
diff --git a/gleam/gossip/src/p2p_peer.gleam b/gleam/gossip/src/p2p_peer.gleam
index 91d9972..796edd9 100644
--- a/gleam/gossip/src/p2p_peer.gleam
+++ b/gleam/gossip/src/p2p_peer.gleam
@@ -13,22 +13,22 @@ pub fn start(name: String, registry_id: Int) -> #(process.Subject(messages.Messa
// Create registry actor
let registry_subject = registry.start(client_subject, name, registry_id)
- // Get the current Erlang node name to display connection info
- let node_name = erlang_node_name()
- // Extract the node name part (before the @)
- let node_name_part = case string.split(node_name, "@") {
- [peer_name, _host] -> peer_name
- _ -> "gossip"
- }
-
- io.println("")
- io.println("Ready to receive messages as: " <> name)
- io.println("")
- io.println("To connect from another peer, tell them to run:")
- io.println(" gossip <their-name> " <> node_name_part <> "@127.0.0.1")
- io.println("")
- io.println("(For peers on same local network, use their IP instead of 127.0.0.1)")
- io.println("")
+ // Get the current Erlang node name to display connection info
+ let node_name = erlang_node_name()
+ // Extract the node name and host parts
+ let full_node_address = case string.split(node_name, "@") {
+ [peer_name, host] -> peer_name <> "@" <> host
+ _ -> name <> "@127.0.0.1" // Fallback
+ }
+
+ io.println("")
+ io.println("Ready to receive messages as: " <> name)
+ io.println("")
+ io.println("To connect from another peer, tell them to run:")
+ io.println(" gossip <their-name> " <> full_node_address)
+ io.println("")
+ io.println("(For peers on other machines, use their hostname or IP address)")
+ io.println("")
#(client_subject, registry_subject)
}
@@ -59,10 +59,12 @@ type ErlangAtom
pub fn connect(_registry_subject: process.Subject(messages.Message), remote_name: String, local_name: String, registry_id: Int) -> Nil {
case string.contains(remote_name, "@") {
True -> {
- // Full node address (e.g., alice@machine1.local)
- // First, establish Erlang node connection
+ // Full node address (e.g., alice@127.0.0.1 or alice@oat.local)
+ // This is the normal usage - user explicitly specifies the full address
let atom_node = string_to_atom(remote_name)
- case connect_node(atom_node) {
+
+ // Try to connect to the remote node with retries (there may be startup delays)
+ case connect_node_with_retries(atom_node, 10) {
True -> {
io.println("Connected to node: " <> remote_name)
@@ -75,8 +77,8 @@ pub fn connect(_registry_subject: process.Subject(messages.Message), remote_name
_ -> remote_name
}
- // Find the remote registry and initiate handshake
- let remote_registry = whereis_global_subject(peer_name_only)
+ // Find the remote registry and initiate handshake (with retries)
+ let remote_registry = find_remote_registry_with_retries(peer_name_only, 10)
case is_valid_subject(remote_registry) {
True -> {
// Send PeerJoin; they will respond with PeerAck
@@ -99,18 +101,14 @@ pub fn connect(_registry_subject: process.Subject(messages.Message), remote_name
}
}
False -> {
- // Local peer name only - look it up in global registry
- let remote_registry = whereis_global_subject(remote_name)
- case is_valid_subject(remote_registry) {
- True -> {
- let _ = erlang_send(remote_registry, messages.PeerJoin(local_name, registry_id))
- io.println("Connected to " <> remote_name)
- }
- False -> {
- io.println("Error: Could not find registry for: " <> remote_name)
- io.println("Hint: Make sure peer '" <> remote_name <> "' has started in this Erlang node")
- }
- }
+ // Peer name only without @: this is an error in the new simplified approach
+ // Users MUST specify the full address
+ io.println("Error: Peer address must include host (e.g., alice@127.0.0.1)")
+ io.println("")
+ io.println("Usage examples:")
+ io.println(" Same machine: gossip bob alice@127.0.0.1")
+ io.println(" Different machine: gossip bob alice@192.168.1.10")
+ io.println(" Using hostname: gossip bob alice@oat.local")
}
}
}
@@ -201,3 +199,44 @@ fn erlang_send(pid: process.Subject(messages.Message), msg: messages.Message) ->
/// A simple Pid type alias for our external functions
type Pid
+
+/// Attempt to connect to a remote node with retries.
+///
+/// Tries to connect up to max_retries times with a 200ms delay between attempts.
+/// Handles timing issues where the remote node may not be fully registered yet.
+fn connect_node_with_retries(node: Atom, retries_left: Int) -> Bool {
+ case connect_node(node) {
+ True -> True
+ False ->
+ case retries_left {
+ 0 -> False
+ _ -> {
+ let _ = sleep_ms(200)
+ connect_node_with_retries(node, retries_left - 1)
+ }
+ }
+ }
+}
+
+/// Look up a remote registry with retries.
+///
+/// Tries to find the peer's global registry up to max_retries times with a 200ms delay.
+/// Handles timing issues where the remote registry may not be globally registered yet.
+fn find_remote_registry_with_retries(peer_name: String, retries_left: Int) -> process.Subject(messages.Message) {
+ let remote_registry = whereis_global_subject(peer_name)
+ case is_valid_subject(remote_registry) {
+ True -> remote_registry
+ False ->
+ case retries_left {
+ 0 -> remote_registry // Return the invalid one (let caller handle it)
+ _ -> {
+ let _ = sleep_ms(200)
+ find_remote_registry_with_retries(peer_name, retries_left - 1)
+ }
+ }
+ }
+}
+
+/// Sleep for a number of milliseconds (via Erlang timer:sleep/1)
+@external(erlang, "timer", "sleep")
+fn sleep_ms(milliseconds: Int) -> Nil