Skip to content

Commit

Permalink
fix(corehttp): adjust peer counting metrics (#8577)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9d197ca)
  • Loading branch information
schomatis authored and guseggert committed Dec 3, 2021
1 parent 726b9e2 commit 042efd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
13 changes: 11 additions & 2 deletions core/corehttp/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions core/corehttp/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 042efd3

Please sign in to comment.