github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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.NewMeter("p2p/InboundConnects")
    29  	ingressTrafficMeter = metrics.NewMeter("p2p/InboundTraffic")
    30  	egressConnectMeter  = metrics.NewMeter("p2p/OutboundConnects")
    31  	egressTrafficMeter  = metrics.NewMeter("p2p/OutboundTraffic")
    32  )
    33  
    34  // meteredConn is a wrapper around a network TCP connection that meters both the
    35  // inbound and outbound network traffic.
    36  type meteredConn struct {
    37  	*net.TCPConn // Network connection to wrap with metering
    38  }
    39  
    40  // newMeteredConn creates a new metered connection, also bumping the ingress or
    41  // egress connection meter. If the metrics system is disabled, this function
    42  // returns the original object.
    43  func newMeteredConn(conn net.Conn, ingress bool) net.Conn {
    44  	// Short circuit if metrics are disabled
    45  	if !metrics.Enabled {
    46  		return conn
    47  	}
    48  	// Otherwise bump the connection counters and wrap the connection
    49  	if ingress {
    50  		ingressConnectMeter.Mark(1)
    51  	} else {
    52  		egressConnectMeter.Mark(1)
    53  	}
    54  	return &meteredConn{conn.(*net.TCPConn)}
    55  }
    56  
    57  // Read delegates a network read to the underlying connection, bumping the ingress
    58  // traffic meter along the way.
    59  func (c *meteredConn) Read(b []byte) (n int, err error) {
    60  	n, err = c.TCPConn.Read(b)
    61  	ingressTrafficMeter.Mark(int64(n))
    62  	return
    63  }
    64  
    65  // Write delegates a network write to the underlying connection, bumping the
    66  // egress traffic meter along the way.
    67  func (c *meteredConn) Write(b []byte) (n int, err error) {
    68  	n, err = c.TCPConn.Write(b)
    69  	egressTrafficMeter.Mark(int64(n))
    70  	return
    71  }