From d2091b9237bbaa44473725844d7e415b12858392 Mon Sep 17 00:00:00 2001 From: Lucas Molas Date: Thu, 2 Dec 2021 22:03:00 -0300 Subject: [PATCH] fix(corehttp): adjust peer counting metrics (#8577) (cherry picked from commit 9d197ca732b5b1f80a907d35f36d6d0c0b6828cf) --- core/corehttp/metrics.go | 13 +++++++++++-- core/corehttp/metrics_test.go | 12 +++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/corehttp/metrics.go b/core/corehttp/metrics.go index e7f36113bda..c3dbde0ac01 100644 --- a/core/corehttp/metrics.go +++ b/core/corehttp/metrics.go @@ -160,9 +160,18 @@ func (c IpfsNodeCollector) PeersTotalValues() map[string]float64 { if c.Node.PeerHost == nil { return vals } - for _, conn := range c.Node.PeerHost.Network().Conns() { + for _, peerID := range c.Node.PeerHost.Network().Peers() { + // Each peer may have more than one connection (see for an explanation + // https://github.com/libp2p/go-libp2p-swarm/commit/0538806), so we grab + // only one, the first (an arbitrary and non-deterministic choice), which + // according to ConnsToPeer is the oldest connection in the list + // (https://github.com/libp2p/go-libp2p-swarm/blob/v0.2.6/swarm.go#L362-L364). + conns := c.Node.PeerHost.Network().ConnsToPeer(peerID) + if len(conns) == 0 { + continue + } tr := "" - for _, proto := range conn.RemoteMultiaddr().Protocols() { + for _, proto := range conns[0].RemoteMultiaddr().Protocols() { tr = tr + "/" + proto.Name } vals[tr] = vals[tr] + 1 diff --git a/core/corehttp/metrics_test.go b/core/corehttp/metrics_test.go index dc3b68bc945..76f79d2ee2d 100644 --- a/core/corehttp/metrics_test.go +++ b/core/corehttp/metrics_test.go @@ -44,11 +44,13 @@ func TestPeersTotal(t *testing.T) { node := &core.IpfsNode{PeerHost: hosts[0]} collector := IpfsNodeCollector{Node: node} - actual := collector.PeersTotalValues() - if len(actual) != 1 { - t.Fatalf("expected 1 peers transport, got %d, transport map %v", len(actual), actual) + peersTransport := collector.PeersTotalValues() + if len(peersTransport) > 2 { + t.Fatalf("expected at most 2 peers transport (tcp and upd/quic), got %d, transport map %v", + len(peersTransport), peersTransport) } - if actual["/ip4/tcp"] != float64(3) { - t.Fatalf("expected 3 peers, got %f", actual["/ip4/tcp"]) + totalPeers := peersTransport["/ip4/tcp"] + peersTransport["/ip4/udp/quic"] + if totalPeers != 3 { + t.Fatalf("expected 3 peers in either tcp or upd/quic transport, got %f", totalPeers) } }