From b1cb0c298dd63ea0c1e63e34d2bb2913536e07bd Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Thu, 10 Dec 2015 09:55:49 +0000 Subject: [PATCH] refactor --- mesh/routes.go | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/mesh/routes.go b/mesh/routes.go index d1fe3474..f2a27ecd 100644 --- a/mesh/routes.go +++ b/mesh/routes.go @@ -71,36 +71,16 @@ func (routes *Routes) UnicastAll(name PeerName) (PeerName, bool) { } func (routes *Routes) Broadcast(name PeerName) []PeerName { - routes.RLock() - hops, found := routes.broadcast[name] - routes.RUnlock() - if found { - return hops - } - res := make(chan []PeerName) - routes.action <- func() { - routes.RLock() - hops, found := routes.broadcast[name] - routes.RUnlock() - if found { - res <- hops - } - routes.peers.RLock() - routes.ourself.RLock() - hops = routes.calculateBroadcast(name, true) - routes.ourself.RUnlock() - routes.peers.RUnlock() - res <- hops - routes.Lock() - routes.broadcast[name] = hops - routes.Unlock() - } - return <-res + return routes.lookupOrCalculate(name, &routes.broadcast, true) } func (routes *Routes) BroadcastAll(name PeerName) []PeerName { + return routes.lookupOrCalculate(name, &routes.broadcastAll, false) +} + +func (routes *Routes) lookupOrCalculate(name PeerName, broadcast *broadcastRoutes, establishedAndSymmetric bool) []PeerName { routes.RLock() - hops, found := routes.broadcastAll[name] + hops, found := (*broadcast)[name] routes.RUnlock() if found { return hops @@ -108,19 +88,19 @@ func (routes *Routes) BroadcastAll(name PeerName) []PeerName { res := make(chan []PeerName) routes.action <- func() { routes.RLock() - hops, found := routes.broadcastAll[name] + hops, found := (*broadcast)[name] routes.RUnlock() if found { res <- hops } routes.peers.RLock() routes.ourself.RLock() - hops = routes.calculateBroadcast(name, false) + hops = routes.calculateBroadcast(name, establishedAndSymmetric) routes.ourself.RUnlock() routes.peers.RUnlock() res <- hops routes.Lock() - routes.broadcastAll[name] = hops + (*broadcast)[name] = hops routes.Unlock() } return <-res