Skip to content

Commit 53cc470

Browse files
committed
api: add ping info to handlers
1 parent 3665103 commit 53cc470

7 files changed

Lines changed: 34 additions & 7 deletions

File tree

api/debug.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (h *Handler) GetP2pDebugInfo(c echo.Context) (err error) {
5252
Total: makeBandwidthInfo(h.p2p.NetworkStats()),
5353
ByProtocol: bandwidthByProtocol,
5454
},
55+
KnownPeers: h.getKnownPeers(),
5556
}
5657

5758
return c.JSONPretty(http.StatusOK, debugInfo, " ")

api/peers.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"sort"
66
"strings"
77

8+
"github.com/labstack/echo/v4"
9+
"github.com/libp2p/go-libp2p/core/peer"
10+
811
"github.com/anywherelan/awl/awldns"
912
"github.com/anywherelan/awl/config"
1013
"github.com/anywherelan/awl/entity"
11-
"github.com/labstack/echo/v4"
12-
"github.com/libp2p/go-libp2p/core/peer"
1314
)
1415

1516
const ErrorPeerAliasIsNotUniq = "peer name is not unique"
@@ -21,6 +22,11 @@ const ErrorPeerAliasIsNotUniq = "peer name is not unique"
2122
// @Success 200 {array} entity.KnownPeersResponse
2223
// @Router /peers/get_known [GET]
2324
func (h *Handler) GetKnownPeers(c echo.Context) (err error) {
25+
result := h.getKnownPeers()
26+
return c.JSON(http.StatusOK, result)
27+
}
28+
29+
func (h *Handler) getKnownPeers() []entity.KnownPeersResponse {
2430
h.conf.RLock()
2531
result := make([]entity.KnownPeersResponse, 0, len(h.conf.KnownPeers))
2632
peers := make([]string, 0, len(h.conf.KnownPeers))
@@ -52,6 +58,7 @@ func (h *Handler) GetKnownPeers(c echo.Context) (err error) {
5258
Connections: h.p2p.PeerConnectionsInfo(id),
5359
NetworkStats: netStats,
5460
NetworkStatsInIECUnits: getStatsInIECUnits(netStats),
61+
Ping: h.p2p.GetPeerLatency(id),
5562
}
5663
result = append(result, kpr)
5764
}
@@ -61,7 +68,7 @@ func (h *Handler) GetKnownPeers(c echo.Context) (err error) {
6168
return result[i].Connected && !result[j].Connected
6269
})
6370

64-
return c.JSON(http.StatusOK, result)
71+
return result
6572
}
6673

6774
// @Tags Peers

application_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ func TestMakeFriends(t *testing.T) {
5656
peer2 := ts.newTestPeer(false)
5757

5858
ts.makeFriends(peer2, peer1)
59+
60+
p1Ping := peer1.app.P2p.GetPeerLatency(peer2.app.P2p.PeerID())
61+
ts.NotEmpty(p1Ping)
62+
p2Ping := peer2.app.P2p.GetPeerLatency(peer1.app.P2p.PeerID())
63+
ts.NotEmpty(p2Ping)
5964
}
6065

6166
func TestRemovePeer(t *testing.T) {

entity/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type (
6262
Connections []p2p.ConnectionInfo
6363
NetworkStats metrics.Stats
6464
NetworkStatsInIECUnits StatsInUnits
65+
Ping time.Duration `swaggertype:"primitive,integer"`
6566
}
6667

6768
PeerInfo struct {
@@ -114,6 +115,7 @@ type (
114115
DHT DhtDebugInfo
115116
Connections ConnectionsDebugInfo
116117
Bandwidth BandwidthDebugInfo
118+
KnownPeers []KnownPeersResponse
117119
}
118120

119121
GeneralDebugInfo struct {

p2p/metrics.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ type ConnectionInfo struct {
4242
Direction string
4343
Opened time.Time
4444
Transient bool
45-
Latency Duration
4645
}
4746

4847
type BootstrapPeerDebugInfo struct {
4948
Error string `json:",omitempty"`
5049
Connections []string `json:",omitempty"`
51-
Latency Duration `json:",omitempty"`
50+
Ping Duration `json:",omitempty"`
5251
}
5352

5453
func (p *P2p) Uptime() time.Duration {
@@ -79,7 +78,6 @@ func (p *P2p) PeerConnectionsInfo(peerID peer.ID) []ConnectionInfo {
7978
info.Direction = strings.ToLower(stat.Direction.String())
8079
info.Opened = stat.Opened
8180
info.Transient = stat.Limited
82-
info.Latency = Duration(p.host.Peerstore().LatencyEWMA(peerID))
8381
infos = append(infos, info)
8482
}
8583
return infos

p2p/p2p.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ func (p *P2p) UnprotectPeer(id peer.ID) {
280280
p.host.ConnManager().Unprotect(id, protectedPeerTag)
281281
}
282282

283+
func (p *P2p) RecordPeerLatency(id peer.ID, rtt time.Duration) {
284+
p.host.Peerstore().RecordLatency(id, rtt)
285+
}
286+
287+
func (p *P2p) GetPeerLatency(id peer.ID) time.Duration {
288+
return p.host.Peerstore().LatencyEWMA(id)
289+
}
290+
283291
func (p *P2p) SubscribeConnectionEvents(onConnected, onDisconnected func(network.Network, network.Conn)) {
284292
notifyBundle := &network.NotifyBundle{
285293
ConnectedF: onConnected,
@@ -385,7 +393,7 @@ func (p *P2p) connectToKnownPeers(ctx context.Context, timeout time.Duration, pe
385393
if err == nil {
386394
p.pingPeer(ctx, peerAddr.ID)
387395
}
388-
info.Latency = Duration(p.host.Peerstore().LatencyEWMA(peerAddr.ID))
396+
info.Ping = Duration(p.host.Peerstore().LatencyEWMA(peerAddr.ID))
389397

390398
mu.Lock()
391399
bootstrapsInfo[peerAddr.ID.String()] = info
@@ -404,6 +412,7 @@ func (p *P2p) pingPeer(ctx context.Context, peerID peer.ID) {
404412
ctx, cancel := context.WithCancel(ctx)
405413
defer cancel()
406414

415+
// TODO: also calc jitter
407416
ch := ping.Ping(ctx, p.host, peerID)
408417
pingCount := 0
409418
for range ch {

service/auth_status.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type P2p interface {
3131
NewStreamWithDedicatedConn(ctx context.Context, id peer.ID, proto libp2pProtocol.ID) (network.Stream, error)
3232
SubscribeConnectionEvents(onConnected, onDisconnected func(network.Network, network.Conn))
3333
ProtectPeer(id peer.ID)
34+
RecordPeerLatency(id peer.ID, rtt time.Duration)
3435
}
3536

3637
type AuthStatus struct {
@@ -125,6 +126,7 @@ func (s *AuthStatus) ExchangeNewStatusInfo(ctx context.Context, remotePeerID pee
125126

126127
_, isBlocked := s.conf.GetBlockedPeer(remotePeerID.String())
127128
myPeerInfo := s.createPeerInfo(knownPeer, s.conf.P2pNode.Name, isBlocked)
129+
timeStarted := time.Now()
128130
err = protocol.SendStatus(stream, myPeerInfo)
129131
if err != nil {
130132
return fmt.Errorf("sending status info: %v", err)
@@ -134,6 +136,7 @@ func (s *AuthStatus) ExchangeNewStatusInfo(ctx context.Context, remotePeerID pee
134136
if err != nil {
135137
return fmt.Errorf("receiving status info: %v", err)
136138
}
139+
s.p2p.RecordPeerLatency(remotePeerID, time.Since(timeStarted))
137140

138141
s.logger.Infof("successfully exchanged status info (outbound) with %s (%s)", knownPeer.DisplayName(), remotePeerID.String())
139142
if isBlocked {
@@ -270,6 +273,7 @@ func (s *AuthStatus) SendAuthRequest(ctx context.Context, peerID peer.ID, req pr
270273
_ = stream.Close()
271274
}()
272275

276+
timeStarted := time.Now()
273277
err = protocol.SendAuth(stream, req)
274278
if err != nil {
275279
return fmt.Errorf("sending auth: %v", err)
@@ -279,6 +283,7 @@ func (s *AuthStatus) SendAuthRequest(ctx context.Context, peerID peer.ID, req pr
279283
if err != nil {
280284
return fmt.Errorf("receiving auth response from %s: %v", peerID, err)
281285
}
286+
s.p2p.RecordPeerLatency(peerID, time.Since(timeStarted))
282287

283288
if authResponse.Confirmed || authResponse.Declined {
284289
s.authsLock.Lock()

0 commit comments

Comments
 (0)