Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions p2p/protocol/circuitv2/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()

Expand Down
16 changes: 8 additions & 8 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions p2p/protocol/identify/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -186,19 +186,19 @@ 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)

*tags = append(*tags, getPushSupport(support))
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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test-plans/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test-plans/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down