github.com/aergoio/aergo@v1.3.1/p2p/metric/peermetric.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package metric
     7  
     8  import (
     9  	"github.com/aergoio/aergo/p2p/p2pcommon"
    10  	"github.com/aergoio/aergo/types"
    11  	"sync/atomic"
    12  	"time"
    13  )
    14  
    15  type PeerMetric struct {
    16  	mm     MetricsManager
    17  	PeerID types.PeerID
    18  	seq    uint32
    19  
    20  	Since    time.Time
    21  	totalIn  int64
    22  	totalOut int64
    23  
    24  	InMetric  DataMetric
    25  	OutMetric DataMetric
    26  }
    27  
    28  var _ p2pcommon.MsgIOListener = (*PeerMetric)(nil)
    29  
    30  func (m *PeerMetric) OnRead(protocol p2pcommon.SubProtocol, read int) {
    31  	atomic.AddInt64(&m.totalIn, int64(read))
    32  	m.InMetric.AddBytes(read)
    33  }
    34  
    35  func (m *PeerMetric) OnWrite(protocol p2pcommon.SubProtocol, write int) {
    36  	atomic.AddInt64(&m.totalOut, int64(write))
    37  	m.OutMetric.AddBytes(write)
    38  }
    39  
    40  func (m *PeerMetric) TotalIn() int64 {
    41  	return atomic.LoadInt64(&m.totalIn)
    42  }
    43  
    44  func (m *PeerMetric) TotalOut() int64 {
    45  	return atomic.LoadInt64(&m.totalOut)
    46  }
    47  
    48  // Deprecated
    49  func (m *PeerMetric) InputAdded(added int) {
    50  	atomic.AddInt64(&m.totalIn, int64(added))
    51  }
    52  
    53  // Deprecated
    54  func (m *PeerMetric) OutputAdded(added int) {
    55  	atomic.AddInt64(&m.totalOut, int64(added))
    56  }