@ -18,6 +18,7 @@ package discover
import (
import (
"fmt"
"fmt"
"net"
"net/netip"
"net/netip"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/metrics"
@ -47,7 +48,7 @@ func init() {
// meteredUdpConn is a wrapper around a net.UDPConn that meters both the
// meteredUdpConn is a wrapper around a net.UDPConn that meters both the
// inbound and outbound network traffic.
// inbound and outbound network traffic.
type meteredUdpConn struct {
type meteredUdpConn struct {
UDPConn
udpConn UDPConn
}
}
func newMeteredConn ( conn UDPConn ) UDPConn {
func newMeteredConn ( conn UDPConn ) UDPConn {
@ -55,19 +56,27 @@ func newMeteredConn(conn UDPConn) UDPConn {
if ! metrics . Enabled {
if ! metrics . Enabled {
return conn
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.
// 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 ) {
func ( c * meteredUdpConn ) ReadFromUDPAddrPort ( b [ ] byte ) ( n int , addr netip . AddrPort , err error ) {
n , addr , err = c . UDPConn . ReadFromUDPAddrPort ( b )
n , addr , err = c . udp Conn. ReadFromUDPAddrPort ( b )
ingressTrafficMeter . Mark ( int64 ( n ) )
ingressTrafficMeter . Mark ( int64 ( n ) )
return n , addr , err
return n , addr , err
}
}
// WriteToUDP delegates a network write to the underlying connection, bumping the udp egress traffic meter along the way.
// WriteToUDPAddrPort 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 ) {
func ( c * meteredUdpConn ) WriteToUDPAddrPort ( b [ ] byte , addr netip . AddrPort ) ( n int , err error ) {
n , err = c . UDP Conn. WriteToUDPAddrPort ( b , addr )
n , err = c . udp Conn. WriteToUDPAddrPort ( b , addr )
egressTrafficMeter . Mark ( int64 ( n ) )
egressTrafficMeter . Mark ( int64 ( n ) )
return n , err
return n , err
}
}