diff --git a/blockmanager.go b/blockmanager.go index ae935e4d2..2fb5ddb07 100644 --- a/blockmanager.go +++ b/blockmanager.go @@ -137,8 +137,7 @@ func (b *blockManager) handleNewPeerMsg(peers *list.List, p *peer) { return } - log.Infof("[BMGR] New valid peer %s (%s)", p.addr, - directionString(p.inbound)) + log.Infof("[BMGR] New valid peer %s", p) // The peer is not a candidate for sync if it's not a full node. 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, - directionString(p.inbound)) + log.Infof("[BMGR] Lost peer %s", p) // Attempt to find a new peer to sync from if the quitting peer is the // sync peer. diff --git a/peer.go b/peer.go index 2c33d3bca..21c3e70be 100644 --- a/peer.go +++ b/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. 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. // This avoids a math import and the need to cast to floats. func minUint32(a, b uint32) uint32 { @@ -124,6 +133,12 @@ type peer struct { 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 // inventory. It is safe for concurrent access. func (p *peer) isKnownInventory(invVect *btcwire.InvVect) bool { diff --git a/server.go b/server.go index 1f8952302..cee81d2ef 100644 --- a/server.go +++ b/server.go @@ -26,15 +26,6 @@ const connectionRetryInterval = time.Second * 10 // defaultMaxOutbound is the default number of max outbound peers. 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 // to all connected peers except specified excluded peers. 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. - direction := directionString(p.inbound) if atomic.LoadInt32(&s.shutdown) != 0 { - log.Infof("[SRVR] New peer %s (%s) ignored - server is "+ - "shutting down", p.addr, direction) + log.Infof("[SRVR] New peer %s ignored - server is shutting "+ + "down", p) p.Shutdown() return false } @@ -105,7 +95,7 @@ func (s *server) handleAddPeerMsg(peers *list.List, banned map[string]time.Time, // Limit max number of total peers. if peers.Len() >= cfg.MaxPeers { log.Infof("[SRVR] Max peers reached [%d] - disconnecting "+ - "peer %s (%s)", cfg.MaxPeers, p.addr, direction) + "peer %s", cfg.MaxPeers, p) p.Shutdown() // TODO(oga) how to handle permanent peers here? // 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. - log.Debugf("[SRVR] New peer %s (%s)", p.addr, direction) + log.Debugf("[SRVR] New peer %s", p) peers.PushBack(p) if p.inbound { 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 // invoked from the peerHandler goroutine. func (s *server) handleDonePeerMsg(peers *list.List, p *peer) bool { - direction := directionString(p.inbound) for e := peers.Front(); e != nil; e = e.Next() { if e.Value == p { @@ -133,14 +122,11 @@ func (s *server) handleDonePeerMsg(peers *list.List, p *peer) bool { // persistent outbound connection. if !p.inbound && p.persistent && atomic.LoadInt32(&s.shutdown) == 0 { - // attempt reconnect. - addr := p.addr - e.Value = newOutboundPeer(s, addr, true) + e.Value = newOutboundPeer(s, p.addr, true) return false } peers.Remove(e) - log.Debugf("[SRVR] Removed peer %s (%s)", p.addr, - direction) + log.Debugf("[SRVR] Removed peer %s", p) return true } }