Introduce a Stringer on peers.
Rather than having all of the various places that print peer figure out the direction and form the string, centralize it by implementing the Stringer interface on the peer.
This commit is contained in:
parent
f1e2de4f0c
commit
29dfa22086
3 changed files with 23 additions and 24 deletions
|
@ -137,8 +137,7 @@ func (b *blockManager) handleNewPeerMsg(peers *list.List, p *peer) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("[BMGR] New valid peer %s (%s)", p.addr,
|
log.Infof("[BMGR] New valid peer %s", p)
|
||||||
directionString(p.inbound))
|
|
||||||
|
|
||||||
// The peer is not a candidate for sync if it's not a full node.
|
// The peer is not a candidate for sync if it's not a full node.
|
||||||
if p.services&btcwire.SFNodeNetwork != btcwire.SFNodeNetwork {
|
if p.services&btcwire.SFNodeNetwork != btcwire.SFNodeNetwork {
|
||||||
|
@ -165,8 +164,7 @@ func (b *blockManager) handleDonePeerMsg(peers *list.List, p *peer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("[BMGR] Lost peer %s (%s)", p.addr,
|
log.Infof("[BMGR] Lost peer %s", p)
|
||||||
directionString(p.inbound))
|
|
||||||
|
|
||||||
// Attempt to find a new peer to sync from if the quitting peer is the
|
// Attempt to find a new peer to sync from if the quitting peer is the
|
||||||
// sync peer.
|
// sync peer.
|
||||||
|
|
15
peer.go
15
peer.go
|
@ -42,6 +42,15 @@ var userAgent = fmt.Sprintf("/btcd:%d.%d.%d/", appMajor, appMinor, appPatch)
|
||||||
// zeroHash is the zero value hash (all zeros). It is defined as a convenience.
|
// zeroHash is the zero value hash (all zeros). It is defined as a convenience.
|
||||||
var zeroHash btcwire.ShaHash
|
var zeroHash btcwire.ShaHash
|
||||||
|
|
||||||
|
// directionString is a helper function that returns a string that represents
|
||||||
|
// the direction of a connection (inbound or outbound).
|
||||||
|
func directionString(inbound bool) string {
|
||||||
|
if inbound {
|
||||||
|
return "inbound"
|
||||||
|
}
|
||||||
|
return "outbound"
|
||||||
|
}
|
||||||
|
|
||||||
// minUint32 is a helper function to return the minimum of two uint32s.
|
// minUint32 is a helper function to return the minimum of two uint32s.
|
||||||
// This avoids a math import and the need to cast to floats.
|
// This avoids a math import and the need to cast to floats.
|
||||||
func minUint32(a, b uint32) uint32 {
|
func minUint32(a, b uint32) uint32 {
|
||||||
|
@ -124,6 +133,12 @@ type peer struct {
|
||||||
quit chan bool
|
quit chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the peer's address and directionality as a human-readable
|
||||||
|
// string.
|
||||||
|
func (p *peer) String() string {
|
||||||
|
return fmt.Sprintf("%s (%s)", p.addr, directionString(p.inbound))
|
||||||
|
}
|
||||||
|
|
||||||
// isKnownInventory returns whether or not the peer is known to have the passed
|
// isKnownInventory returns whether or not the peer is known to have the passed
|
||||||
// inventory. It is safe for concurrent access.
|
// inventory. It is safe for concurrent access.
|
||||||
func (p *peer) isKnownInventory(invVect *btcwire.InvVect) bool {
|
func (p *peer) isKnownInventory(invVect *btcwire.InvVect) bool {
|
||||||
|
|
26
server.go
26
server.go
|
@ -26,15 +26,6 @@ const connectionRetryInterval = time.Second * 10
|
||||||
// defaultMaxOutbound is the default number of max outbound peers.
|
// defaultMaxOutbound is the default number of max outbound peers.
|
||||||
const defaultMaxOutbound = 8
|
const defaultMaxOutbound = 8
|
||||||
|
|
||||||
// directionString is a helper function that returns a string that represents
|
|
||||||
// the direction of a connection (inbound or outbound).
|
|
||||||
func directionString(inbound bool) string {
|
|
||||||
if inbound {
|
|
||||||
return "inbound"
|
|
||||||
}
|
|
||||||
return "outbound"
|
|
||||||
}
|
|
||||||
|
|
||||||
// broadcastMsg provides the ability to house a bitcoin message to be broadcast
|
// broadcastMsg provides the ability to house a bitcoin message to be broadcast
|
||||||
// to all connected peers except specified excluded peers.
|
// to all connected peers except specified excluded peers.
|
||||||
type broadcastMsg struct {
|
type broadcastMsg struct {
|
||||||
|
@ -73,10 +64,9 @@ func (s *server) handleAddPeerMsg(peers *list.List, banned map[string]time.Time,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore new peers if we're shutting down.
|
// Ignore new peers if we're shutting down.
|
||||||
direction := directionString(p.inbound)
|
|
||||||
if atomic.LoadInt32(&s.shutdown) != 0 {
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
||||||
log.Infof("[SRVR] New peer %s (%s) ignored - server is "+
|
log.Infof("[SRVR] New peer %s ignored - server is shutting "+
|
||||||
"shutting down", p.addr, direction)
|
"down", p)
|
||||||
p.Shutdown()
|
p.Shutdown()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -105,7 +95,7 @@ func (s *server) handleAddPeerMsg(peers *list.List, banned map[string]time.Time,
|
||||||
// Limit max number of total peers.
|
// Limit max number of total peers.
|
||||||
if peers.Len() >= cfg.MaxPeers {
|
if peers.Len() >= cfg.MaxPeers {
|
||||||
log.Infof("[SRVR] Max peers reached [%d] - disconnecting "+
|
log.Infof("[SRVR] Max peers reached [%d] - disconnecting "+
|
||||||
"peer %s (%s)", cfg.MaxPeers, p.addr, direction)
|
"peer %s", cfg.MaxPeers, p)
|
||||||
p.Shutdown()
|
p.Shutdown()
|
||||||
// TODO(oga) how to handle permanent peers here?
|
// TODO(oga) how to handle permanent peers here?
|
||||||
// they should be rescheduled.
|
// they should be rescheduled.
|
||||||
|
@ -113,7 +103,7 @@ func (s *server) handleAddPeerMsg(peers *list.List, banned map[string]time.Time,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the new peer and start it.
|
// Add the new peer and start it.
|
||||||
log.Debugf("[SRVR] New peer %s (%s)", p.addr, direction)
|
log.Debugf("[SRVR] New peer %s", p)
|
||||||
peers.PushBack(p)
|
peers.PushBack(p)
|
||||||
if p.inbound {
|
if p.inbound {
|
||||||
p.Start()
|
p.Start()
|
||||||
|
@ -125,7 +115,6 @@ func (s *server) handleAddPeerMsg(peers *list.List, banned map[string]time.Time,
|
||||||
// handleDonePeerMsg deals with peers that have signalled they are done. It is
|
// handleDonePeerMsg deals with peers that have signalled they are done. It is
|
||||||
// invoked from the peerHandler goroutine.
|
// invoked from the peerHandler goroutine.
|
||||||
func (s *server) handleDonePeerMsg(peers *list.List, p *peer) bool {
|
func (s *server) handleDonePeerMsg(peers *list.List, p *peer) bool {
|
||||||
direction := directionString(p.inbound)
|
|
||||||
for e := peers.Front(); e != nil; e = e.Next() {
|
for e := peers.Front(); e != nil; e = e.Next() {
|
||||||
if e.Value == p {
|
if e.Value == p {
|
||||||
|
|
||||||
|
@ -133,14 +122,11 @@ func (s *server) handleDonePeerMsg(peers *list.List, p *peer) bool {
|
||||||
// persistent outbound connection.
|
// persistent outbound connection.
|
||||||
if !p.inbound && p.persistent &&
|
if !p.inbound && p.persistent &&
|
||||||
atomic.LoadInt32(&s.shutdown) == 0 {
|
atomic.LoadInt32(&s.shutdown) == 0 {
|
||||||
// attempt reconnect.
|
e.Value = newOutboundPeer(s, p.addr, true)
|
||||||
addr := p.addr
|
|
||||||
e.Value = newOutboundPeer(s, addr, true)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
peers.Remove(e)
|
peers.Remove(e)
|
||||||
log.Debugf("[SRVR] Removed peer %s (%s)", p.addr,
|
log.Debugf("[SRVR] Removed peer %s", p)
|
||||||
direction)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue