relay: initialize the ASN util when starting the service (#2143)

* relay: initialize the ASN util when starting the service

* Revert "Fix flaky TestMetricsNoAllocNoCover test (#2142)"

This reverts commit cbcdd79084.
This commit is contained in:
Marten Seemann 2023-02-24 14:56:35 -08:00 committed by GitHub
parent cbcdd79084
commit c1cfe6c16e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 33 deletions

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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()

View File

@ -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 {

View File

@ -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"

View File

@ -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 {

View File

@ -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

View File

@ -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=