github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/p2p/metrics.go (about) 1 // Copyright 2015 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 // Contains the meters and timers used by the networking layer. 18 19 package p2p 20 21 import ( 22 "net" 23 24 "github.com/kisexp/xdchain/metrics" 25 ) 26 27 const ( 28 ingressMeterName = "p2p/ingress" 29 egressMeterName = "p2p/egress" 30 ) 31 32 var ( 33 ingressConnectMeter = metrics.NewRegisteredMeter("p2p/serves", nil) 34 ingressTrafficMeter = metrics.NewRegisteredMeter(ingressMeterName, nil) 35 egressConnectMeter = metrics.NewRegisteredMeter("p2p/dials", nil) 36 egressTrafficMeter = metrics.NewRegisteredMeter(egressMeterName, nil) 37 activePeerGauge = metrics.NewRegisteredGauge("p2p/peers", nil) 38 ) 39 40 // meteredConn is a wrapper around a net.Conn that meters both the 41 // inbound and outbound network traffic. 42 type meteredConn struct { 43 net.Conn 44 } 45 46 // newMeteredConn creates a new metered connection, bumps the ingress or egress 47 // connection meter and also increases the metered peer count. If the metrics 48 // system is disabled, function returns the original connection. 49 func newMeteredConn(conn net.Conn, ingress bool, addr *net.TCPAddr) net.Conn { 50 // Short circuit if metrics are disabled 51 if !metrics.Enabled { 52 return conn 53 } 54 // Bump the connection counters and wrap the connection 55 if ingress { 56 ingressConnectMeter.Mark(1) 57 } else { 58 egressConnectMeter.Mark(1) 59 } 60 activePeerGauge.Inc(1) 61 return &meteredConn{Conn: conn} 62 } 63 64 // Read delegates a network read to the underlying connection, bumping the common 65 // and the peer ingress traffic meters along the way. 66 func (c *meteredConn) Read(b []byte) (n int, err error) { 67 n, err = c.Conn.Read(b) 68 ingressTrafficMeter.Mark(int64(n)) 69 return n, err 70 } 71 72 // Write delegates a network write to the underlying connection, bumping the common 73 // and the peer egress traffic meters along the way. 74 func (c *meteredConn) Write(b []byte) (n int, err error) { 75 n, err = c.Conn.Write(b) 76 egressTrafficMeter.Mark(int64(n)) 77 return n, err 78 } 79 80 // Close delegates a close operation to the underlying connection, unregisters 81 // the peer from the traffic registries and emits close event. 82 func (c *meteredConn) Close() error { 83 err := c.Conn.Close() 84 if err == nil { 85 activePeerGauge.Dec(1) 86 } 87 return err 88 }