From 953449c21568739b0949305acb10e71821b55fb2 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 11 May 2015 21:06:07 +0100 Subject: [PATCH] refactor: extract Peer.ForEachConnectedPeer helper --- router/peer.go | 31 ++++++++++++++++++++----------- router/routes.go | 16 ++-------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/router/peer.go b/router/peer.go index cbfa9f59..3305e01a 100644 --- a/router/peer.go +++ b/router/peer.go @@ -73,15 +73,10 @@ func (peer *Peer) Routes(stopAt *Peer, establishedAndSymmetric bool) (bool, map[ if curPeer == stopAt { return true, routes } - for remoteName, conn := range curPeer.connections { - if establishedAndSymmetric && !conn.Established() { - continue - } - if _, found := routes[remoteName]; found { - continue - } - if remoteConn, found := conn.Remote().connections[curPeer.Name]; !establishedAndSymmetric || (found && remoteConn.Established()) { - nextWorklist = append(nextWorklist, conn.Remote()) + curPeer.ForEachConnectedPeer(establishedAndSymmetric, routes, + func(remotePeer *Peer) { + nextWorklist = append(nextWorklist, remotePeer) + remoteName := remotePeer.Name // We now know how to get to remoteName: the same // way we get to curPeer. Except, if curPeer is // the starting peer in which case we know we can @@ -91,9 +86,23 @@ func (peer *Peer) Routes(stopAt *Peer, establishedAndSymmetric bool) (bool, map[ } else { routes[remoteName] = routes[curPeer.Name] } - } - } + }) } } return false, routes } + +func (peer *Peer) ForEachConnectedPeer(establishedAndSymmetric bool, exclude map[PeerName]PeerName, f func(*Peer)) { + for remoteName, conn := range peer.connections { + if establishedAndSymmetric && !conn.Established() { + continue + } + if _, found := exclude[remoteName]; found { + continue + } + remotePeer := conn.Remote() + if remoteConn, found := remotePeer.connections[peer.Name]; !establishedAndSymmetric || (found && remoteConn.Established()) { + f(remotePeer) + } + } +} diff --git a/router/routes.go b/router/routes.go index e464c8e2..f244f28f 100644 --- a/router/routes.go +++ b/router/routes.go @@ -222,20 +222,8 @@ func (routes *Routes) calculateBroadcast(establishedAndSymmetric bool) map[PeerN for _, peer := range routes.peers.table { hops := []PeerName{} if found, reached := peer.Routes(routes.ourself.Peer, establishedAndSymmetric); found { - // This is rather similar to the inner loop on - // peer.Routes(...); the main difference is in the - // locking. - for remoteName, conn := range routes.ourself.connections { - if establishedAndSymmetric && !conn.Established() { - continue - } - if _, found := reached[remoteName]; found { - continue - } - if remoteConn, found := conn.Remote().connections[routes.ourself.Name]; !establishedAndSymmetric || (found && remoteConn.Established()) { - hops = append(hops, remoteName) - } - } + routes.ourself.ForEachConnectedPeer(establishedAndSymmetric, reached, + func(remotePeer *Peer) { hops = append(hops, remotePeer.Name) }) } broadcast[peer.Name] = hops }