Skip to content

Agent Mesh

router: forget a peer router the control plane no longer lists

Antonio Ojeadd17037a3633
diff --git a/internal/router/router.go b/internal/router/router.go
index 9c83059f..5e5f218d 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -278,8 +278,8 @@ type Router struct {
EventTopic *pubsub.Topic
authenticatedPeers sync.Map
bannedPeers sync.Map
- // peerRouters is the other routers of the mesh, as /info lists them:
- // held whatever the load and never sent away.
+ // peerRouters is the other routers of the mesh, as /info lists them or as
+ // a handshake proved them: held whatever the load and never sent away.
peerRouters sync.Map
// handshakeLimiter bounds /mesh/auth attempts per peer; any internet peer
// can open those streams.
@@ -1283,9 +1283,11 @@ func (r *Router) connectBootstrapRouters() {
// restarted or missed a MeshEvent_BANNED catches up.
r.reconcileBannedPeers(info.GetBannedPeerIds(), fetchedAt)
+ listed := map[peer.ID]bool{}
for _, addrStr := range info.RouterAddresses {
ma, err := multiaddr.NewMultiaddr(addrStr)
if err != nil {
+ logger.Warnf("[Federation] Ignoring router address %q: %v", addrStr, err)
continue
}
@@ -1303,6 +1305,7 @@ func (r *Router) connectBootstrapRouters() {
if pi.ID == r.Host.ID() {
continue
}
+ listed[pi.ID] = true
r.peerRouters.Store(pi.ID, true)
r.Host.ConnManager().Protect(pi.ID, federationTag)
if r.isPeerAuthenticated(pi.ID) {
@@ -1311,6 +1314,26 @@ func (r *Router) connectBootstrapRouters() {
r.authenticatePeerRouter(*pi)
}
}
+ r.forgetUnlistedRouters(listed)
+}
+
+// forgetUnlistedRouters drops the peer routers the control plane no longer
+// lists: a router that was replaced keeps its lease until it expires and
+// nothing else would ever remove it, so it would stay listed and protected
+// here for the life of the process. A router that proved its role in a
+// handshake but is not listed is kept while that admission lasts; its
+// lease is normally visible before it handshakes.
+func (r *Router) forgetUnlistedRouters(listed map[peer.ID]bool) {
+ r.peerRouters.Range(func(k, _ any) bool {
+ p := k.(peer.ID)
+ if listed[p] || r.isPeerAuthenticated(p) {
+ return true
+ }
+ logger.Infof("[Federation] Peer router %s is no longer listed by the control plane, forgetting it", p)
+ r.peerRouters.Delete(p)
+ r.Host.ConnManager().Unprotect(p, federationTag)
+ return true
+ })
}
// authenticatePeerRouter runs the mutual handshake with a router the control
diff --git a/internal/router/router_test.go b/internal/router/router_test.go
index 4f7b2ccd..279b42c5 100644
--- a/internal/router/router_test.go
+++ b/internal/router/router_test.go
@@ -1123,6 +1123,35 @@ func TestFederationAuthenticatesListedRoutersNotCurrentlyAdmitted(t *testing.T)
}
}
+// A router the control plane stopped listing is forgotten once its
+// admission here has lapsed, and its connection loses the federation
+// protection; one still admitted, listed or not, is kept.
+func TestFederationForgetsRoutersNoLongerListed(t *testing.T) {
+ r := newGoAwayRouter(t, 100)
+ listed, unlistedAdmitted, gone := peer.ID("router-listed"), peer.ID("router-unlisted-admitted"), peer.ID("router-gone")
+ for _, p := range []peer.ID{listed, unlistedAdmitted, gone} {
+ r.peerRouters.Store(p, true)
+ r.Host.ConnManager().Protect(p, federationTag)
+ }
+ r.authenticatedPeers.Store(listed, time.Now().Add(time.Hour))
+ r.authenticatedPeers.Store(unlistedAdmitted, time.Now().Add(time.Hour))
+ r.authenticatedPeers.Store(gone, time.Now().Add(-time.Minute))
+
+ r.forgetUnlistedRouters(map[peer.ID]bool{listed: true})
+
+ for _, p := range []peer.ID{listed, unlistedAdmitted} {
+ if _, ok := r.peerRouters.Load(p); !ok || !r.Host.ConnManager().IsProtected(p, federationTag) {
+ t.Errorf("%s was forgotten or unprotected; want kept", p)
+ }
+ }
+ if _, ok := r.peerRouters.Load(gone); ok || r.Host.ConnManager().IsProtected(gone, federationTag) {
+ t.Errorf("%s is still a protected peer router after leaving the list", gone)
+ }
+ if n, _ := r.peerRouterCounts(); n != 2 {
+ t.Errorf("peer routers listed: %d, want 2", n)
+ }
+}
+
// The router's /mesh/auth handler is reachable by any internet peer. An idle
// stream must be closed on the router's schedule, and a peer handshaking in a
// tight loop must be cut off before every frame is verified.