commit
e190b7fa44
36
peer/peer.go
36
peer/peer.go
@ -394,10 +394,14 @@ type HostToNetAddrFunc func(host string, port uint16,
|
||||
// of specific types that typically require common special handling are
|
||||
// provided as a convenience.
|
||||
type Peer struct {
|
||||
started int32
|
||||
connected int32
|
||||
disconnect int32 // only to be used atomically
|
||||
conn net.Conn
|
||||
// The following variables must only be used atomically
|
||||
started int32
|
||||
connected int32
|
||||
disconnect int32
|
||||
bytesReceived uint64
|
||||
bytesSent uint64
|
||||
|
||||
conn net.Conn
|
||||
|
||||
// These fields are set at creation time and never modified, so they are
|
||||
// safe to read from concurrently without a mutex.
|
||||
@ -430,8 +434,6 @@ type Peer struct {
|
||||
timeConnected time.Time
|
||||
lastSend time.Time
|
||||
lastRecv time.Time
|
||||
bytesReceived uint64
|
||||
bytesSent uint64
|
||||
startingHeight int32
|
||||
lastBlock int32
|
||||
lastAnnouncedBlock *chainhash.Hash
|
||||
@ -512,8 +514,8 @@ func (p *Peer) StatsSnapshot() *StatsSnap {
|
||||
Services: services,
|
||||
LastSend: p.lastSend,
|
||||
LastRecv: p.lastRecv,
|
||||
BytesSent: p.bytesSent,
|
||||
BytesRecv: p.bytesReceived,
|
||||
BytesSent: atomic.LoadUint64(&p.bytesSent),
|
||||
BytesRecv: atomic.LoadUint64(&p.bytesReceived),
|
||||
ConnTime: p.timeConnected,
|
||||
TimeOffset: p.timeOffset,
|
||||
Version: protocolVersion,
|
||||
@ -688,20 +690,14 @@ func (p *Peer) LastRecv() time.Time {
|
||||
//
|
||||
// This function is safe for concurrent access.
|
||||
func (p *Peer) BytesSent() uint64 {
|
||||
p.statsMtx.RLock()
|
||||
defer p.statsMtx.RUnlock()
|
||||
|
||||
return p.bytesSent
|
||||
return atomic.LoadUint64(&p.bytesSent)
|
||||
}
|
||||
|
||||
// BytesReceived returns the total number of bytes received by the peer.
|
||||
//
|
||||
// This function is safe for concurrent access.
|
||||
func (p *Peer) BytesReceived() uint64 {
|
||||
p.statsMtx.RLock()
|
||||
defer p.statsMtx.RUnlock()
|
||||
|
||||
return p.bytesReceived
|
||||
return atomic.LoadUint64(&p.bytesReceived)
|
||||
}
|
||||
|
||||
// TimeConnected returns the time at which the peer connected.
|
||||
@ -1104,9 +1100,7 @@ func (p *Peer) handlePongMsg(msg *wire.MsgPong) {
|
||||
func (p *Peer) readMessage() (wire.Message, []byte, error) {
|
||||
n, msg, buf, err := wire.ReadMessageN(p.conn, p.ProtocolVersion(),
|
||||
p.cfg.ChainParams.Net)
|
||||
p.statsMtx.Lock()
|
||||
p.bytesReceived += uint64(n)
|
||||
p.statsMtx.Unlock()
|
||||
atomic.AddUint64(&p.bytesReceived, uint64(n))
|
||||
if p.cfg.Listeners.OnRead != nil {
|
||||
p.cfg.Listeners.OnRead(p, n, msg, err)
|
||||
}
|
||||
@ -1181,9 +1175,7 @@ func (p *Peer) writeMessage(msg wire.Message) error {
|
||||
// Write the message to the peer.
|
||||
n, err := wire.WriteMessageN(p.conn, msg, p.ProtocolVersion(),
|
||||
p.cfg.ChainParams.Net)
|
||||
p.statsMtx.Lock()
|
||||
p.bytesSent += uint64(n)
|
||||
p.statsMtx.Unlock()
|
||||
atomic.AddUint64(&p.bytesSent, uint64(n))
|
||||
if p.cfg.Listeners.OnWrite != nil {
|
||||
p.cfg.Listeners.OnWrite(p, n, msg, err)
|
||||
}
|
||||
|
||||
29
server.go
29
server.go
@ -169,14 +169,15 @@ func (ps *peerState) forAllPeers(closure func(sp *serverPeer)) {
|
||||
// server provides a decred server for handling communications to and from
|
||||
// decred peers.
|
||||
type server struct {
|
||||
// The following variables must only be used atomically.
|
||||
started int32
|
||||
shutdown int32
|
||||
shutdownSched int32
|
||||
bytesReceived uint64 // Total bytes received from all peers since start.
|
||||
bytesSent uint64 // Total bytes sent by all peers since start.
|
||||
|
||||
listeners []net.Listener
|
||||
chainParams *chaincfg.Params
|
||||
started int32 // atomic
|
||||
shutdown int32 // atomic
|
||||
shutdownSched int32 // atomic
|
||||
bytesMutex sync.Mutex // For the following two fields.
|
||||
bytesReceived uint64 // Total bytes received from all peers since start.
|
||||
bytesSent uint64 // Total bytes sent by all peers since start.
|
||||
addrManager *addrmgr.AddrManager
|
||||
sigCache *txscript.SigCache
|
||||
rpcServer *rpcServer
|
||||
@ -2010,28 +2011,20 @@ func (s *server) ConnectNode(addr string, permanent bool) error {
|
||||
// AddBytesSent adds the passed number of bytes to the total bytes sent counter
|
||||
// for the server. It is safe for concurrent access.
|
||||
func (s *server) AddBytesSent(bytesSent uint64) {
|
||||
s.bytesMutex.Lock()
|
||||
defer s.bytesMutex.Unlock()
|
||||
|
||||
s.bytesSent += bytesSent
|
||||
atomic.AddUint64(&s.bytesSent, bytesSent)
|
||||
}
|
||||
|
||||
// AddBytesReceived adds the passed number of bytes to the total bytes received
|
||||
// counter for the server. It is safe for concurrent access.
|
||||
func (s *server) AddBytesReceived(bytesReceived uint64) {
|
||||
s.bytesMutex.Lock()
|
||||
defer s.bytesMutex.Unlock()
|
||||
|
||||
s.bytesReceived += bytesReceived
|
||||
atomic.AddUint64(&s.bytesReceived, bytesReceived)
|
||||
}
|
||||
|
||||
// NetTotals returns the sum of all bytes received and sent across the network
|
||||
// for all peers. It is safe for concurrent access.
|
||||
func (s *server) NetTotals() (uint64, uint64) {
|
||||
s.bytesMutex.Lock()
|
||||
defer s.bytesMutex.Unlock()
|
||||
|
||||
return s.bytesReceived, s.bytesSent
|
||||
return atomic.LoadUint64(&s.bytesReceived),
|
||||
atomic.LoadUint64(&s.bytesSent)
|
||||
}
|
||||
|
||||
// UpdatePeerHeights updates the heights of all peers who have have announced
|
||||
|
||||
Loading…
Reference in New Issue
Block a user