Host stats read each peer's latency and who dialled whom

ipfs swarm peers --latency --direction, for the big map's peer table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vvvomitandClaude Opus 5.5 committed 2026-10-30 19:55:09 +03:30
1 parent 0e572e386c
commit f2823ad950
2 files changed
+26 -2

No files matched your search

+19
View File
@@ -54,6 +54,8 @@ type Peer struct {
ID string
Addr netip.Addr
Sent, Recv int64
Latency time.Duration // 0 if not measured yet
Inbound bool // it connected to the node, not the other way
}
// HostPart is one part of the newest publish on the node.
@@ -147,6 +149,7 @@ ipfs swarm peers | head -24 | while read -r a; do
l=$(ipfs bitswap ledger --enc=json "${a##*/}" </dev/null)
echo peer $a $(echo "$l" | grep -oE '"Sent":[0-9]+' | cut -d: -f2) $(echo "$l" | grep -oE '"Recv":[0-9]+' | cut -d: -f2)
done
echo swarm $(ipfs swarm peers --latency --direction --enc=json | head -c 65536)
echo bitswap "{$(ipfs stats bitswap --enc=json | grep -oE '"(BlocksSent|DataSent|BlocksReceived|DataReceived)":[0-9]+' | paste -sd,)}"
# The newest publish: ipfs-follow pins its root directly, the mirror's
# publish recursively.
@@ -253,6 +256,22 @@ func parseHostStats(out string) *HostStats {
p.Sent, p.Recv = num(f[1]), num(f[2])
}
s.PeerList = append(s.PeerList, p)
case "swarm": // after the peer lines
var x struct {
Peers []struct {
Peer, Latency string
Direction int
}
}
_ = json.Unmarshal([]byte(v), &x)
for _, sp := range x.Peers {
for i := range s.PeerList {
if s.PeerList[i].ID == sp.Peer {
s.PeerList[i].Latency, _ = time.ParseDuration(sp.Latency)
s.PeerList[i].Inbound = sp.Direction == 1
}
}
}
case "mirror":
s.Mirror = v
case "root":
+7 -2
View File
@@ -1,6 +1,9 @@
package deploy
import "testing"
import (
"testing"
"time"
)
func TestParseHostStats(t *testing.T) {
s := parseHostStats(`installed yes
@@ -49,12 +52,14 @@ active active
self /ip4/10.0.0.5/tcp/4001/p2p/me /ip4/2.176.0.9/tcp/4001/p2p/me /dns4/mirror.example/tcp/4001/p2p/me
peer /ip4/5.160.0.1/tcp/4001/p2p/12D3a 300 9000
peer /ip6/2a01::1/tcp/4001/p2p/12D3b
swarm {"Peers":[{"Addr":"/ip4/5.160.0.1/tcp/4001","Peer":"12D3a","Latency":"41.5ms","Muxer":"","Direction":1,"Streams":null}]}
`)
if len(s.Self) != 1 || s.Self[0].String() != "2.176.0.9" || len(s.selfNames) != 1 || s.selfNames[0] != "mirror.example" {
t.Fatalf("self %v names %v", s.Self, s.selfNames)
}
if len(s.PeerList) != 2 || s.PeerList[0].ID != "12D3a" || s.PeerList[0].Sent != 300 || s.PeerList[0].Recv != 9000 ||
s.PeerList[1].Addr.String() != "2a01::1" || s.PeerList[1].Sent != 0 {
s.PeerList[1].Addr.String() != "2a01::1" || s.PeerList[1].Sent != 0 ||
s.PeerList[0].Latency != 41500*time.Microsecond || !s.PeerList[0].Inbound || s.PeerList[1].Inbound {
t.Fatalf("%+v", s.PeerList)
}
}