Skip to content

Commit 16e4d0e

Browse files
kurkomisifjl
authored andcommitted
p2p: meter peer traffic, emit metered peer events (#17695)
This change extends the peer metrics collection: - traces the life-cycle of the peers - meters the peer traffic separately for every peer - creates event feed for the peer events - emits the peer events
1 parent 331fa6d commit 16e4d0e

3 files changed

Lines changed: 187 additions & 18 deletions

File tree

‎p2p/dial.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ func (t *dialTask) dial(srv *Server, dest *enode.Node) error {
350350
if err != nil {
351351
return &dialError{err}
352352
}
353-
mfd := newMeteredConn(fd, false)
353+
mfd := newMeteredConn(fd, false, dest.IP())
354354
return srv.SetupConn(mfd, t.flags, dest)
355355
}
356356

‎p2p/metrics.go‎

Lines changed: 178 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,215 @@
1919
package p2p
2020

2121
import (
22+
"fmt"
2223
"net"
24+
"sync"
25+
"sync/atomic"
26+
"time"
2327

28+
"github.com/ethereum/go-ethereum/p2p/enode"
29+
30+
"github.com/ethereum/go-ethereum/event"
31+
"github.com/ethereum/go-ethereum/log"
2432
"github.com/ethereum/go-ethereum/metrics"
2533
)
2634

35+
const (
36+
MetricsInboundConnects = "p2p/InboundConnects" // Name for the registered inbound connects meter
37+
MetricsInboundTraffic = "p2p/InboundTraffic" // Name for the registered inbound traffic meter
38+
MetricsOutboundConnects = "p2p/OutboundConnects" // Name for the registered outbound connects meter
39+
MetricsOutboundTraffic = "p2p/OutboundTraffic" // Name for the registered outbound traffic meter
40+
41+
MeteredPeerLimit = 1024 // This amount of peers are individually metered
42+
)
43+
2744
var (
28-
ingressConnectMeter = metrics.NewRegisteredMeter("p2p/InboundConnects", nil)
29-
ingressTrafficMeter = metrics.NewRegisteredMeter("p2p/InboundTraffic", nil)
30-
egressConnectMeter = metrics.NewRegisteredMeter("p2p/OutboundConnects", nil)
31-
egressTrafficMeter = metrics.NewRegisteredMeter("p2p/OutboundTraffic", nil)
45+
ingressConnectMeter = metrics.NewRegisteredMeter(MetricsInboundConnects, nil) // Meter counting the ingress connections
46+
ingressTrafficMeter = metrics.NewRegisteredMeter(MetricsInboundTraffic, nil) // Meter metering the cumulative ingress traffic
47+
egressConnectMeter = metrics.NewRegisteredMeter(MetricsOutboundConnects, nil) // Meter counting the egress connections
48+
egressTrafficMeter = metrics.NewRegisteredMeter(MetricsOutboundTraffic, nil) // Meter metering the cumulative egress traffic
49+
50+
PeerIngressRegistry = metrics.NewPrefixedChildRegistry(metrics.DefaultRegistry, MetricsInboundTraffic+"/") // Registry containing the peer ingress
51+
PeerEgressRegistry = metrics.NewPrefixedChildRegistry(metrics.DefaultRegistry, MetricsOutboundTraffic+"/") // Registry containing the peer egress
52+
53+
meteredPeerFeed event.Feed // Event feed for peer metrics
54+
meteredPeerCount int32 // Actually stored peer connection count
3255
)
3356

57+
// MeteredPeerEventType is the type of peer events emitted by a metered connection.
58+
type MeteredPeerEventType int
59+
60+
const (
61+
// PeerConnected is the type of event emitted when a peer successfully
62+
// made the handshake.
63+
PeerConnected MeteredPeerEventType = iota
64+
65+
// PeerDisconnected is the type of event emitted when a peer disconnects.
66+
PeerDisconnected
67+
68+
// PeerHandshakeFailed is the type of event emitted when a peer fails to
69+
// make the handshake or disconnects before the handshake.
70+
PeerHandshakeFailed
71+
)
72+
73+
// MeteredPeerEvent is an event emitted when peers connect or disconnect.
74+
type MeteredPeerEvent struct {
75+
Type MeteredPeerEventType // Type of peer event
76+
IP net.IP // IP address of the peer
77+
ID string // NodeID of the peer
78+
Elapsed time.Duration // Time elapsed between the connection and the handshake/disconnection
79+
Ingress uint64 // Ingress count at the moment of the event
80+
Egress uint64 // Egress count at the moment of the event
81+
}
82+
83+
// SubscribeMeteredPeerEvent registers a subscription for peer life-cycle events
84+
// if metrics collection is enabled.
85+
func SubscribeMeteredPeerEvent(ch chan<- MeteredPeerEvent) event.Subscription {
86+
return meteredPeerFeed.Subscribe(ch)
87+
}
88+
3489
// meteredConn is a wrapper around a net.Conn that meters both the
3590
// inbound and outbound network traffic.
3691
type meteredConn struct {
3792
net.Conn // Network connection to wrap with metering
93+
94+
connected time.Time // Connection time of the peer
95+
ip net.IP // IP address of the peer
96+
id string // NodeID of the peer
97+
98+
// trafficMetered denotes if the peer is registered in the traffic registries.
99+
// Its value is true if the metered peer count doesn't reach the limit in the
100+
// moment of the peer's connection.
101+
trafficMetered bool
102+
ingressMeter metrics.Meter // Meter for the read bytes of the peer
103+
egressMeter metrics.Meter // Meter for the written bytes of the peer
104+
105+
lock sync.RWMutex // Lock protecting the metered connection's internals
38106
}
39107

40-
// newMeteredConn creates a new metered connection, also bumping the ingress or
41-
// egress connection meter. If the metrics system is disabled, this function
42-
// returns the original object.
43-
func newMeteredConn(conn net.Conn, ingress bool) net.Conn {
108+
// newMeteredConn creates a new metered connection, bumps the ingress or egress
109+
// connection meter and also increases the metered peer count. If the metrics
110+
// system is disabled or the IP address is unspecified, this function returns
111+
// the original object.
112+
func newMeteredConn(conn net.Conn, ingress bool, ip net.IP) net.Conn {
44113
// Short circuit if metrics are disabled
45114
if !metrics.Enabled {
46115
return conn
47116
}
48-
// Otherwise bump the connection counters and wrap the connection
117+
if ip.IsUnspecified() {
118+
log.Warn("Peer IP is unspecified")
119+
return conn
120+
}
121+
// Bump the connection counters and wrap the connection
49122
if ingress {
50123
ingressConnectMeter.Mark(1)
51124
} else {
52125
egressConnectMeter.Mark(1)
53126
}
54-
return &meteredConn{Conn: conn}
127+
return &meteredConn{
128+
Conn: conn,
129+
ip: ip,
130+
connected: time.Now(),
131+
}
55132
}
56133

57-
// Read delegates a network read to the underlying connection, bumping the ingress
58-
// traffic meter along the way.
134+
// Read delegates a network read to the underlying connection, bumping the common
135+
// and the peer ingress traffic meters along the way.
59136
func (c *meteredConn) Read(b []byte) (n int, err error) {
60137
n, err = c.Conn.Read(b)
61138
ingressTrafficMeter.Mark(int64(n))
62-
return
139+
c.lock.RLock()
140+
if c.trafficMetered {
141+
c.ingressMeter.Mark(int64(n))
142+
}
143+
c.lock.RUnlock()
144+
return n, err
63145
}
64146

65-
// Write delegates a network write to the underlying connection, bumping the
66-
// egress traffic meter along the way.
147+
// Write delegates a network write to the underlying connection, bumping the common
148+
// and the peer egress traffic meters along the way.
67149
func (c *meteredConn) Write(b []byte) (n int, err error) {
68150
n, err = c.Conn.Write(b)
69151
egressTrafficMeter.Mark(int64(n))
70-
return
152+
c.lock.RLock()
153+
if c.trafficMetered {
154+
c.egressMeter.Mark(int64(n))
155+
}
156+
c.lock.RUnlock()
157+
return n, err
158+
}
159+
160+
// handshakeDone is called when a peer handshake is done. Registers the peer to
161+
// the ingress and the egress traffic registries using the peer's IP and node ID,
162+
// also emits connect event.
163+
func (c *meteredConn) handshakeDone(nodeID enode.ID) {
164+
id := nodeID.String()
165+
if atomic.AddInt32(&meteredPeerCount, 1) >= MeteredPeerLimit {
166+
// Don't register the peer in the traffic registries.
167+
atomic.AddInt32(&meteredPeerCount, -1)
168+
c.lock.Lock()
169+
c.id, c.trafficMetered = id, false
170+
c.lock.Unlock()
171+
log.Warn("Metered peer count reached the limit")
172+
} else {
173+
key := fmt.Sprintf("%s/%s", c.ip, id)
174+
c.lock.Lock()
175+
c.id, c.trafficMetered = id, true
176+
c.ingressMeter = metrics.NewRegisteredMeter(key, PeerIngressRegistry)
177+
c.egressMeter = metrics.NewRegisteredMeter(key, PeerEgressRegistry)
178+
c.lock.Unlock()
179+
}
180+
meteredPeerFeed.Send(MeteredPeerEvent{
181+
Type: PeerConnected,
182+
IP: c.ip,
183+
ID: id,
184+
Elapsed: time.Since(c.connected),
185+
})
186+
}
187+
188+
// Close delegates a close operation to the underlying connection, unregisters
189+
// the peer from the traffic registries and emits close event.
190+
func (c *meteredConn) Close() error {
191+
err := c.Conn.Close()
192+
c.lock.RLock()
193+
if c.id == "" {
194+
// If the peer disconnects before the handshake.
195+
c.lock.RUnlock()
196+
meteredPeerFeed.Send(MeteredPeerEvent{
197+
Type: PeerHandshakeFailed,
198+
IP: c.ip,
199+
Elapsed: time.Since(c.connected),
200+
})
201+
return err
202+
}
203+
id := c.id
204+
if !c.trafficMetered {
205+
// If the peer isn't registered in the traffic registries.
206+
c.lock.RUnlock()
207+
meteredPeerFeed.Send(MeteredPeerEvent{
208+
Type: PeerDisconnected,
209+
IP: c.ip,
210+
ID: id,
211+
})
212+
return err
213+
}
214+
ingress, egress := uint64(c.ingressMeter.Count()), uint64(c.egressMeter.Count())
215+
c.lock.RUnlock()
216+
217+
// Decrement the metered peer count
218+
atomic.AddInt32(&meteredPeerCount, -1)
219+
220+
// Unregister the peer from the traffic registries
221+
key := fmt.Sprintf("%s/%s", c.ip, id)
222+
PeerIngressRegistry.Unregister(key)
223+
PeerEgressRegistry.Unregister(key)
224+
225+
meteredPeerFeed.Send(MeteredPeerEvent{
226+
Type: PeerDisconnected,
227+
IP: c.ip,
228+
ID: id,
229+
Ingress: ingress,
230+
Egress: egress,
231+
})
232+
return err
71233
}

‎p2p/server.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,11 @@ func (srv *Server) listenLoop() {
864864
}
865865
}
866866

867-
fd = newMeteredConn(fd, true)
867+
var ip net.IP
868+
if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok {
869+
ip = tcp.IP
870+
}
871+
fd = newMeteredConn(fd, true, ip)
868872
srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr())
869873
go func() {
870874
srv.SetupConn(fd, inboundConn, nil)
@@ -917,6 +921,9 @@ func (srv *Server) setupConn(c *conn, flags connFlag, dialDest *enode.Node) erro
917921
} else {
918922
c.node = nodeFromConn(remotePubkey, c.fd)
919923
}
924+
if conn, ok := c.fd.(*meteredConn); ok {
925+
conn.handshakeDone(c.node.ID())
926+
}
920927
clog := srv.log.New("id", c.node.ID(), "addr", c.fd.RemoteAddr(), "conn", c.flags)
921928
err = srv.checkpoint(c, srv.posthandshake)
922929
if err != nil {

0 commit comments

Comments
 (0)