Skip to content

Commit

Permalink
p2p/discover: fix Write method in metered connection (ethereum#30355)
Browse files Browse the repository at this point in the history
`WriteToUDP` was never called, since `meteredUdpConn` exposed directly
all the methods from the underlying `UDPConn` interface.

This fixes the `discover/egress` metric never being updated.
  • Loading branch information
ngotchac authored and mask-pp committed Aug 30, 2024
1 parent 4b4aa39 commit 750519d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions p2p/discover/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package discover

import (
"fmt"
"net"
"net/netip"

"github.com/ethereum/go-ethereum/metrics"
Expand Down Expand Up @@ -47,27 +48,35 @@ func init() {
// meteredUdpConn is a wrapper around a net.UDPConn that meters both the
// inbound and outbound network traffic.
type meteredUdpConn struct {
UDPConn
udpConn UDPConn
}

func newMeteredConn(conn UDPConn) UDPConn {
// Short circuit if metrics are disabled
if !metrics.Enabled {
return conn
}
return &meteredUdpConn{UDPConn: conn}
return &meteredUdpConn{udpConn: conn}
}

func (c *meteredUdpConn) Close() error {
return c.udpConn.Close()
}

func (c *meteredUdpConn) LocalAddr() net.Addr {
return c.udpConn.LocalAddr()
}

// ReadFromUDPAddrPort delegates a network read to the underlying connection, bumping the udp ingress traffic meter along the way.
func (c *meteredUdpConn) ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error) {
n, addr, err = c.UDPConn.ReadFromUDPAddrPort(b)
n, addr, err = c.udpConn.ReadFromUDPAddrPort(b)
ingressTrafficMeter.Mark(int64(n))
return n, addr, err
}

// WriteToUDP delegates a network write to the underlying connection, bumping the udp egress traffic meter along the way.
func (c *meteredUdpConn) WriteToUDP(b []byte, addr netip.AddrPort) (n int, err error) {
n, err = c.UDPConn.WriteToUDPAddrPort(b, addr)
// WriteToUDPAddrPort delegates a network write to the underlying connection, bumping the udp egress traffic meter along the way.
func (c *meteredUdpConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (n int, err error) {
n, err = c.udpConn.WriteToUDPAddrPort(b, addr)
egressTrafficMeter.Mark(int64(n))
return n, err
}

0 comments on commit 750519d

Please sign in to comment.