Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node: reprovide within the provider TTL
Antonio Ojeab90ad0e9036a
diff --git a/internal/node/node.go b/internal/node/node.goindex e84d2c83..6791e7fe 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -792,8 +792,12 @@ func (n *AgentMeshNode) Start(ctx context.Context) error {// Start connection monitorn.startConnectionMonitor(ctx, n.config.MonitorBootstrap, n.config.MonitorInterval, 3)- // Periodically and on-demand reprovide registered services to the DHT- n.startReprovideLoop(ctx, ReprovideInterval)+ // Periodically and on-demand reprovide registered services to the DHT.+ // A record lives DHTProviderAddrTTL after its last announcement, so the+ // announcements come at least three times within it, as the default+ // pair does; an operator who shortens the TTL does not get a service+ // that is absent most of the time.+ n.startReprovideLoop(ctx, n.reprovideInterval())// Everything read from the control plane (keys, bans, router addresses,// policy) in one loop; gossip events only bring the next pull forward.@@ -815,6 +819,16 @@ func (n *AgentMeshNode) triggerReprovide() {}}+// reprovideInterval is ReprovideInterval, or a third of a shorter provider+// TTL, never under a second.+func (n *AgentMeshNode) reprovideInterval() time.Duration {+ ttl := n.config.DHTProviderAddrTTL+ if ttl <= 0 || ttl/3 >= ReprovideInterval {+ return ReprovideInterval+ }+ return max(ttl/3, time.Second)+}+func (n *AgentMeshNode) startReprovideLoop(ctx context.Context, interval time.Duration) {go func() {initialDelay := 5 * time.Seconddiff --git a/internal/node/node_test.go b/internal/node/node_test.goindex 453cacc4..13471a69 100644--- a/internal/node/node_test.go+++ b/internal/node/node_test.go@@ -891,3 +891,25 @@ func TestFindProvidersSkipsRevoked(t *testing.T) {t.Errorf("revoked peer returned as provider: %v", providers)}}++// A provider record lives DHTProviderAddrTTL; the node announces at least+// three times within it, so a shorter TTL shortens the interval and the+// default pair stays as it was.+func TestReprovideIntervalFollowsProviderTTL(t *testing.T) {+ for _, tc := range []struct {+ ttl time.Duration+ want time.Duration+ }{+ {0, ReprovideInterval},+ {DefaultDHTProviderAddrTTL, ReprovideInterval},+ {time.Hour, ReprovideInterval},+ {30 * time.Second, 10 * time.Second},+ {5 * time.Second, 5 * time.Second / 3},+ {time.Second, time.Second},+ } {+ n := &AgentMeshNode{config: Options{DHTProviderAddrTTL: tc.ttl}}+ if got := n.reprovideInterval(); got != tc.want {+ t.Errorf("ttl %s: reprovide every %s, want %s", tc.ttl, got, tc.want)+ }+ }+}