github.com/quinndk/ethereum_read@v0.0.0-20181211143958-29c55eec3237/go-ethereum-master_read/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/ethereum/go-ethereum/metrics" 25 ) 26 27 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) 32 ) 33 34 // meteredConn is a wrapper around a net.Conn that meters both the 35 // inbound and outbound network traffic. 36 // 计量连接,用来计量入站和出站网络流量 37 type meteredConn struct { 38 net.Conn // Network connection to wrap with metering 39 } 40 41 // newMeteredConn creates a new metered connection, also bumping the ingress or 42 // egress connection meter. If the metrics system is disabled, this function 43 // returns the original object. 44 // 新建一个计量连接 45 func newMeteredConn(conn net.Conn, ingress bool) net.Conn { 46 // Short circuit if metrics are disabled 47 if !metrics.Enabled { 48 return conn 49 } 50 // Otherwise bump the connection counters and wrap the connection 51 if ingress { 52 ingressConnectMeter.Mark(1) 53 } else { 54 egressConnectMeter.Mark(1) 55 } 56 return &meteredConn{Conn: conn} 57 } 58 59 // Read delegates a network read to the underlying connection, bumping the ingress 60 // traffic meter along the way. 61 func (c *meteredConn) Read(b []byte) (n int, err error) { 62 n, err = c.Conn.Read(b) 63 ingressTrafficMeter.Mark(int64(n)) 64 return 65 } 66 67 // Write delegates a network write to the underlying connection, bumping the 68 // egress traffic meter along the way. 69 func (c *meteredConn) Write(b []byte) (n int, err error) { 70 n, err = c.Conn.Write(b) 71 egressTrafficMeter.Mark(int64(n)) 72 return 73 }