aprs.me/lib/aprsme/cluster/packet_distributor.ex
Graham McIntire 4804bd16be
Add distributed Erlang clustering for single APRS-IS connection
Implement leader election to ensure only one APRS-IS connection across
multiple Kubernetes replicas. This prevents duplicate packet processing
and respects APRS-IS usage policies.

Key changes:
- Add leader election using :global registry
- Create connection manager for dynamic APRS-IS management
- Implement packet distribution from leader to all nodes
- Add Kubernetes headless service for node discovery
- Configure DNS-based clustering with libcluster
- Update deployment to support 3 replicas with clustering

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-25 10:30:29 -05:00

38 lines
1.1 KiB
Elixir

defmodule Aprsme.Cluster.PacketDistributor do
@moduledoc """
Distributes APRS packets from the leader node to all cluster members.
This ensures all nodes can serve real-time updates via LiveView while
only the leader maintains the APRS-IS connection.
"""
require Logger
@pubsub_topic "cluster:packets"
def distribute_packet(packet) do
# Only distribute if we're the leader
if Aprsme.Cluster.LeaderElection.is_leader?() do
# Broadcast to all nodes including self
Phoenix.PubSub.broadcast(
Aprsme.PubSub,
@pubsub_topic,
{:distributed_packet, packet}
)
Logger.debug("Distributed packet #{packet.raw} to cluster")
end
end
def subscribe do
Phoenix.PubSub.subscribe(Aprsme.PubSub, @pubsub_topic)
end
def handle_distributed_packet({:distributed_packet, packet}) do
# Broadcast to local LiveView clients
Aprsme.StreamingPacketsPubSub.broadcast_packet(packet)
# Update packet store for LiveView
AprsmeWeb.MapLive.PacketStore.store_packet(packet)
Logger.debug("Received distributed packet on node #{node()}")
end
end