From 29dfa220866d9bd0ed3cccb25cd9bf404a54d632 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 2 Oct 2013 20:05:10 -0500 Subject: [PATCH] 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. --- blockmanager.go | 6 ++---- peer.go | 15 +++++++++++++++ server.go | 26 ++++++-------------------- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/blockmanager.go b/blockmanager.go index ae935e4d..2fb5ddb0 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 2c33d3bc..21c3e70b 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 1f895230..cee81d2e 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 } }