diff --git a/go.mod b/go.mod index ac5631de..617b820b 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/klauspost/compress v1.15.12 github.com/libp2p/go-buffer-pool v0.1.0 github.com/libp2p/go-flow-metrics v0.1.0 - github.com/libp2p/go-libp2p-asn-util v0.2.0 + github.com/libp2p/go-libp2p-asn-util v0.3.0 github.com/libp2p/go-libp2p-testing v0.12.0 github.com/libp2p/go-mplex v0.7.0 github.com/libp2p/go-msgio v0.3.0 diff --git a/go.sum b/go.sum index 5509e551..6bfd02a8 100644 --- a/go.sum +++ b/go.sum @@ -318,8 +318,8 @@ github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38y github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM= github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro= -github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw= -github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI= +github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s= +github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w= github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA= github.com/libp2p/go-libp2p-testing v0.12.0/go.mod h1:KcGDRXyN7sQCllucn1cOOS+Dmm7ujhfEyXQL5lvkcPg= github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY= diff --git a/p2p/protocol/circuitv2/relay/relay.go b/p2p/protocol/circuitv2/relay/relay.go index ca50d424..ee6f5528 100644 --- a/p2p/protocol/circuitv2/relay/relay.go +++ b/p2p/protocol/circuitv2/relay/relay.go @@ -18,6 +18,7 @@ import ( logging "github.com/ipfs/go-log/v2" pool "github.com/libp2p/go-buffer-pool" + asnutil "github.com/libp2p/go-libp2p-asn-util" ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" ) @@ -545,6 +546,8 @@ func (r *Relay) makeLimitMsg(p peer.ID) *pbv2.Limit { } func (r *Relay) background() { + asnutil.Store.Init() + ticker := time.NewTicker(time.Minute) defer ticker.Stop() diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 91c351e5..72d591ec 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -81,12 +81,12 @@ type IDService interface { io.Closer } -type IdentifyPushSupport uint8 +type identifyPushSupport uint8 const ( - IdentifyPushSupportUnknown IdentifyPushSupport = iota - IdentifyPushSupported - IdentifyPushUnsupported + identifyPushSupportUnknown identifyPushSupport = iota + identifyPushSupported + identifyPushUnsupported ) type entry struct { @@ -96,7 +96,7 @@ type entry struct { // PushSupport saves our knowledge about the peer's support of the Identify Push protocol. // Before the identify request returns, we don't know yet if the peer supports Identify Push. - PushSupport IdentifyPushSupport + PushSupport identifyPushSupport // Sequence is the sequence number of the last snapshot we sent to this peer. Sequence uint64 } @@ -271,7 +271,7 @@ func (ids *idService) sendPushes(ctx context.Context) { for c, e := range ids.conns { // Push even if we don't know if push is supported. // This will be only the case while the IdentifyWaitChan call is in flight. - if e.PushSupport == IdentifyPushSupported || e.PushSupport == IdentifyPushSupportUnknown { + if e.PushSupport == identifyPushSupported || e.PushSupport == identifyPushSupportUnknown { conns = append(conns, c) } } @@ -496,9 +496,9 @@ func (ids *idService) handleIdentifyResponse(s network.Stream, isPush bool) erro } sup, err := ids.Host.Peerstore().SupportsProtocols(c.RemotePeer(), IDPush) if supportsIdentifyPush := err == nil && len(sup) > 0; supportsIdentifyPush { - e.PushSupport = IdentifyPushSupported + e.PushSupport = identifyPushSupported } else { - e.PushSupport = IdentifyPushUnsupported + e.PushSupport = identifyPushUnsupported } if ids.metricsTracer != nil { diff --git a/p2p/protocol/identify/metrics.go b/p2p/protocol/identify/metrics.go index f11a5851..28598fa3 100644 --- a/p2p/protocol/identify/metrics.go +++ b/p2p/protocol/identify/metrics.go @@ -95,7 +95,7 @@ type MetricsTracer interface { TriggeredPushes(event any) // ConnPushSupport counts peers by Push Support - ConnPushSupport(IdentifyPushSupport) + ConnPushSupport(identifyPushSupport) // IdentifyReceived tracks metrics on receiving an identify response IdentifyReceived(isPush bool, numProtocols int, numAddrs int) @@ -146,7 +146,7 @@ func (t *metricsTracer) TriggeredPushes(ev any) { pushesTriggered.WithLabelValues(*tags...).Inc() } -func (t *metricsTracer) IncrementPushSupport(s IdentifyPushSupport) { +func (t *metricsTracer) IncrementPushSupport(s identifyPushSupport) { tags := metricshelper.GetStringSlice() defer metricshelper.PutStringSlice(tags) @@ -186,7 +186,7 @@ func (t *metricsTracer) IdentifyReceived(isPush bool, numProtocols int, numAddrs numAddrsReceived.Observe(float64(numAddrs)) } -func (t *metricsTracer) ConnPushSupport(support IdentifyPushSupport) { +func (t *metricsTracer) ConnPushSupport(support identifyPushSupport) { tags := metricshelper.GetStringSlice() defer metricshelper.PutStringSlice(tags) @@ -194,11 +194,11 @@ func (t *metricsTracer) ConnPushSupport(support IdentifyPushSupport) { connPushSupportTotal.WithLabelValues(*tags...).Inc() } -func getPushSupport(s IdentifyPushSupport) string { +func getPushSupport(s identifyPushSupport) string { switch s { - case IdentifyPushSupported: + case identifyPushSupported: return "supported" - case IdentifyPushUnsupported: + case identifyPushUnsupported: return "not supported" default: return "unknown" diff --git a/p2p/protocol/identify/metrics_alloc_test/metrics_test.go b/p2p/protocol/identify/metrics_test.go similarity index 61% rename from p2p/protocol/identify/metrics_alloc_test/metrics_test.go rename to p2p/protocol/identify/metrics_test.go index 961daf14..2cf5a209 100644 --- a/p2p/protocol/identify/metrics_alloc_test/metrics_test.go +++ b/p2p/protocol/identify/metrics_test.go @@ -1,18 +1,12 @@ //go:build nocover -// These tests are in their own package to avoid transitively pulling in other -// deps that may run background tasks in their init and thus allocate. Looking -// at you -// [go-libp2p-asn-util](https://github.com/libp2p/go-libp2p-asn-util/blob/master/asn.go#L14) - -package identify_alloc_test +package identify import ( "math/rand" "testing" "github.com/libp2p/go-libp2p/core/event" - "github.com/libp2p/go-libp2p/p2p/protocol/identify" ) func TestMetricsNoAllocNoCover(t *testing.T) { @@ -22,20 +16,19 @@ func TestMetricsNoAllocNoCover(t *testing.T) { event.EvtNATDeviceTypeChanged{}, } - pushSupport := []identify.IdentifyPushSupport{ - identify.IdentifyPushSupportUnknown, - identify.IdentifyPushSupported, - identify.IdentifyPushUnsupported, + pushSupport := []identifyPushSupport{ + identifyPushSupportUnknown, + identifyPushSupported, + identifyPushUnsupported, } - tr := identify.NewMetricsTracer() + tr := NewMetricsTracer() tests := map[string]func(){ "TriggeredPushes": func() { tr.TriggeredPushes(events[rand.Intn(len(events))]) }, "ConnPushSupport": func() { tr.ConnPushSupport(pushSupport[rand.Intn(len(pushSupport))]) }, "IdentifyReceived": func() { tr.IdentifyReceived(rand.Intn(2) == 0, rand.Intn(20), rand.Intn(20)) }, "IdentifySent": func() { tr.IdentifySent(rand.Intn(2) == 0, rand.Intn(20), rand.Intn(20)) }, } - for method, f := range tests { allocs := testing.AllocsPerRun(1000, f) if allocs > 0 { diff --git a/test-plans/go.mod b/test-plans/go.mod index 5c044526..2d60359b 100644 --- a/test-plans/go.mod +++ b/test-plans/go.mod @@ -40,7 +40,7 @@ require ( github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-cidranger v1.1.0 // indirect github.com/libp2p/go-flow-metrics v0.1.0 // indirect - github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect + github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect github.com/libp2p/go-mplex v0.7.0 // indirect github.com/libp2p/go-msgio v0.3.0 // indirect github.com/libp2p/go-nat v0.1.0 // indirect diff --git a/test-plans/go.sum b/test-plans/go.sum index 08138b32..a20af0bb 100644 --- a/test-plans/go.sum +++ b/test-plans/go.sum @@ -280,8 +280,8 @@ github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38y github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic= github.com/libp2p/go-flow-metrics v0.1.0 h1:0iPhMI8PskQwzh57jB9WxIuIOQ0r+15PChFGkx3Q3WM= github.com/libp2p/go-flow-metrics v0.1.0/go.mod h1:4Xi8MX8wj5aWNDAZttg6UPmc0ZrnFNsMtpsYUClFtro= -github.com/libp2p/go-libp2p-asn-util v0.2.0 h1:rg3+Os8jbnO5DxkC7K/Utdi+DkY3q/d1/1q+8WeNAsw= -github.com/libp2p/go-libp2p-asn-util v0.2.0/go.mod h1:WoaWxbHKBymSN41hWSq/lGKJEca7TNm58+gGJi2WsLI= +github.com/libp2p/go-libp2p-asn-util v0.3.0 h1:gMDcMyYiZKkocGXDQ5nsUQyquC9+H+iLEQHwOCZ7s8s= +github.com/libp2p/go-libp2p-asn-util v0.3.0/go.mod h1:B1mcOrKUE35Xq/ASTmQ4tN3LNzVVaMNmq2NACuqyB9w= github.com/libp2p/go-libp2p-testing v0.12.0 h1:EPvBb4kKMWO29qP4mZGyhVzUyR25dvfUIK5WDu6iPUA= github.com/libp2p/go-mplex v0.7.0 h1:BDhFZdlk5tbr0oyFq/xv/NPGfjbnrsDam1EvutpBDbY= github.com/libp2p/go-mplex v0.7.0/go.mod h1:rW8ThnRcYWft/Jb2jeORBmPd6xuG3dGxWN/W168L9EU=