Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
p2plimits: refuse a cap under one, take a nil logger and a nil endpoint
Antonio Ojeaf61d1029f055
diff --git a/internal/p2plimits/limits.go b/internal/p2plimits/limits.goindex 6bfbf574..dccdb0ac 100644--- a/internal/p2plimits/limits.go+++ b/internal/p2plimits/limits.go@@ -23,6 +23,7 @@ package p2plimitsimport ("errors"+ "fmt""net/netip""strings""sync"@@ -68,8 +69,13 @@ const libp2pDefaultConnsPerIP = 8// NewResourceManager is libp2p's resource manager with the given limits, a// per-source-IP inbound connection cap of perIP and the per-subnet// connection rate limit scaled to it. Loopback is exempt from both, as in-// libp2p's default, so a local mesh never meets them.+// libp2p's default, so a local mesh never meets them. A cap under one+// would refuse every peer, so it is an error here rather than a silent+// listener.func NewResourceManager(limits rcmgr.ConcreteLimitConfig, perIP int) (network.ResourceManager, error) {+ if perIP <= 0 {+ return nil, fmt.Errorf("connections per source address must be at least 1, got %d", perIP)+ }scale := float64(perIP) / libp2pDefaultConnsPerIPconnRateLimiter := &xrate.Limiter{NetworkPrefixLimits: []xrate.PrefixLimit{@@ -122,11 +128,15 @@ type Observed struct {}// Observe wraps inner. refused is counted by reason (see Reasons) and warnf-// written once per source address per RefusalLogInterval.+// written once per source address per RefusalLogInterval; a nil warnf only+// counts.func Observe(inner network.ResourceManager, refused *prometheus.CounterVec, warnf func(format string, args ...any)) *Observed {for _, v := range Reasons {refused.WithLabelValues(v)}+ if warnf == nil {+ warnf = func(string, ...any) {}+ }return &Observed{ResourceManager: inner, refused: refused, warnf: warnf, lastLogAt: map[string]time.Time{}, now: time.Now}}@@ -171,9 +181,12 @@ func (m *Observed) refusedConn(endpoint multiaddr.Multiaddr, err error) {reason := RefusalReason(err)m.refused.WithLabelValues(reason).Inc()+ // The QUIC transport passes no address when it could not build one.source := "?"- if ip, ipErr := manet.ToIP(endpoint); ipErr == nil {- source = ip.String()+ if endpoint != nil {+ if ip, ipErr := manet.ToIP(endpoint); ipErr == nil {+ source = ip.String()+ }}m.mu.Lock()last, seen := m.lastLogAt[source]diff --git a/internal/p2plimits/limits_test.go b/internal/p2plimits/limits_test.goindex 9db3ffed..05dcad79 100644--- a/internal/p2plimits/limits_test.go+++ b/internal/p2plimits/limits_test.go@@ -95,6 +95,20 @@ func TestObservedCountsAndClassifiesRefusals(t *testing.T) {if ok.GetConnLimit() != 42 {t.Errorf("GetConnLimit = %d, want the wrapped manager's 42", ok.GetConnLimit())}++ // The QUIC transport passes no address when it could not build one, and+ // a caller may pass no logger; both are counted and nothing more.+ before = counterValue(t, refused.WithLabelValues(RefusedOther))+ bare := Observe(&refusingResourceManager{err: errors.New("no address")}, refused, nil)+ if _, err := bare.OpenConnection(network.DirInbound, true, nil); err == nil {+ t.Fatal("refusal with no endpoint not returned")+ }+ if got := counterValue(t, refused.WithLabelValues(RefusedOther)); got != before+1 {+ t.Errorf("refusal with no endpoint: counter %v, want %v", got, before+1)+ }+ if _, logged := bare.LastLoggedAt("?"); !logged {+ t.Error("refusal with no endpoint not attributed to the unknown source")+ }}func TestObservedLogsOneSourceOncePerInterval(t *testing.T) {@@ -132,8 +146,12 @@ func TestObservedLogsOneSourceOncePerInterval(t *testing.T) {}// The per-source cap is what the manager enforces, and loopback is exempt.+// A cap under one would refuse every peer and is refused itself.func TestNewResourceManagerCapsOneSourceAddress(t *testing.T) {limits := rcmgr.InfiniteLimits+ if _, err := NewResourceManager(limits, 0); err == nil {+ t.Fatal("a cap of 0 was accepted")+ }m, err := NewResourceManager(limits, 2)if err != nil {t.Fatal(err)