github.com/theQRL/go-zond@v0.1.1/p2p/discover/metrics.go (about) 1 // Copyright 2023 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package discover 18 19 import ( 20 "fmt" 21 "net" 22 23 "github.com/theQRL/go-zond/metrics" 24 ) 25 26 const ( 27 moduleName = "discover" 28 // ingressMeterName is the prefix of the per-packet inbound metrics. 29 ingressMeterName = moduleName + "/ingress" 30 31 // egressMeterName is the prefix of the per-packet outbound metrics. 32 egressMeterName = moduleName + "/egress" 33 ) 34 35 var ( 36 bucketsCounter []metrics.Counter 37 ingressTrafficMeter = metrics.NewRegisteredMeter(ingressMeterName, nil) 38 egressTrafficMeter = metrics.NewRegisteredMeter(egressMeterName, nil) 39 ) 40 41 func init() { 42 for i := 0; i < nBuckets; i++ { 43 bucketsCounter = append(bucketsCounter, metrics.NewRegisteredCounter(fmt.Sprintf("%s/bucket/%d/count", moduleName, i), nil)) 44 } 45 } 46 47 // meteredConn is a wrapper around a net.UDPConn that meters both the 48 // inbound and outbound network traffic. 49 type meteredUdpConn struct { 50 UDPConn 51 } 52 53 func newMeteredConn(conn UDPConn) UDPConn { 54 // Short circuit if metrics are disabled 55 if !metrics.Enabled { 56 return conn 57 } 58 return &meteredUdpConn{UDPConn: conn} 59 } 60 61 // Read delegates a network read to the underlying connection, bumping the udp ingress traffic meter along the way. 62 func (c *meteredUdpConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) { 63 n, addr, err = c.UDPConn.ReadFromUDP(b) 64 ingressTrafficMeter.Mark(int64(n)) 65 return n, addr, err 66 } 67 68 // Write delegates a network write to the underlying connection, bumping the udp egress traffic meter along the way. 69 func (c *meteredUdpConn) WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error) { 70 n, err = c.UDPConn.WriteToUDP(b, addr) 71 egressTrafficMeter.Mark(int64(n)) 72 return n, err 73 }