Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix hub
Antonio Ojea6607d3ab7d85
diff --git a/cmd/sam-hub/hooks.go b/cmd/sam-hub/hooks.goindex 71831727..a95163d7 100644--- a/cmd/sam-hub/hooks.go+++ b/cmd/sam-hub/hooks.go@@ -70,6 +70,12 @@ func (n *notifier) ListenClose(network.Network, multiaddr.Multiaddr) {}func (n *notifier) Connected(network.Network, network.Conn) {}func (n *notifier) Disconnected(_ network.Network, c network.Conn) {p := c.RemotePeer()++ // If there are still active connections to this peer, don't remove them yet+ if len(n.hub.Host.Network().ConnsToPeer(p)) > 0 {+ return+ }+n.hub.gater.mu.Lock()wasAuth := n.hub.gater.authenticated[p]now := time.Now().UnixMilli()@@ -104,12 +110,16 @@ func (n *notifier) Disconnected(_ network.Network, c network.Conn) {return}// Notify the mesh about the peer disconnection- if err := n.hub.EventTopic.Publish(context.Background(), data); err != nil {- logger.Errorw("Failed to publish mesh event", "peer_id", p, "error", err)- return+ if n.hub.EventTopic != nil {+ if err := n.hub.EventTopic.Publish(context.Background(), data); err != nil {+ logger.Errorw("Failed to publish mesh event", "peer_id", p, "error", err)+ return+ }+ logger.Infow("Published EXIT event", "peer_id", p)+ samHubMeshEventsTotal.WithLabelValues("EXIT").Inc()+ } else {+ logger.Warnw("EventTopic is nil, could not publish EXIT event", "peer_id", p)}- logger.Infow("Published EXIT event", "peer_id", p)- samHubMeshEventsTotal.WithLabelValues("EXIT").Inc()}}}diff --git a/cmd/sam-hub/hooks_test.go b/cmd/sam-hub/hooks_test.gonew file mode 100644index 00000000..2cc2bd10--- /dev/null+++ b/cmd/sam-hub/hooks_test.go@@ -0,0 +1,91 @@+package main++import (+ "testing"++ "time"+ "path/filepath"++ "github.com/libp2p/go-libp2p/core/network"+ "github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-libp2p/core/host"+)++type mockConn struct {+ network.Conn+ peer peer.ID+}++func (m mockConn) RemotePeer() peer.ID { return m.peer }++type mockNetwork struct {+ network.Network+ conns int+}++func (m *mockNetwork) ConnsToPeer(p peer.ID) []network.Conn {+ conns := make([]network.Conn, m.conns)+ return conns+}++type mockHost struct {+ host.Host+ net *mockNetwork+}++func (m *mockHost) Network() network.Network { return m.net }++func TestNotifier_Disconnected(t *testing.T) {+ dir := t.TempDir()+ dbPath := filepath.Join(dir, "test.db")+ kr, err := NewKeyRing(dbPath, 24*time.Hour, nil)+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = kr.Close() }()++ remotePeer := peer.ID("peer1")++ mockNet := &mockNetwork{conns: 1} // Initial state: 1 connection remains after one drops (so 2 total originally)+ mockH := &mockHost{net: mockNet}++ hub := &Hub{+ Host: mockH,+ KeyRing: kr,+ gater: newHubConnGate(),+ }++ notifier := ¬ifier{+ hub: hub,+ }++ // Simulate authentication+ hub.gater.mu.Lock()+ hub.gater.authenticated[remotePeer] = true+ hub.gater.mu.Unlock()++ conn := mockConn{peer: remotePeer}++ // 1. One connection drops, but another remains active (mockNet.conns = 1)+ notifier.Disconnected(mockNet, conn)++ hub.gater.mu.RLock()+ authed := hub.gater.authenticated[remotePeer]+ hub.gater.mu.RUnlock()++ if !authed {+ t.Fatal("Expected peer to remain authenticated because another connection is active")+ }++ // 2. The last connection drops (mockNet.conns = 0)+ mockNet.conns = 0+ notifier.Disconnected(mockNet, conn)++ hub.gater.mu.RLock()+ authed = hub.gater.authenticated[remotePeer]+ hub.gater.mu.RUnlock()++ if authed {+ t.Fatal("Expected peer to be removed from authenticated map after last connection dropped")+ }+}diff --git a/cmd/sam-hub/sync.go b/cmd/sam-hub/sync.goindex 5f113601..9713482f 100644--- a/cmd/sam-hub/sync.go+++ b/cmd/sam-hub/sync.go@@ -266,10 +266,15 @@ func (h *Hub) pruneStaleData() {nowMilli := now.UnixMilli()for p, ts := range h.gater.lastUpdated {if !h.gater.authenticated[p] {- if nowMilli-ts > int64(time.Hour/time.Millisecond) {+ if nowMilli-ts > int64(time.Hour/time.Millisecond) {delete(h.gater.lastUpdated, p)logger.Debugf("Pruned transient peer %s from lastUpdated", p.String())}+ } else if nowMilli-ts > int64(2*time.Hour/time.Millisecond) {+ samHubActiveNodes.Dec()+ delete(h.gater.authenticated, p)+ delete(h.gater.lastUpdated, p)+ logger.Infof("Pruned expired peer %s from authenticated list", p.String())}}}