From f3d20111d8fd18d082d0067a1ce4cde65e4ab64a Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 10 Apr 2015 10:51:40 +0100 Subject: [PATCH] eliminate dependency on LocalPeer.Router (from things outside LocalPeer) --- router/gossip.go | 14 ++++++++------ router/routes.go | 4 ++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/router/gossip.go b/router/gossip.go index c0cfcde6b7..5ae23709f4 100644 --- a/router/gossip.go +++ b/router/gossip.go @@ -83,6 +83,7 @@ type peerSenders map[PeerName]*GossipSender type GossipChannel struct { sync.Mutex ourself *LocalPeer + routes *Routes name string hash uint32 gossiper Gossiper @@ -94,6 +95,7 @@ func (router *Router) NewGossip(channelName string, g Gossiper) Gossip { channelHash := hash(channelName) channel := &GossipChannel{ ourself: router.Ourself, + routes: router.Routes, name: channelName, hash: channelHash, gossiper: g, @@ -182,9 +184,9 @@ func (c *GossipChannel) deliver(srcName PeerName, _ []byte, dec *gob.Decoder) er func (c *GossipChannel) Send(srcName PeerName, data GossipData) { // do this outside the lock below so we avoid lock nesting - c.ourself.Router.Routes.EnsureRecalculated() + c.routes.EnsureRecalculated() selectedConnections := make(ConnectionSet) - for name := range c.ourself.Router.Routes.RandomNeighbours(srcName) { + for name := range c.routes.RandomNeighbours(srcName) { if conn, found := c.ourself.ConnectionTo(name); found { selectedConnections[conn] = void } @@ -247,7 +249,7 @@ func (c *GossipChannel) GossipBroadcast(update GossipData) error { } func (c *GossipChannel) relayUnicast(dstPeerName PeerName, buf []byte) error { - if relayPeerName, found := c.ourself.Router.Routes.UnicastAll(dstPeerName); !found { + if relayPeerName, found := c.routes.UnicastAll(dstPeerName); !found { c.log("unknown relay destination:", dstPeerName) } else if conn, found := c.ourself.ConnectionTo(relayPeerName); !found { c.log("unable to find connection to relay peer", relayPeerName) @@ -258,7 +260,7 @@ func (c *GossipChannel) relayUnicast(dstPeerName PeerName, buf []byte) error { } func (c *GossipChannel) relayBroadcast(srcName PeerName, update GossipData) error { - names := c.ourself.Router.Peers.Names() // do this outside the lock so they don't nest + names := c.routes.PeerNames() // do this outside the lock so they don't nest c.Lock() defer c.Unlock() // GC - randomly (courtesy of go's map iterator) pick some @@ -291,8 +293,8 @@ func (c *GossipChannel) relayBroadcast(srcName PeerName, update GossipData) erro } func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) { - c.ourself.Router.Routes.EnsureRecalculated() - nextHops := c.ourself.Router.Routes.BroadcastAll(srcName) + c.routes.EnsureRecalculated() + nextHops := c.routes.BroadcastAll(srcName) if len(nextHops) == 0 { return } diff --git a/router/routes.go b/router/routes.go index 1e85422385..04020954fc 100644 --- a/router/routes.go +++ b/router/routes.go @@ -44,6 +44,10 @@ func (routes *Routes) Start() { go routes.run(recalculate, wait) } +func (routes *Routes) PeerNames() PeerNameSet { + return routes.peers.Names() +} + func (routes *Routes) Unicast(name PeerName) (PeerName, bool) { routes.RLock() defer routes.RUnlock()